func validateConfigContent(t *testing.T, config string, search string) { lines, err := file.ReadLines(config) if err != nil { t.Fatalf("cannot read file %s: %v", config, err) } for _, line := range lines { if strings.Contains(line, search) { // search string was found. Test was successful return } } t.Errorf("Could not find search string %q in config file %s", search, config) }
// readSecret reads the contents of a secret file func readSecret(baseDir, fileName string) (string, error) { path := filepath.Join(baseDir, fileName) lines, err := file.ReadLines(path) if err != nil { if os.IsNotExist(err) { return "", nil } return "", err } // If the file is empty, simply return empty string if len(lines) == 0 { return "", nil } return lines[0], nil }
func validateConfig(t *testing.T, config string, search string) { if len(config) == 0 { return } lines, err := file.ReadLines(config) if err != nil { t.Fatalf("cannot read file %s: %v", config, err) } for _, line := range lines { if !strings.HasPrefix(line, "path = ") { continue } includedConfig := strings.TrimPrefix(line, "path = ") validateConfigContent(t, includedConfig, search) } }
func cleanupConfig(config string) { if len(config) == 0 { return } lines, err := file.ReadLines(config) if err != nil { // abort cleanup on error return } for _, line := range lines { if !strings.HasPrefix(line, "path = ") { continue } cleanupDir(strings.TrimPrefix(line, "path = ")) } cleanupDir(filepath.Dir(config)) }
// ensureGitConfigIncludes ensures that the OS env var GIT_CONFIG is set and // that it points to a file that has an include statement for the given path func ensureGitConfigIncludes(path string) error { gitconfig := os.Getenv("GIT_CONFIG") if gitconfig == "" { return createGitConfig(path) } lines, err := file.ReadLines(gitconfig) if err != nil { return err } for _, line := range lines { // If include already exists, return with no error if line == fmt.Sprintf("path = %s", path) { return nil } } lines = append(lines, fmt.Sprintf("path = %s", path)) content := []byte(strings.Join(lines, "\n")) return ioutil.WriteFile(gitconfig, content, 0644) }