-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (70 loc) · 1.47 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/urfave/cli/v2"
"git-fast-reword/utility"
)
func prettyMap(m map[string]string) (string, error) {
pretty, err := json.MarshalIndent(m, "", " ")
if err != nil {
return "", err
}
return string(pretty), nil
}
func update(cfg map[string]string) error {
newHashes, err := utility.Update(".git", cfg)
if err != nil {
return err
}
pretty, err := prettyMap(newHashes)
if err != nil {
return err
}
fmt.Printf("New hashes:\n%s\n", pretty)
return nil
}
func main() {
app := &cli.App{
Name: "git-fast-reword",
Usage: "fast commits rewording",
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "from-file",
Aliases: []string{"ff"},
Action: func(c *cli.Context) error {
args := c.Args()
if args.Len() < 1 {
return fmt.Errorf("invalid number of arguments")
}
data, err := ioutil.ReadFile(args.Get(0))
if err != nil {
return err
}
var cfg map[string]string
err = json.Unmarshal(data, &cfg)
if err != nil {
return err
}
return update(cfg)
},
},
},
Action: func(c *cli.Context) error {
args := c.Args()
if args.Len() < 2 {
return fmt.Errorf("invalid number of arguments")
}
hash := args.Get(0)
msg := args.Get(1)
return update(map[string]string{hash: msg})
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
}
}