github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/bob/gitignore.go (about) 1 package bob 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/Benchkram/errz" 11 ) 12 13 const gitignore = ".gitignore" 14 15 // func (b *B) gitignoreRemove(name string) (err error) { 16 // defer errz.Recover(&err) 17 18 // file, err := os.Open(gitignore) 19 // errz.Fatal(err) 20 // defer file.Close() 21 22 // scanner := bufio.NewScanner(file) 23 // // optionally, resize scanner's capacity for lines over 64K, see next example 24 // for scanner.Scan() { 25 // fmt.Println(scanner.Text()) 26 // } 27 28 // return nil 29 // } 30 31 // gitignoreAdd a dir to the end of the .gitignore file. 32 func (b *B) gitignoreAdd(dir string) (err error) { 33 defer errz.Recover(&err) 34 35 if !strings.HasSuffix(dir, "/") { 36 dir = dir + "/" 37 } 38 39 file, err := os.OpenFile(filepath.Join(b.dir, gitignore), os.O_RDWR|os.O_CREATE, 0664) 40 errz.Fatal(err) 41 defer file.Close() 42 43 scanner := bufio.NewScanner(file) 44 for scanner.Scan() { 45 text := scanner.Text() 46 if text == dir { 47 // Already on ignore list, no need to do anything 48 return nil 49 } 50 } 51 52 w := bufio.NewWriter(file) 53 fmt.Fprintln(w, "") // Make sure to write to a new line 54 fmt.Fprint(w, dir) 55 w.Flush() 56 57 return nil 58 }