github.com/lgug2z/story@v0.4.1/git/add.go (about) 1 package git 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 ) 8 9 type AddOpts struct { 10 Files []string 11 Project string 12 } 13 14 func Add(opts AddOpts) (string, error) { 15 var args []string 16 args = append(args, "add") 17 18 for _, file := range opts.Files { 19 args = append(args, file) 20 } 21 22 command := exec.Command("git", args...) 23 if opts.Project != "" { 24 command.Dir = opts.Project 25 } 26 27 combinedOutput, err := command.CombinedOutput() 28 if err != nil { 29 return "", fmt.Errorf("%s: %s", err, combinedOutput) 30 } 31 32 return strings.TrimSpace(string(combinedOutput)), nil 33 }