github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/test/cmd/lfstest-testutils.go (about) 1 // +build testtools 2 3 package main 4 5 import ( 6 "encoding/json" 7 "fmt" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 12 "github.com/git-lfs/git-lfs/test" 13 ) 14 15 type TestUtilRepoCallback struct{} 16 17 func (*TestUtilRepoCallback) Fatalf(format string, args ...interface{}) { 18 fmt.Fprintf(os.Stderr, format, args...) 19 os.Exit(4) 20 } 21 func (*TestUtilRepoCallback) Errorf(format string, args ...interface{}) { 22 fmt.Fprintf(os.Stderr, format, args...) 23 } 24 25 func main() { 26 commandMap := map[string]func(*test.Repo){ 27 "addcommits": AddCommits, 28 } 29 if len(os.Args) < 2 { 30 fmt.Fprintf(os.Stderr, "Command required (e.g. addcommits)\n") 31 os.Exit(2) 32 } 33 34 f, ok := commandMap[os.Args[1]] 35 if !ok { 36 fmt.Fprintf(os.Stderr, "Unknown command: %v\n", os.Args[1]) 37 os.Exit(2) 38 } 39 // Construct test repo context (note: no Cleanup() call since managed outside) 40 // also assume we're in the same folder 41 wd, err := os.Getwd() 42 if err != nil { 43 fmt.Fprintf(os.Stderr, "Problem getting working dir: %v\n", err) 44 os.Exit(2) 45 } 46 // Make sure we're directly inside directory which contains .git 47 // don't want to accidentally end up committing to some other parent git 48 _, err = os.Stat(filepath.Join(wd, ".git")) 49 if err != nil { 50 fmt.Fprintf(os.Stderr, "You're in the wrong directory, should be in root of a test repo: %v\n", err) 51 os.Exit(2) 52 } 53 54 repo := test.WrapRepo(&TestUtilRepoCallback{}, wd) 55 f(repo) 56 } 57 58 func AddCommits(repo *test.Repo) { 59 // Read stdin as JSON []*test.CommitInput 60 in, err := ioutil.ReadAll(os.Stdin) 61 if err != nil { 62 fmt.Fprintf(os.Stderr, "addcommits: Unable to read input data: %v\n", err) 63 os.Exit(3) 64 } 65 inputs := make([]*test.CommitInput, 0) 66 err = json.Unmarshal(in, &inputs) 67 if err != nil { 68 fmt.Fprintf(os.Stderr, "addcommits: Unable to unmarshal JSON: %v\n%v\n", string(in), err) 69 os.Exit(3) 70 } 71 outputs := repo.AddCommits(inputs) 72 73 by, err := json.Marshal(outputs) 74 if err != nil { 75 fmt.Fprintf(os.Stderr, "addcommits: Unable to marshal output JSON: %v\n", err) 76 os.Exit(3) 77 } 78 // Write response to stdout 79 _, err = os.Stdout.Write(by) 80 if err != nil { 81 fmt.Fprintf(os.Stderr, "addcommits: Error writing JSON to stdout: %v\n", err) 82 os.Exit(3) 83 } 84 os.Stdout.WriteString("\n") 85 86 }