From 919af939d218f9dcd17d0174ecb02c4dab37669c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Lemeunier?= Date: Wed, 15 Nov 2017 15:10:35 +0100 Subject: [PATCH 1/2] Fix name --- README.md | 3 +++ commands/deploy.go | 12 +++++++++++- context/context.go | 21 +++++++++++++-------- examples/config.sample.yml | 4 ++++ utils/config.go | 1 + utils/config_test.go | 11 ++++++++++- utils/testdata/config.yaml | 4 ++++ 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b65ab95..85b700d 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,9 @@ lockSystem: # Define a lock system in order to avoid multiple com fileLock: # Currently we only have the lock by file. filePath: /tmp # Path where the is going to be written. +potentialUsername: # By default get the current user. + - USER # You can also have a list of env variable + - bamboo.jira.username # where you can find the username ``` ## Configuration by service (manifest) diff --git a/commands/deploy.go b/commands/deploy.go index b242484..5323290 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -47,10 +47,20 @@ func (d *Deploy) execute() { defer timer.ObserveDuration() currentUser, err := user.Current() - if err == nil { + if err == nil && currentUser.Name != "" { userName = currentUser.Name } + if len(d.Context.PotentialUsername) > 0 { + for _, value := range d.Context.PotentialUsername { + potentialUserName := os.Getenv(value) + if potentialUserName != "" { + userName = potentialUserName + break + } + } + } + if err := utils.CheckIfIsLaunchedInAScreen(); err != nil && d.Context.ScreenMandatory == true { log.Errorln(fmt.Sprintf("Screen error raised: %q", err)) return diff --git a/context/context.go b/context/context.go index 7318141..137937c 100644 --- a/context/context.go +++ b/context/context.go @@ -11,14 +11,15 @@ import ( ) type Context struct { - Deployer deployers.Deployer - Vcs services.Sources - Binaries services.Binaries - Hooks []hooks.Hooks - LockSystem utils.Lock - ScreenMandatory bool - Log *log.Logger - Metrics *utils.MetricsRegistry + Deployer deployers.Deployer + Vcs services.Sources + Binaries services.Binaries + Hooks []hooks.Hooks + LockSystem utils.Lock + ScreenMandatory bool + PotentialUsername []string + Log *log.Logger + Metrics *utils.MetricsRegistry } func NewContext(cfg *utils.Config, manifest *utils.Manifest) (*Context, error) { @@ -112,6 +113,10 @@ func NewContext(cfg *utils.Config, manifest *utils.Manifest) (*Context, error) { } } + if len(cfg.PotentialUsername) > 0 { + ctx.PotentialUsername = cfg.PotentialUsername + } + if cfg.MetricsConfig.PrometheusConfig != (utils.PrometheusConfig{}) { ctx.Metrics = utils.NewPrometheusMetricsRegistry(cfg.MetricsConfig.PrometheusConfig) } else { diff --git a/examples/config.sample.yml b/examples/config.sample.yml index 6d1e77e..605fbc6 100644 --- a/examples/config.sample.yml +++ b/examples/config.sample.yml @@ -44,3 +44,7 @@ metrics: lockSystem: fileLock: filePath: /tmp + +potentialUsername: + - USER + - bamboo.jira.username diff --git a/utils/config.go b/utils/config.go index eb38a25..d606ab2 100644 --- a/utils/config.go +++ b/utils/config.go @@ -19,6 +19,7 @@ type Config struct { HookConfig `mapstructure:"hooks"` LockSystemConfig `mapstructure:"lockSystem"` MetricsConfig `mapstructure:"metrics"` + PotentialUsername []string } type DeployerConfig struct { diff --git a/utils/config_test.go b/utils/config_test.go index b0c01ec..3ddb5c6 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -1,8 +1,9 @@ package utils import ( - "github.com/spf13/viper" "testing" + + "github.com/spf13/viper" ) func TestLoadConfig(t *testing.T) { @@ -66,6 +67,14 @@ func TestLoadConfig(t *testing.T) { if cfg.ScreenMandatory != true { t.Error("ScreenMandatory was expected to be true") } + + if len(cfg.PotentialUsername) != 2 { + t.Fatalf("PotentialUsername length should be 2 instead got: %q", len(cfg.PotentialUsername)) + } + + if cfg.PotentialUsername[0] != "USER" || cfg.PotentialUsername[1] != "bamboo.jira.username" { + t.Error("Error PotentialUsername doesn't contain the right informations.") + } } func TestDiscoverServices(t *testing.T) { diff --git a/utils/testdata/config.yaml b/utils/testdata/config.yaml index a862870..5990e4a 100644 --- a/utils/testdata/config.yaml +++ b/utils/testdata/config.yaml @@ -45,3 +45,7 @@ hooks: lockSystem: fileLock: filePath: /tmp + +potentialUsername: + - USER + - bamboo.jira.username \ No newline at end of file From 583c0920e1c64621d13881175093a26814c74e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Lemeunier?= Date: Wed, 15 Nov 2017 15:19:06 +0100 Subject: [PATCH 2/2] add more explanations on 'username' in the readme. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 85b700d..f22ff86 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,8 @@ lockSystem: # Define a lock system in order to avoid multiple com filePath: /tmp # Path where the is going to be written. potentialUsername: # By default get the current user. - - USER # You can also have a list of env variable - - bamboo.jira.username # where you can find the username + - USER # You can also have a list of env variable where you can find the username. + - bamboo.jira.username # It stops on the first username found in the list. ``` ## Configuration by service (manifest)