-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
100 lines (86 loc) · 2.46 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
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
package main
import (
"context"
"errors"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
rootCmd = &cobra.Command{
Use: "vikingr [options] [command]",
Short: "A Viking that swears to protect master branches.",
Long: `All master branches for private repos in the given organisation will be protected!`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if viper.GetString("user") == "" {
return errors.New("user is required")
}
if viper.GetString("token") == "" {
return errors.New("token is required")
}
if viper.GetString("org") == "" {
return errors.New("org is required")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
gh := getGithubClient(
viper.GetString("user"),
viper.GetString("token"),
)
org := viper.GetString("org")
frequency := viper.GetInt64("frequency")
if frequency == 0 {
return runCheck(ctx, gh, org)
}
ticker := time.NewTicker(time.Duration(frequency) * time.Minute)
quit := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
for {
select {
case <-ticker.C:
if err := runCheck(ctx, gh, org); err != nil {
log.Printf("error(s) occured during protecting phase: %s\n", err)
}
case <-quit:
ticker.Stop()
done <- true
return
}
}
}()
<-done
return nil
},
}
)
func init() {
rootCmd.PersistentFlags().StringP("user", "", "", "user to access GitHub as.")
rootCmd.PersistentFlags().StringP("token", "", "", "token to access GitHub with.")
rootCmd.PersistentFlags().StringP("org", "", "", "organisation to monitor.")
rootCmd.PersistentFlags().Int64P("frequency", "", 0, "frequency to run in minutes")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose logging.")
viper.BindPFlag("user", rootCmd.PersistentFlags().Lookup("user"))
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("org", rootCmd.PersistentFlags().Lookup("org"))
viper.BindPFlag("frequency", rootCmd.PersistentFlags().Lookup("frequency"))
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
viper.SetEnvPrefix("VIKINGR")
viper.AutomaticEnv()
}
func debug(f string, v ...interface{}) {
if viper.GetBool("verbose") {
if len(v) > 0 {
log.Printf(f+"\n", v...)
} else {
log.Println(f)
}
}
}