From 2fe36c67b2fa88602f25b1e59be3907721e40ab1 Mon Sep 17 00:00:00 2001 From: Ber Zoidberg Date: Mon, 23 Dec 2019 18:21:09 -0800 Subject: [PATCH] Always output the default header when saving ini file (#476) --- vault/config.go | 7 ++++- vault/config_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/vault/config.go b/vault/config.go index 8534ea6d8..428ac761b 100644 --- a/vault/config.go +++ b/vault/config.go @@ -36,6 +36,7 @@ const ( ) func init() { + ini.DefaultHeader = true ini.PrettyFormat = false } @@ -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 { @@ -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 diff --git a/vault/config_test.go b/vault/config_test.go index b2c18d4db..2fb1669c8 100644 --- a/vault/config_test.go +++ b/vault/config_test.go @@ -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 @@ -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 { @@ -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) + } +}