-
Notifications
You must be signed in to change notification settings - Fork 277
/
main.go
106 lines (85 loc) · 2.67 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/mitchellh/multistep"
"github.com/pearkes/gethub/steps"
)
const versionString = "gethub 0.1.3"
func main() {
// Debugging and version flags
debug := flag.Bool("debug", false, "Logs debugging information to STDOUT.")
version := flag.Bool("version", false, "Prints the version and exits.")
flag.BoolVar(debug, "d", false, "Logs debugging information to STDOUT.")
flag.BoolVar(version, "v", false, "Prints the version and exits.")
// Override the flag.Usage function to print custom usage info.
flag.Usage = usage
flag.Parse()
arg := flag.Arg(0)
// Discard logging if debug is turned off.
if *debug == false {
log.SetOutput(ioutil.Discard)
}
// Print the version and exit
if *version {
fmt.Println(versionString)
os.Exit(1)
}
// Log enabled debugging
log.Println("Debugging enabled for", versionString)
state := new(multistep.BasicStateBag)
state.Put("debug", *debug)
state.Put("config_path", os.Getenv("GETCONFIG_PATH"))
if arg == "authorize" {
authorizeRunner(state)
} else if arg == "" {
updateRunner(state)
} else {
fmt.Println("Invalid argument: " + arg)
// Prints the usage and exits
usage()
}
}
// Builds the steps and kicks off the runner for updating
// repositories.
func updateRunner(state multistep.StateBag) {
steps := []multistep.Step{
&steps.StepCheckConfigurationFile{},
&steps.StepInjectConfiguration{},
&steps.StepCheckPath{},
&steps.StepCheckConfiguration{},
&steps.StepRetrieveRepositories{},
&steps.StepUpdateRepositories{},
}
runner := &multistep.BasicRunner{Steps: steps}
runner.Run(state)
}
// Builds the steps and kicks off the runner for authorizing
// and creating configuration.
func authorizeRunner(state multistep.StateBag) {
steps := []multistep.Step{
&steps.StepAuthorizeGithub{},
&steps.StepCreateConfiguration{},
&steps.StepCheckConfigurationFile{},
&steps.StepCheckConfiguration{},
}
runner := &multistep.BasicRunner{Steps: steps}
runner.Run(state)
}
// usage prints out the package help
func usage() {
fmt.Println(`Usage: gethub [-v] [-h] [-d] [<path>]
-v, --version Prints the version and exits.
-h, --help Prints the usage information.
-d, --debug Logs debugging information to STDOUT.
Arguments:
path The path to place or update the
repostories. Defaults to the path
in ~/.gethubconfig. This is required
the first time you run gethub.
To learn more or to contribute, please see github.com/pearkes/gethub`)
os.Exit(1)
}