-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
gitignore.go
54 lines (45 loc) · 1.04 KB
/
gitignore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package config
import (
"fmt"
"os"
ignore "github.com/sabhiram/go-gitignore"
)
func (config TalhelperConfig) GenerateGitignore(outputDir string) error {
for _, node := range config.Nodes {
fileName := config.ClusterName + "-" + node.Hostname + ".yaml"
err := createGitIgnore(outputDir, fileName)
if err != nil {
return err
}
}
fileName := "talosconfig"
err := createGitIgnore(outputDir, fileName)
if err != nil {
return err
}
fmt.Printf("generated .gitignore file in %s/.gitignore\n", outputDir)
return nil
}
func createGitIgnore(path, line string) error {
ignorefPath := path + "/.gitignore"
file, err := os.OpenFile(ignorefPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
ignoreObject, err := ignore.CompileIgnoreFile(ignorefPath)
if err != nil {
file.Close()
return err
}
if !ignoreObject.MatchesPath(line) {
if _, err := file.Write([]byte(line + "\n")); err != nil {
file.Close()
return err
}
if err := file.Close(); err != nil {
return err
}
return nil
}
return nil
}