This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
/
state.go
150 lines (120 loc) · 3 KB
/
state.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package state
import (
"context"
"errors"
"flag"
"fmt"
"strings"
"time"
"github.com/aphistic/gomol"
"github.com/demosdemon/golang-app-framework/app"
)
var (
// ErrActionRequired is returned when no command action is provided
ErrActionRequired = errors.New("need an action")
// ErrInvalidRepo is returned if the repo provided on the command line does not look like <owner>/<name>.
ErrInvalidRepo = errors.New("invalid repo")
)
// The State of the application.
type State struct {
*app.App
// command-line flags
debug bool
repo string
timeout time.Duration
action string
templates []string
}
func (s *State) ParseArguments() error {
fs := flag.NewFlagSet("update-gitignore", flag.ContinueOnError)
fs.SetOutput(s.Stderr)
fs.Usage = usage(fs)
debug := fs.Bool("debug", false, "print debug statements to STDERR")
repo := fs.String("repo", "github/gitignore", "the template repository to use")
timeout := fs.Duration("timeout", time.Second*30, "the max duration for network requests (0 for no timeout)")
if err := fs.Parse(s.Arguments); err != nil {
return err
}
s.SetDebug(*debug)
s.SetRepo(*repo)
s.SetTimeout(*timeout)
args := fs.Args()
if len(args) == 0 {
fs.Usage()
return ErrActionRequired
}
s.action = args[0]
s.templates = args[1:]
return nil
}
func (s *State) SetDebug(debug bool) {
logger := s.Logger()
s.debug = debug
if s.debug {
logger.SetLogLevel(gomol.LevelDebug)
} else {
logger.SetLogLevel(gomol.LevelInfo)
}
}
func (s *State) Debug() bool {
return s.debug
}
func (s *State) SetRepo(repo string) {
s.repo = repo
}
func (s *State) Repo() string {
return s.repo
}
func (s *State) SetTimeout(timeout time.Duration) {
if timeout < 0 {
timeout = 0
}
s.timeout = timeout
}
func (s *State) Timeout() time.Duration {
return s.timeout
}
func (s *State) Command() (Command, error) {
switch s.action {
case "dump":
return (*dumpCommand)(s), nil
case "list":
return (*listCommand)(s), nil
default:
return nil, fmt.Errorf("unrecognized action %s", s.action)
}
}
func (s *State) Client() (*Client, error) {
slice := strings.SplitN(s.repo, "/", 2)
if len(slice) != 2 {
return nil, ErrInvalidRepo
}
cl := &Client{
state: s,
owner: slice[0],
repo: slice[1],
}
cl.SetHTTPClient(nil)
return cl, nil
}
func (s *State) deadline() (context.Context, context.CancelFunc) {
if s.timeout > 0 {
return context.WithTimeout(s.Context, s.timeout)
}
return s.Context, func() {}
}
func usage(flagset *flag.FlagSet) func() {
return func() {
fmt.Fprintln(flagset.Output(), `usage: update-gitignore [{flags}] {action} [{template}...]
Actions:
dump - dumps the selected template(s) to STDOUT
list - lists the available templates, optionally filtered by the provided arguments
{flags} - Command line flags (see below)
{template} - The Template to dump (required for "dump") or a search string to filter (optional for "list")
Examples:
update-gitignore list go
update-gitignore -debug dump Go > .gitignore
Flags:`)
flagset.PrintDefaults()
}
}