diff --git a/README.md b/README.md index b65ab95..f22ff86 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 where you can find the username. + - bamboo.jira.username # It stops on the first username found in the list. ``` ## 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