-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
75 lines (63 loc) · 1.49 KB
/
root.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
package cmd
import (
"fmt"
"log"
"net/http"
"github.com/go-redis/redis/v8"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/bigkevmcd/go-demo/pkg/demo"
)
func init() {
cobra.OnInitialize(initConfig)
}
func logIfError(e error) {
if e != nil {
log.Fatal(e)
}
}
func makeRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "go-demo",
Short: "Just a demo for GitOps",
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := redis.ParseURL(viper.GetString("redis_url"))
if err != nil {
return err
}
rdb := redis.NewClient(opts)
h := demo.New(demo.Config{Redis: rdb, Key: viper.GetString("redis_key")})
addr := fmt.Sprintf(":%d", viper.GetInt("port"))
log.Printf("listening on %s, connecting to Redis %s\n", addr, opts.Addr)
return http.ListenAndServe(addr, h)
},
}
cmd.Flags().Int(
"port",
8080,
"port to serve requests on",
)
logIfError(viper.BindPFlag("port", cmd.Flags().Lookup("port")))
cmd.Flags().String(
"redis_url",
"redis://localhost:6379",
"url to connect to Redis on",
)
logIfError(viper.BindPFlag("redis_url", cmd.Flags().Lookup("redis_url")))
cmd.Flags().String(
"redis_key",
"demo:value",
"key to fetch from Redis",
)
logIfError(viper.BindPFlag("redis_key", cmd.Flags().Lookup("redis_key")))
return cmd
}
func initConfig() {
viper.AutomaticEnv()
}
// Execute is the main entry point into this component.
func Execute() {
if err := makeRootCmd().Execute(); err != nil {
log.Fatal(err)
}
}