(filename, section, line string, unset bool)
| 17 | ) |
| 18 | |
| 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 || !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 | } else if strings.HasPrefix(scannedLine, "[") && strings.HasSuffix(scannedLine, "]") { |
| 46 | isCorrectSection = false |
| 47 | } |
| 48 | } |
| 49 | if err := scanner.Err(); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | file.Close() |
| 53 | |
| 54 | file, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileinfo.Mode().Perm()) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | defer file.Close() |
| 59 | |
| 60 | writer := bufio.NewWriter(file) |
| 61 | for _, line := range newLines { |
| 62 | fmt.Fprintln(writer, line) |
| 63 | } |
| 64 | return writer.Flush() |
| 65 | } |
| 66 | |
| 67 | func setINIConfigAndFileFlush() { |
| 68 | cfgfilepath := "~/.aws/config" |
no outgoing calls
no test coverage detected