(fileName, key string, newDirectives []string)
| 1072 | } |
| 1073 | |
| 1074 | func rewriteSSHManagedDirectives(fileName, key string, newDirectives []string) error { |
| 1075 | content, err := os.ReadFile(fileName) |
| 1076 | if err != nil { |
| 1077 | return err |
| 1078 | } |
| 1079 | lines := strings.Split(string(content), "\n") |
| 1080 | lines, insertAt := ensureSSHManagedMarker(lines) |
| 1081 | |
| 1082 | block, ok := loadSSHManagedBlock(lines) |
| 1083 | if ok { |
| 1084 | var filtered []string |
| 1085 | for index := block.Start; index < block.End; index++ { |
| 1086 | line := strings.TrimSpace(lines[index]) |
| 1087 | if line == "" || strings.HasPrefix(line, "#") { |
| 1088 | filtered = append(filtered, lines[index]) |
| 1089 | continue |
| 1090 | } |
| 1091 | itemKey, _, ok := parseSSHConfigLine(line) |
| 1092 | if ok && strings.EqualFold(itemKey, key) { |
| 1093 | continue |
| 1094 | } |
| 1095 | filtered = append(filtered, lines[index]) |
| 1096 | } |
| 1097 | lines = append(append(lines[:block.Start], filtered...), lines[block.End:]...) |
| 1098 | insertAt = block.Start |
| 1099 | } |
| 1100 | |
| 1101 | if len(newDirectives) != 0 { |
| 1102 | lines = insertSSHDirectivesAt(lines, insertAt, newDirectives) |
| 1103 | } |
| 1104 | return os.WriteFile(fileName, []byte(strings.Join(lines, "\n")), constant.FilePerm) |
| 1105 | } |
| 1106 | |
| 1107 | func commentSSHConfigLines(fileName string, targetLines []int) error { |
| 1108 | content, err := os.ReadFile(fileName) |
no test coverage detected