-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (83 loc) · 1.86 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"os"
"github.com/mh-cbon/go-repo-utils/repoutils"
"github.com/urfave/cli"
)
var VERSION = "0.0.0"
func main() {
app := cli.NewApp()
app.Name = "commit"
app.Version = VERSION
app.Usage = "Commit file"
app.UsageText = "commit -m <message> -f <file>"
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "file, f",
Value: &cli.StringSlice{},
Usage: "File to add and commit",
},
cli.StringFlag{
Name: "message, m",
Value: "",
Usage: "Message of the commit",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Silently fail",
},
}
app.Action = func(c *cli.Context) error {
files := c.StringSlice("file")
message := c.String("message")
quiet := c.Bool("quiet")
if len(files) == 0 {
cli.ShowAppHelp(c)
return cli.NewExitError("Files are required", 1)
}
if len(message) == 0 {
cli.ShowAppHelp(c)
return cli.NewExitError("Message is required", 1)
}
path, err := os.Getwd()
exitWithError(err)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
vcs, err := repoutils.WhichVcs(path)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
sfile := make([]string, 0)
for _, file := range files {
sfile = append(sfile, string(file))
err = repoutils.Add(vcs, path, string(file))
if err != nil {
if quiet {
// it does not exit on error, just print it
fmt.Printf("Failed to add %s: %s\n", file, err.Error())
} else {
return cli.NewExitError(err.Error(), 1)
}
}
}
err = repoutils.Commit(vcs, path, message, sfile)
if err != nil {
if quiet {
// it does not exit on error, just print it
fmt.Printf("Failed to commit %s\n", err.Error())
} else {
return cli.NewExitError(err.Error(), 1)
}
}
return nil
}
app.Run(os.Args)
}
func exitWithError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}