Skip to content

Commit

Permalink
Always output the default header when saving ini file (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustydecapod authored and mtibben committed Dec 24, 2019
1 parent 51909f1 commit 2fe36c6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
7 changes: 6 additions & 1 deletion vault/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
)

func init() {
ini.DefaultHeader = true
ini.PrettyFormat = false
}

Expand Down Expand Up @@ -201,6 +202,10 @@ func (c *ConfigFile) ProfileSection(name string) (ProfileSection, bool) {
return profile, true
}

func (c *ConfigFile) Save() error {
return c.iniFile.SaveTo(c.Path)
}

// Add the profile to the configuration file
func (c *ConfigFile) Add(profile ProfileSection) error {
if c.iniFile == nil {
Expand All @@ -218,7 +223,7 @@ func (c *ConfigFile) Add(profile ProfileSection) error {
if err = section.ReflectFrom(&profile); err != nil {
return fmt.Errorf("Error mapping profile to ini file: %v", err)
}
return c.iniFile.SaveTo(c.Path)
return c.Save()
}

// ProfileNames returns a slice of profile names from the AWS config
Expand Down
62 changes: 61 additions & 1 deletion vault/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ region=us-east-1
parent_profile=testparentprofile1
`)

var nestedConfig = []byte(`[profile testing]
var nestedConfig = []byte(`[default]
[profile testing]
aws_access_key_id=foo
aws_secret_access_key=bar
region=us-west-2
Expand All @@ -49,6 +51,17 @@ s3=
max_queue_size=1000
`)

var defaultsOnlyConfigWithHeader = []byte(`[default]
region=us-west-2
output=json
`)

var defaultsOnlyConfigWithoutHeader = []byte(`region=us-west-2
output=json
`)

func newConfigFile(t *testing.T, b []byte) string {
f, err := ioutil.TempFile("", "aws-config")
if err != nil {
Expand Down Expand Up @@ -276,3 +289,50 @@ func TestProfileIsEmpty(t *testing.T) {
t.Errorf("Expected p to be empty")
}
}

func TestIniWithHeaderSavesWithHeader(t *testing.T) {
f := newConfigFile(t, defaultsOnlyConfigWithHeader)
defer os.Remove(f)

cfg, err := vault.LoadConfig(f)
if err != nil {
t.Fatal(err)
}

err = cfg.Save()
if err != nil {
t.Fatal(err)
}

expected := defaultsOnlyConfigWithHeader

b, _ := ioutil.ReadFile(f)

if !bytes.Equal(expected, b) {
t.Fatalf("Expected:\n%q\nGot:\n%q", expected, b)
}

}

func TestIniWithoutHeaderSavesWithHeader(t *testing.T) {
f := newConfigFile(t, defaultsOnlyConfigWithoutHeader)
defer os.Remove(f)

cfg, err := vault.LoadConfig(f)
if err != nil {
t.Fatal(err)
}

err = cfg.Save()
if err != nil {
t.Fatal(err)
}

expected := defaultsOnlyConfigWithHeader

b, _ := ioutil.ReadFile(f)

if !bytes.Equal(expected, b) {
t.Fatalf("Expected:\n%q\nGot:\n%q", expected, b)
}
}

0 comments on commit 2fe36c6

Please sign in to comment.