跳过host检查
(sshDirectory string, isReset bool)
| 41 | |
| 42 | // 跳过host检查 |
| 43 | func (fs *fileOperation) SkipStrictHostKeyChecking(sshDirectory string, isReset bool) error { |
| 44 | sshConfigPath := filepath.Join(sshDirectory, "config") |
| 45 | |
| 46 | // 是否存在 |
| 47 | if !IsExist(sshConfigPath) { |
| 48 | SmartIDELog.Debug(fmt.Sprintf("创建 %v 文件", sshConfigPath)) |
| 49 | |
| 50 | // 文件夹是否存在 |
| 51 | dirPath := filepath.Dir(sshConfigPath) |
| 52 | if dirPath != "" && !IsExist(dirPath) { |
| 53 | err := os.MkdirAll(dirPath, os.ModePerm) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // 创建文件 |
| 60 | f, err := os.Create(sshConfigPath) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | defer f.Close() |
| 65 | } |
| 66 | |
| 67 | // 写入配置 |
| 68 | b, err := fs.CheckFileContainsStr(sshConfigPath, "## smartide StrictHostKeyChecking ##") // 检查是否存在指定的文本 |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | // 是否重置 |
| 74 | lines := `## smartide StrictHostKeyChecking ## |
| 75 | HOST * |
| 76 | StrictHostKeyChecking no` |
| 77 | if isReset { |
| 78 | fileContentBytes, err := os.ReadFile(sshConfigPath) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | fileContent := string(fileContentBytes) |
| 83 | if strings.Contains(fileContent, lines) { |
| 84 | fileContent = strings.Replace(fileContent, lines, "", -1) |
| 85 | return os.WriteFile(sshConfigPath, []byte(fileContent), 0644) |
| 86 | } |
| 87 | |
| 88 | } else { |
| 89 | if !b { |
| 90 | return fs.AppendToFile(sshConfigPath, lines) // 写入 |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (fs *fileOperation) CreateOrOverWrite(filePath string, content string) error { |
| 99 | return writeToFile(filePath, content, true) |
no test coverage detected