-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (64 loc) · 1.23 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
defaultConfigName = "default"
saveDirName = "git-sw"
)
var (
userHomeDir, saveDirPath string
profiles []Profile
)
func main() {
var err error
parseFlag()
if len(os.Args) == 1 {
flag.Usage()
return
}
cmd := flag.Arg(0)
action := getAction(strings.ToLower(cmd))
if !action.IsValid() {
fmt.Println(formatError(fmt.Errorf("invalid command = %s", cmd)))
flag.Usage()
os.Exit(1)
}
if _, ok := allowedGlobal[action]; isGlobal && !ok {
errorAndExit(errors.New("flag -g can only be used with the 'use', 'edit', and 'delete' commands"))
}
userHomeDir, err = os.UserHomeDir()
if err != nil {
errorAndExit(err)
}
userConfigDir, err := os.UserConfigDir()
if err != nil {
errorAndExit(err)
}
saveDirPath = filepath.Join(userConfigDir, saveDirName)
err = os.MkdirAll(saveDirPath, 0o744)
if err != nil {
errorAndExit(err)
}
err = copyDefault()
if err != nil {
errorAndExit(err)
}
profiles, err = getProfiles(saveDirPath)
if err != nil {
errorAndExit(err)
}
command, ok := commands[action]
if !ok {
errorAndExit(ErrNotImplemented)
}
err = command.Func()
if err != nil {
errorAndExit(err)
}
}