Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #271 - Add ask_vault_pass config setting #321

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion trellis/cli_config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package trellis

import (
"os"
)

type CliConfig struct {
Open map[string]string `yaml:"open"`
AskVaultPass bool `yaml:"ask_vault_pass"`
Open map[string]string `yaml:"open"`
}

func (c *CliConfig) Init() error {
if c.AskVaultPass {
// https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-ask-vault-pass
os.Setenv("ANSIBLE_ASK_VAULT_PASS", "true")
}

return nil
}
30 changes: 30 additions & 0 deletions trellis/cli_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package trellis

import (
"os"
"testing"
)

func TestCliConfigInit(t *testing.T) {
config := CliConfig{}
config.Init()

_, ok := os.LookupEnv("ANSIBLE_ASK_VAULT_PASS")

if ok {
t.Error("expected ANSIBLE_ASK_VAULT_PASS to not be set")
}
}

func TestCliConfigInitEnablesAskVaultPass(t *testing.T) {
config := CliConfig{AskVaultPass: true}
config.Init()

env := os.Getenv("ANSIBLE_ASK_VAULT_PASS")

os.Setenv("ANSIBLE_ASK_VAULT_PASS", "")

if env != "true" {
t.Error("expected ANSIBLE_ASK_VAULT_PASS to be true")
}
}
25 changes: 17 additions & 8 deletions trellis/trellis.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (t *Trellis) LoadProject() error {

wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
return fmt.Errorf("Fatal error getting current working directory: %v", err)
}

path, ok := t.Detect(wd)
Expand All @@ -163,7 +163,11 @@ func (t *Trellis) LoadProject() error {
return errors.New("No Trellis project detected in the current directory or any of its parent directories.")
}

t.CliConfig = t.LoadCliConfig()
t.CliConfig, err = t.LoadCliConfig()
if err != nil {
return err
}

t.Path = path
t.Virtualenv = NewVirtualenv(t.ConfigPath())

Expand Down Expand Up @@ -256,21 +260,26 @@ func (t *Trellis) getDefaultSiteNameFromEnvironment(environment string) (siteNam
return sites[0], nil
}

func (t *Trellis) LoadCliConfig() *CliConfig {
config := &CliConfig{}
configYaml, err := os.ReadFile(filepath.Join(t.ConfigPath(), ConfigFile))
func (t *Trellis) LoadCliConfig() (config *CliConfig, err error) {
config = &CliConfig{}
path := filepath.Join(t.ConfigPath(), ConfigFile)
configYaml, err := os.ReadFile(path)

if err != nil && !os.IsNotExist(err) {
log.Fatalln(err)
return config, fmt.Errorf("Error reading CLI config file %s: %v", path, err)
}

if err == nil {
if err = yaml.UnmarshalStrict(configYaml, &config); err != nil {
log.Fatalln(err)
return config, fmt.Errorf("Error parsing CLI config file: %v", err)
}
}

return config
if err = config.Init(); err != nil {
return config, fmt.Errorf("Error initializing CLI config: %v", err)
}

return config, nil
}

func (t *Trellis) SiteFromEnvironmentAndName(environment string, name string) *Site {
Expand Down
4 changes: 2 additions & 2 deletions trellis/trellis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestLoadProjectForProjects(t *testing.T) {
func TestLoadCliConfigWhenFileDoesNotExist(t *testing.T) {
tp := NewTrellis()

config := tp.LoadCliConfig()
config, _ := tp.LoadCliConfig()

if config == nil {
t.Error("expected config object")
Expand All @@ -335,7 +335,7 @@ func TestLoadCliConfigWhenFileExists(t *testing.T) {
t.Fatal(err)
}

config := tp.LoadCliConfig()
config, _ := tp.LoadCliConfig()

if !reflect.DeepEqual(config, &CliConfig{}) {
t.Error("expected open object")
Expand Down