-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.go
72 lines (64 loc) · 1.58 KB
/
cli.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
package dl
import (
"context"
"errors"
"fmt"
"os"
)
// Although these values will be casted to []byte, they are declared as constants
// because that is't happened frequently.
const (
preCommitScript = `#!/bin/sh
dl clean .
git add .
`
postCommitScript = `#!/bin/sh
dl restore .
`
cleanCmdStr = "dl clean"
restoreCmdStr = "dl restore"
dlDir = ".dl"
msg = "dl %s: The instant logger package for debug.\n"
)
type cmd interface {
Run(ctx context.Context, baseDir string) error
}
// CLI structs.
type CLI struct {
}
// New for running dl package with CLI.
func New() *CLI {
return &CLI{}
}
// Run executes each method for dl package.
func (d *CLI) Run(ctx context.Context, version string, args []string) error {
if len(args) == 0 {
d.usage(version, "")
return errors.New("no command is given.")
}
if len(args) == 1 {
args = append(args, ".")
}
switch args[0] {
case "clean":
return newCleanCmd().Run(ctx, args[1])
case "init":
return newInitCmd().Run(ctx, args[1])
case "remove":
return newRemoveCmd().Run(ctx, args[1])
case "restore":
return newRestoreCmd().Run(ctx, args[1])
default:
return d.usage(version, args[0])
}
}
func (d *CLI) usage(version, invalidCmd string) error {
fmt.Fprintf(os.Stderr, msg+`Usage: dl [command]
Commands:
clean <dir> deletes logs used this package.
init <dir> add dl command into pre-commit script.
remove <dir> remove dl command from pre-commit script.
restore <dir> restore removed logs.
`, version)
return fmt.Errorf("%s is not implemented.", invalidCmd)
}