MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / rewriteSSHManagedDirectives

Function rewriteSSHManagedDirectives

agent/app/service/ssh.go:1074–1105  ·  view source on GitHub ↗
(fileName, key string, newDirectives []string)

Source from the content-addressed store, hash-verified

1072}
1073
1074func 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
1107func commentSSHConfigLines(fileName string, targetLines []int) error {
1108 content, err := os.ReadFile(fileName)

Callers 2

Calls 5

ensureSSHManagedMarkerFunction · 0.85
loadSSHManagedBlockFunction · 0.85
parseSSHConfigLineFunction · 0.85
insertSSHDirectivesAtFunction · 0.85
WriteFileMethod · 0.80

Tested by

no test coverage detected