Skip to content

Commit

Permalink
Use newest API for config upgrades (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmiyake authored Mar 25, 2018
1 parent 9b29386 commit b2f71cc
Show file tree
Hide file tree
Showing 152 changed files with 31,055 additions and 386 deletions.
14 changes: 10 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions commoncmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"github.com/pkg/errors"
"gopkg.in/yaml.v2"

"github.com/palantir/go-license/golicense"
"github.com/palantir/go-license/golicense/config"
)

func LoadConfig(cfgFile string) (golicense.ProjectConfig, error) {
func LoadConfig(cfgFile string) (config.ProjectConfig, error) {
cfgYML, err := ioutil.ReadFile(cfgFile)
if err != nil {
return golicense.ProjectConfig{}, errors.Wrapf(err, "failed to read file %s", cfgFile)
return config.ProjectConfig{}, errors.Wrapf(err, "failed to read file %s", cfgFile)
}

upgradedBytes, err := UpgradeConfig(cfgYML)
upgradedBytes, err := config.UpgradeConfig(cfgYML)
if err != nil {
return golicense.ProjectConfig{}, errors.Wrapf(err, "failed to read file %s", cfgFile)
return config.ProjectConfig{}, errors.Wrapf(err, "failed to read file %s", cfgFile)
}

var cfg golicense.ProjectConfig
var cfg config.ProjectConfig
if err := yaml.Unmarshal(upgradedBytes, &cfg); err != nil {
return golicense.ProjectConfig{}, errors.Wrapf(err, "failed to unmarshal configuration as YAML")
return config.ProjectConfig{}, errors.Wrapf(err, "failed to unmarshal configuration as YAML")
}
return cfg, nil
}
4 changes: 2 additions & 2 deletions godelplugin/cmd/upgradeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ package cmd
import (
"github.com/palantir/godel/framework/pluginapi"

"github.com/palantir/go-license/commoncmd"
"github.com/palantir/go-license/golicense/config"
)

var upgradeConfigCmd = pluginapi.CobraUpgradeConfigCmd(commoncmd.UpgradeConfig)
var upgradeConfigCmd = pluginapi.CobraUpgradeConfigCmd(config.UpgradeConfig)

func init() {
RootCmd.AddCommand(upgradeConfigCmd)
Expand Down
144 changes: 0 additions & 144 deletions golicense/config.go

This file was deleted.

106 changes: 106 additions & 0 deletions golicense/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2016 Palantir Technologies Inc. All rights reserved.
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

package config

import (
"fmt"
"sort"
"strings"

"github.com/pkg/errors"

"github.com/palantir/go-license/golicense"
"github.com/palantir/go-license/golicense/config/internal/v0"
)

type ProjectConfig v0.ProjectConfig

func (cfg *ProjectConfig) ToParam() (golicense.ProjectParam, error) {
customHeaders := make([]golicense.CustomHeaderParam, len(cfg.CustomHeaders))
for i, v := range cfg.CustomHeaders {
v := CustomHeaderConfig(v)
headerVal, err := v.ToParam()
if err != nil {
return golicense.ProjectParam{}, err
}
customHeaders[i] = headerVal
}

if err := validateCustomHeaderParams(customHeaders); err != nil {
return golicense.ProjectParam{}, err
}
return golicense.ProjectParam{
Licenser: golicense.NewLicenser(cfg.Header),
CustomHeaders: customHeaders,
Exclude: cfg.Exclude.Matcher(),
}, nil
}

func validateCustomHeaderParams(headerParams []golicense.CustomHeaderParam) error {
allNames := make(map[string]struct{})
collisions := make(map[string]struct{})
for _, param := range headerParams {
if _, seen := allNames[param.Name]; seen {
collisions[param.Name] = struct{}{}
}
allNames[param.Name] = struct{}{}
}
if len(collisions) > 0 {
var sortedNames []string
for k := range collisions {
sortedNames = append(sortedNames, k)
}
sort.Strings(sortedNames)
return errors.Errorf("custom header(s) defined multiple times: %v", sortedNames)
}

// map from path to custom header entries that have the path
pathsToCustomEntries := make(map[string][]string)
for _, ch := range headerParams {
for _, path := range ch.IncludePaths {
pathsToCustomEntries[path] = append(pathsToCustomEntries[path], ch.Name)
}
}
var customPathCollisionMsgs []string
sortedKeys := make([]string, 0, len(pathsToCustomEntries))
for k := range pathsToCustomEntries {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
for _, k := range sortedKeys {
v := pathsToCustomEntries[k]
if len(v) > 1 {
customPathCollisionMsgs = append(customPathCollisionMsgs, fmt.Sprintf("%s: %s", k, strings.Join(v, ", ")))
}
}
if len(customPathCollisionMsgs) > 0 {
return errors.Errorf(strings.Join(append([]string{"the same path is defined by multiple custom header entries:"}, customPathCollisionMsgs...), "\n\t"))
}
return nil
}

type CustomHeaderConfig v0.CustomHeaderConfig

func ToCustomHeaderConfigs(in []CustomHeaderConfig) []v0.CustomHeaderConfig {
if in == nil {
return nil
}
out := make([]v0.CustomHeaderConfig, len(in))
for i, v := range in {
out[i] = v0.CustomHeaderConfig(v)
}
return out
}

func (cfg *CustomHeaderConfig) ToParam() (golicense.CustomHeaderParam, error) {
if cfg.Name == "" {
return golicense.CustomHeaderParam{}, errors.Errorf("custom header name cannot be blank")
}
return golicense.CustomHeaderParam{
Name: cfg.Name,
Licenser: golicense.NewLicenser(cfg.Header),
IncludePaths: cfg.Paths,
}, nil
}
6 changes: 3 additions & 3 deletions golicense/example_test.go → golicense/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

package golicense_test
package config_test

import (
"fmt"

"gopkg.in/yaml.v2"

"github.com/palantir/go-license/golicense"
"github.com/palantir/go-license/golicense/config"
)

func Example() {
Expand All @@ -28,7 +28,7 @@ custom-headers:
paths:
- subprojectDir
`
var cfg golicense.ProjectConfig
var cfg config.ProjectConfig
if err := yaml.Unmarshal([]byte(yml), &cfg); err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit b2f71cc

Please sign in to comment.