-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
115 lines (98 loc) · 2.18 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"os"
"path"
"github.com/erkkah/git-private/commands"
"github.com/erkkah/git-private/utils"
)
func main() {
args := os.Args
if len(args) < 2 {
usage()
os.Exit(1)
}
err := checkSetup()
if err == nil {
cmd := args[1]
err = runCommand(cmd, os.Args[2:])
}
if err != nil {
fmt.Printf("%s error: %v\n", appName(), err)
os.Exit(1)
}
}
func verifyStateDirIsNotIgnored() error {
stateDir, err := utils.StateDir()
if err != nil {
return err
}
ignored, err := utils.IsGitIgnored(stateDir.Absolute())
if err != nil {
return err
}
if ignored {
return fmt.Errorf("%q is in .gitignore", stateDir)
}
return nil
}
func checkSetup() error {
inTree, err := utils.IsInsideGitTree()
if err != nil {
return err
}
if !inTree {
return fmt.Errorf("not in dir with git repo. Use 'git init' or 'git clone', then in repo use 'git %s init'", utils.ToolName)
}
err = verifyStateDirIsNotIgnored()
if err != nil {
return err
}
return nil
}
func appName() string {
app := path.Base(os.Args[0])
return app
}
func usage() {
fmt.Fprintf(os.Stderr, `Usage:
%[1]s init
%[1]s add <FILE...>
%[1]s remove <FILE...>
%[1]s hide [-keyfile FILE] [-clean] [FILE...]
%[1]s reveal [-keyfile FILE] [-force] [FILE...]
%[1]s keys list [-keyfile FILE]
%[1]s keys add [-keyfile FILE] [-id ID] [-readonly] <-pubfile FILE | public key>
%[1]s keys remove [-keyfile FILE] <-id ID | ID>
%[1]s keys generate -keyfile FILE [-pubfile FILE]
%[1]s clean [-force]
%[1]s status
Example:
$ git-private init
$ git-private add apikey.txt
$ git-private keys add -pubfile ~/.ssh/id_rsa.pub
$ git-private hide -keyfile ~/.ssh/id_rsa
`, appName())
}
func runCommand(cmd string, args []string) error {
cmds := map[string]func([]string, func()) error{
"init": commands.Init,
"add": commands.Add,
"remove": commands.Remove,
"hide": commands.Hide,
"reveal": commands.Reveal,
"keys": commands.Keys,
"clean": commands.Clean,
"status": commands.Status,
"help": help,
}
command, found := cmds[cmd]
if !found {
return fmt.Errorf("command %q not found", cmd)
}
return command(args, usage)
}
func help(_ []string, usage func()) error {
usage()
return nil
}