Skip to content

Commit

Permalink
fixing bumpr merge. If cli flags have not been passed, dont override …
Browse files Browse the repository at this point in the history
…values from config file.
  • Loading branch information
AnalogJ committed Mar 10, 2022
2 parents 32fd460 + fe84bd5 commit f8b6229
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cmd/bumpr/bumpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ func main() {
Action: func(c *cli.Context) error {

configuration, _ := config.Create()
configuration.Set(config.PACKAGR_SCM, c.String("scm"))
configuration.Set(config.PACKAGR_PACKAGE_TYPE, c.String("package_type"))
if c.IsSet("scm") {
configuration.Set(config.PACKAGR_SCM, c.String("scm"))
}
if c.IsSet("package_type") {
configuration.Set(config.PACKAGR_PACKAGE_TYPE, c.String("package_type"))
}

//config.Set("dry_run", c.String("dry_run"))

fmt.Println("package type:", configuration.GetString(config.PACKAGR_PACKAGE_TYPE))
Expand Down
29 changes: 29 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package config

import (
stderrors "errors"
"fmt"
"github.com/analogj/go-util/utils"
"github.com/spf13/viper"
"log"
"os"
)

// When initializing this class the following methods must be called:
Expand All @@ -26,6 +31,7 @@ func (c *configuration) Init() error {
c.SetDefault(PACKAGR_PACKAGE_TYPE, "generic")
c.SetDefault(PACKAGR_SCM, "default")
c.SetDefault(PACKAGR_VERSION_BUMP_TYPE, "patch")
c.SetDefault(PACKAGR_ENGINE_REPO_CONFIG_PATH, "packagr.yml")

//set the default system config file search path.
//if you want to load a non-standard location system config file (~/capsule.yml), use ReadConfig
Expand All @@ -41,3 +47,26 @@ func (c *configuration) Init() error {

return nil
}

func (c *configuration) ReadConfig(configFilePath string) error {

if !utils.FileExists(configFilePath) {
message := fmt.Sprintf("The configuration file (%s) could not be found. Skipping", configFilePath)
log.Printf(message)
return stderrors.New(message)
}

log.Printf("Loading configuration file: %s", configFilePath)

config_data, err := os.Open(configFilePath)
if err != nil {
log.Printf("Error reading configuration file: %s", err)
return err
}
err = c.MergeConfig(config_data)
if err != nil {
log.Printf("Error merging config file: %s", err)
return err
}
return nil
}
19 changes: 19 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/packagrio/bumpr/pkg/config"
"github.com/stretchr/testify/require"
"os"
"path"
"testing"
)

Expand Down Expand Up @@ -36,3 +37,21 @@ func TestConfiguration_init_EnvVariablesShouldLoadProperly(t *testing.T) {
//teardown
os.Unsetenv("PACKAGR_VERSION_BUMP_TYPE")
}

func TestConfiguration_ReadConfig(t *testing.T) {
//setup
testConfig, _ := config.Create()
testConfig.SetDefault(config.PACKAGR_PACKAGE_TYPE, "generic")
testConfig.SetDefault(config.PACKAGR_SCM, "default")
testConfig.SetDefault(config.PACKAGR_VERSION_BUMP_TYPE, "patch")

//test
err := testConfig.ReadConfig(path.Join("testdata", "simple_overrides.yml"))

//assert
require.NoErrorf(t, err, "No error")
require.Equal(t, "golang", testConfig.GetString(config.PACKAGR_PACKAGE_TYPE), "should populate Package Type from overrides config file")
require.Equal(t, "github", testConfig.GetString(config.PACKAGR_SCM), "should populate SCM from overrides config file")
require.Equal(t, "major", testConfig.GetString(config.PACKAGR_VERSION_BUMP_TYPE), "should populate Engine Version Bump Type from overrides config file")

}
2 changes: 2 additions & 0 deletions pkg/config/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ type Interface interface {
GetString(key string) string
GetStringSlice(key string) []string
UnmarshalKey(key string, rawVal interface{}, decoder ...viper.DecoderConfigOption) error
ReadConfig(configFilePath string) error
}

const PACKAGR_PACKAGE_TYPE = "package_type"
const PACKAGR_SCM = "scm"
const PACKAGR_VERSION_BUMP_TYPE = "version_bump_type"
const PACKAGR_VERSION_METADATA_PATH = "version_metadata_path"
const PACKAGR_ENGINE_REPO_CONFIG_PATH = "engine_repo_config_path"
const PACKAGR_GENERIC_VERSION_TEMPLATE = "generic_version_template"
3 changes: 3 additions & 0 deletions pkg/config/testdata/simple_overrides.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package_type: 'golang'
scm: 'github'
version_bump_type: 'major'
25 changes: 25 additions & 0 deletions pkg/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package pkg

import (
"errors"
"fmt"
"github.com/analogj/go-util/utils"
"github.com/packagrio/bumpr/pkg/config"
"github.com/packagrio/bumpr/pkg/engine"
"github.com/packagrio/go-common/pipeline"
"github.com/packagrio/go-common/scm"
"log"
"os"
"path"
"path/filepath"
)

Expand All @@ -27,6 +31,11 @@ func (p *Pipeline) Start(configData config.Interface) error {
p.Data.GitLocalPath = cwdPath
p.Data.GitParentPath = filepath.Dir(cwdPath)

//Parse Repo config if present.
if err := p.ParseRepoConfig(); err != nil {
return err
}

sourceScm, err := scm.Create(p.Config.GetString(config.PACKAGR_SCM), p.Data, p.Config, nil)
if err != nil {
fmt.Printf("FATAL: %+v\n", err)
Expand Down Expand Up @@ -67,3 +76,19 @@ func (p *Pipeline) Start(configData config.Interface) error {
fmt.Printf("version bumped to %s", p.Data.ReleaseVersion)
return nil
}

func (p *Pipeline) ParseRepoConfig() error {
log.Println("parse_repo_config")
// update the config with repo config file options
repoConfig := path.Join(p.Data.GitLocalPath, p.Config.GetString(config.PACKAGR_ENGINE_REPO_CONFIG_PATH))
if utils.FileExists(repoConfig) {
log.Println("Found config file in working dir!")
if err := p.Config.ReadConfig(repoConfig); err != nil {
return errors.New("An error occured while parsing repository packagr.yml file")
}
} else {
log.Println("No repo packagr.yml file found, using existing config.")
}

return nil
}

0 comments on commit f8b6229

Please sign in to comment.