11package main
22
33import (
4+ "bufio"
45 "encoding/json"
56 "fmt"
67 "io/ioutil"
@@ -13,37 +14,79 @@ import (
1314 "syscall"
1415
1516 "github.com/mitchellh/go-homedir"
16- "gopkg.in/ini.v1"
1717)
1818
19+ func setConfigKey (filename , section , line string , unset bool ) error {
20+ fileinfo , err := os .Stat (filename )
21+ if err != nil {
22+ return err
23+ }
24+
25+ file , err := os .OpenFile (filename , os .O_RDONLY , fileinfo .Mode ().Perm ())
26+ if err != nil {
27+ return err
28+ }
29+ defer file .Close ()
30+
31+ var newLines []string
32+ isCorrectSection := false
33+
34+ scanner := bufio .NewScanner (file )
35+ for scanner .Scan () {
36+ scannedLine := scanner .Text ()
37+ if scannedLine != line || ! unset || ! isCorrectSection { // write all but lines to be removed
38+ newLines = append (newLines , scannedLine )
39+ }
40+ if scannedLine == fmt .Sprintf ("[%s]" , section ) {
41+ isCorrectSection = true
42+ if ! unset {
43+ newLines = append (newLines , line )
44+ }
45+ }
46+ }
47+ if err := scanner .Err (); err != nil {
48+ return err
49+ }
50+ file .Close ()
51+
52+ file , err = os .OpenFile (filename , os .O_RDWR | os .O_CREATE | os .O_TRUNC , fileinfo .Mode ().Perm ())
53+ if err != nil {
54+ return err
55+ }
56+ defer file .Close ()
57+
58+ writer := bufio .NewWriter (file )
59+ for _ , line := range newLines {
60+ fmt .Fprintln (writer , line )
61+ }
62+ return writer .Flush ()
63+ }
64+
1965func setINIConfigAndFileFlush () {
2066 // set ini
2167 if * setiniFlag {
2268 cfgfile , err := homedir .Expand ("~/.aws/config" )
2369 if err != nil {
24- return
25- }
26-
27- cfg , err := ini .Load (cfgfile )
28- if err != nil {
29- return
70+ log .Fatal (err )
3071 }
3172
3273 if * profileFlag == "default" {
3374 if * modeFlag == "csm" {
34- cfg . Section ( "default" ). Key ( "csm_enabled" ). SetValue ( " true" )
75+ err = setConfigKey ( cfgfile , "default" , "csm_enabled = true" , false )
3576 } else if * modeFlag == "proxy" {
36- cfg . Section ( "default" ). Key ("ca_bundle" ). SetValue ( * caBundleFlag )
77+ err = setConfigKey ( cfgfile , "default" , fmt . Sprintf ("ca_bundle = %s" , * caBundleFlag ), false )
3778 }
3879 } else {
3980 if * modeFlag == "csm" {
40- cfg . Section ( fmt .Sprintf ("profile %s" , * profileFlag )). Key ( "csm_enabled" ). SetValue ( " true" )
81+ err = setConfigKey ( cfgfile , fmt .Sprintf ("profile %s" , * profileFlag ), "csm_enabled = true" , false )
4182 } else if * modeFlag == "proxy" {
42- cfg . Section ( fmt .Sprintf ("profile %s" , * profileFlag )). Key ("ca_bundle" ). SetValue ( * caBundleFlag )
83+ err = setConfigKey ( cfgfile , fmt .Sprintf ("profile %s" , * profileFlag ), fmt . Sprintf ("ca_bundle = %s" , * caBundleFlag ), false )
4384 }
4485 }
4586
46- cfg .SaveTo (cfgfile )
87+ if err != nil {
88+ log .Fatal (err )
89+ }
4790 }
4891
4992 // listen for exit, cleanup and flush
@@ -71,25 +114,19 @@ func setINIConfigAndFileFlush() {
71114 os .Exit (1 )
72115 }
73116
74- cfg , err := ini .Load (cfgfile )
75- if err != nil {
76- os .Exit (1 )
77- }
78-
79117 if * profileFlag == "default" {
80118 if * modeFlag == "csm" {
81- cfg . Section ( "default" ). DeleteKey ( "csm_enabled" )
119+ setConfigKey ( cfgfile , "default" , "csm_enabled = true" , true )
82120 } else if * modeFlag == "proxy" {
83- cfg . Section ( "default" ). DeleteKey ("ca_bundle" )
121+ setConfigKey ( cfgfile , "default" , fmt . Sprintf ("ca_bundle = %s" , * caBundleFlag ), true )
84122 }
85123 } else {
86124 if * modeFlag == "csm" {
87- cfg . Section ( fmt .Sprintf ("profile %s" , * profileFlag )). DeleteKey ( "csm_enabled" )
125+ setConfigKey ( cfgfile , fmt .Sprintf ("profile %s" , * profileFlag ), "csm_enabled = true" , true )
88126 } else if * modeFlag == "proxy" {
89- cfg . Section ( fmt .Sprintf ("profile %s" , * profileFlag )). DeleteKey ("ca_bundle" )
127+ setConfigKey ( cfgfile , fmt .Sprintf ("profile %s" , * profileFlag ), fmt . Sprintf ("ca_bundle = %s" , * caBundleFlag ), true )
90128 }
91129 }
92- cfg .SaveTo (cfgfile )
93130 }
94131
95132 pprof .StopCPUProfile ()
0 commit comments