Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #75 from remyLemeunier/fix-name
Browse files Browse the repository at this point in the history
Fix name
  • Loading branch information
remyLemeunier authored Nov 15, 2017
2 parents fe6dd2a + 583c092 commit 08dce31
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions examples/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ metrics:
lockSystem:
fileLock:
filePath: /tmp

potentialUsername:
- USER
- bamboo.jira.username
1 change: 1 addition & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
HookConfig `mapstructure:"hooks"`
LockSystemConfig `mapstructure:"lockSystem"`
MetricsConfig `mapstructure:"metrics"`
PotentialUsername []string
}

type DeployerConfig struct {
Expand Down
11 changes: 10 additions & 1 deletion utils/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package utils

import (
"github.com/spf13/viper"
"testing"

"github.com/spf13/viper"
)

func TestLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions utils/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ hooks:
lockSystem:
fileLock:
filePath: /tmp

potentialUsername:
- USER
- bamboo.jira.username

0 comments on commit 08dce31

Please sign in to comment.