-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (107 loc) · 3.11 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
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const defaultConfigFilename = "config"
var (
logger *zerolog.Logger
// BuildID is the git commit hash of the build provided at compile time.
BuildID string
// BuildTime is the timestamp of the build provided at compile time.
BuildTime string
// BuildVersion is the version string provided at compile time.
BuildVersion string
// dir - target directory to write git star repos to
dir string
// token is the github user access token used for pulling user attributes
// (in this case repositories starred)
token string
// debug bool is used to determine if debug logging should be used.
debug bool
)
// List of command variables
var (
rootCmd = &cobra.Command{
Use: "starchive",
Short: "tool to archive github user starred repositories",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return initializeConfig(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
if debug {
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger().Level(zerolog.DebugLevel)
logger = &log
}
if token == "" {
fmt.Print("Enter Github User Access Token: ")
reader := bufio.NewReader(os.Stdin)
token, _ := reader.ReadString('\n')
token = strings.Replace(token, "\n", "", -1)
}
starchive := &Starchive{
logger: logger,
Dir: dir,
Repos: []Repo{},
Token: token,
}
starchive.Run()
},
}
versionCmd = &cobra.Command{
Use: "version",
Short: "version of the starchive binary",
Run: func(cmd *cobra.Command, args []string) {
build, err := cmd.Flags().GetBool("build")
if err != nil {
logger.Warn().Err(err).Msg("error parsing build flag for version command")
}
fmt.Printf("Version:\t%s\n", BuildVersion)
if build {
fmt.Printf("BuildID:\t%s\n", BuildID)
fmt.Printf("BuildTime:\t%s\n", BuildTime)
}
os.Exit(0)
},
}
)
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})
}
func initializeConfig(cmd *cobra.Command) error {
v := viper.New()
v.SetConfigName(defaultConfigFilename)
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
bindFlags(cmd, v)
return nil
}
func init() {
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger().Level(zerolog.WarnLevel)
logger = &log
rootCmd.Flags().StringVarP(&dir, "dir", "d", "./", "target directory for archiving repositories")
rootCmd.Flags().StringVarP(&token, "token", "t", "", "github user access token")
rootCmd.Flags().BoolVar(&debug, "debug", false, "debug level logging output")
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().Bool("build", false, "displays all the build information for starchive")
}
func main() {
if err := rootCmd.Execute(); err != nil {
logger.Fatal().Err(err)
}
}