-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
41 lines (34 loc) · 903 Bytes
/
common.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
package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/src-d/go-git.v4"
)
// CheckIfError should be used to naively panics if an error is not nil.
func checkIfError(err error) {
if err == nil {
return
}
throwError(err.Error())
}
// Helper function to write byte array to file on disk
func writeFile(contents []byte, filename string) {
err := ioutil.WriteFile(filename, contents, 0644)
checkIfError(err)
}
func throwError(msg string) {
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", msg))
os.Exit(1)
}
// Warning should be used to display a warning
func warning(format string, args ...interface{}) {
fmt.Printf("\x1b[36;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}
// Returns a go-git repo object should a repository
// exist at the specified filepath.
func openRepo(path string) *git.Repository {
repo, err := git.PlainOpen(path)
checkIfError(err)
return repo
}