Skip to content

Commit 91bbf72

Browse files
committed
Refactor things to be clean and private
1 parent a7902f6 commit 91bbf72

8 files changed

Lines changed: 132 additions & 113 deletions

File tree

bonvoy.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,8 @@ import (
66
"os"
77
)
88

9-
//func root(args[] string) error {
10-
// var cmds = commands.All()
11-
// subcommand := os.Args[1]
12-
//
13-
// for _, cmd := range cmds {
14-
// if cmd.Name() == subcommand {
15-
// var err = cmd.Init(os.Args[2:])
16-
// if err != nil {
17-
// panic(err)
18-
// }
19-
// return cmd.Run()
20-
// }
21-
// }
22-
// return fmt.Errorf("Unknown subcommand: %s", subcommand)
23-
//}
24-
259
func main() {
2610
_ = os.Setenv("DOCKER_API_VERSION", "1.39")
2711
config.Load()
28-
commands.Init()
29-
//if len(os.Args) < 2 {
30-
// fmt.Println("USAGE: bonvoy" +
31-
// "\n\tversion - Display bonvoy version" +
32-
// "\n\tlog-level [service] (level) - Set log level for a sidecar" +
33-
// "\n\tcerts-expired - Display expired certificates compared to Consul agent for a sidecar" +
34-
// "\n\tlisteners [service] - Display all listeners for sidecar")
35-
//} else if err := root(os.Args[1:]); err != nil {
36-
// fmt.Println(err)
37-
// os.Exit(1)
38-
//}
12+
commands.NewRegistry().Init()
3913
}

commands/certificates.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,40 @@ import (
88
"strings"
99
)
1010

11-
type ExpiredCertificatesController struct {
12-
ServiceName string
13-
Consul consul.Client
11+
type Certificates struct {
12+
Command *cobra.Command
13+
}
14+
15+
func (r *Registry) Certificates() *Certificates {
16+
cmd := &cobra.Command{
17+
Use: "certificates",
18+
Short: "Certificates-related commands",
19+
}
20+
cmd.AddCommand(r.BuildExpiredCertificatesCommand())
21+
return &Certificates{
22+
Command: cmd,
23+
}
1424
}
1525

16-
func BuildExpiredCertificatesController(rootCmd *cobra.Command) {
17-
rootCmd.AddCommand(&cobra.Command{
18-
Use: "certificates expired",
26+
func (r *Registry) BuildExpiredCertificatesCommand() *cobra.Command {
27+
return &cobra.Command{
28+
Use: "expired",
1929
Short: "Show all expired certificates",
2030
Long: `Display all expired sidecar certificates as compared to the local Consul agent`,
21-
Args: cobra.MinimumNArgs(2),
31+
Args: cobra.MinimumNArgs(1),
2232
RunE: func(cmd *cobra.Command, args []string) error {
2333
controller := ExpiredCertificatesController{
24-
ServiceName: args[1],
34+
ServiceName: args[0],
2535
Consul: consul.NewClient(),
2636
}
2737
return controller.Run()
2838
},
29-
})
39+
}
40+
}
41+
42+
type ExpiredCertificatesController struct {
43+
ServiceName string
44+
Consul consul.Client
3045
}
3146

3247
func (c *ExpiredCertificatesController) Run() error {

commands/listeners.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ package commands
22

33
import (
44
"bonvoy/envoy"
5-
"flag"
65
"fmt"
76
"github.com/spf13/cobra"
87
)
98

10-
type ListenersController struct {
11-
ServiceName string
9+
type Listeners struct {
10+
Command *cobra.Command
1211
}
1312

14-
func BuildListenersCommand(rootCmd *cobra.Command) {
15-
rootCmd.AddCommand(&cobra.Command{
16-
Use: "listeners",
17-
Short: "Show Envoy listeners",
18-
Long: `Display all registered Envoy sidecar listeners`,
19-
Args: cobra.MinimumNArgs(2),
20-
RunE: func(cmd *cobra.Command, args []string) error {
21-
controller := ListenersController{
22-
ServiceName: args[1],
23-
}
24-
return controller.Run()
13+
func (r *Registry) Listeners() *Listeners {
14+
return &Listeners{
15+
Command: &cobra.Command{
16+
Use: "listeners",
17+
Short: "Show Envoy listeners",
18+
Long: `Display all registered Envoy sidecar listeners`,
19+
Args: cobra.MinimumNArgs(1),
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
controller := ListenersController{
22+
ServiceName: args[0],
23+
}
24+
return controller.Run()
25+
},
2526
},
26-
})
27+
}
28+
}
29+
30+
type ListenersController struct {
31+
ServiceName string
2732
}
2833

2934
func (s *ListenersController) Run() error {
@@ -38,8 +43,4 @@ func (s *ListenersController) Run() error {
3843
fmt.Printf("%s: %s\n", listener.Name, listener.TargetAddress)
3944
}
4045
return nil
41-
}
42-
type ListenersCommand struct {
43-
fs *flag.FlagSet
44-
name string
4546
}

commands/logging.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,57 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10-
var (
11-
logLevel string
12-
)
10+
type Logging struct {
11+
Command *cobra.Command
12+
}
1313

14-
type SetLogLevelController struct {
15-
ServiceName string
14+
func (r *Registry) Logging() *Logging {
15+
cmd := &cobra.Command{
16+
Use: "log",
17+
Short: "Logging related commands",
18+
}
19+
cmd.AddCommand(r.BuildSetLogLevelCommand())
20+
return &Logging{
21+
Command: cmd,
22+
}
1623
}
1724

18-
func BuildSetLogLevelCommand(rootCmd *cobra.Command) {
25+
// log level
26+
27+
var (
28+
desiredLogLevel string
29+
)
30+
31+
func (r *Registry) BuildSetLogLevelCommand() *cobra.Command {
1932
cmd := &cobra.Command{
20-
Use: "log level",
33+
Use: "level",
2134
Short: "Set Envoy log level",
2235
Long: `Set the Envoy sidecar log level`,
23-
Args: cobra.MinimumNArgs(2),
36+
Args: cobra.MinimumNArgs(1),
2437
RunE: func(cmd *cobra.Command, args []string) error {
2538
controller := SetLogLevelController{
26-
ServiceName: args[1],
39+
ServiceName: args[0],
2740
}
2841
return controller.Run()
2942
},
3043
}
31-
cmd.Flags().StringVarP(&logLevel, "level", "l", "", "Desired log level (debug/info/warning/error")
32-
rootCmd.AddCommand(cmd)
44+
cmd.Flags().StringVarP(&desiredLogLevel, "level", "l", "", "Desired log level (debug/info/warning/error")
45+
return cmd
46+
}
47+
48+
type SetLogLevelController struct {
49+
ServiceName string
3350
}
3451

3552
func (c *SetLogLevelController) Run() error {
36-
if logLevel == "" {
37-
logLevel = c.SelectLogLevel()
53+
if desiredLogLevel == "" {
54+
desiredLogLevel = c.SelectLogLevel()
3855
}
3956
e, err := envoy.NewFromServiceName(c.ServiceName)
4057
if err != nil {
4158
return err
4259
}
43-
e.Logging().SetLevel(logLevel)
60+
e.Logging().SetLevel(desiredLogLevel)
4461
return nil
4562
}
4663

commands/registry.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,30 @@ import (
66
"os"
77
)
88

9+
type Registry struct {}
10+
11+
func NewRegistry() *Registry {
12+
return &Registry{}
13+
}
14+
915
var rootCmd = &cobra.Command{
1016
Use: "bonvoy",
1117
Short: "Envoy and Consul Connect interaction",
12-
Long: ``,
13-
Run: func(cmd *cobra.Command, args []string) {
14-
15-
},
1618
}
1719

18-
func Init() {
19-
BuildServerInfoCommand(rootCmd)
20-
BuildSetLogLevelCommand(rootCmd)
21-
BuildListenersCommand(rootCmd)
22-
BuildExpiredCertificatesController(rootCmd)
23-
BuildVersionCommand(rootCmd)
20+
func (r *Registry) Init() {
21+
r.RegisterCommands()
2422
if err := rootCmd.Execute(); err != nil {
2523
_, _ = fmt.Fprintln(os.Stderr, err)
2624
os.Exit(1)
2725
}
2826
}
2927

30-
//type Runner interface {
31-
// Init([]string) error
32-
// Run() error
33-
// Name() string
34-
//}
35-
//
36-
//func All() []Runner {
37-
// return []Runner{
38-
// BuildListeners(),
39-
// BuildVersion(),
40-
// BuildExpiredCertificatesCommand(),
41-
// BuildSetLogLevelCommand(),
42-
// BuildServerInfoCommand(),
43-
// }
44-
//}
28+
func (r *Registry) RegisterCommands() {
29+
// Root level subcommands/commands
30+
rootCmd.AddCommand(r.Server().Command)
31+
rootCmd.AddCommand(r.Logging().Command)
32+
rootCmd.AddCommand(r.Listeners().Command)
33+
rootCmd.AddCommand(r.Certificates().Command)
34+
rootCmd.AddCommand(r.Version().Command)
35+
}
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,38 @@ import (
88
"os"
99
)
1010

11-
type ServerInfoController struct {
12-
ServiceName string
11+
type Server struct {
12+
Command *cobra.Command
13+
}
14+
15+
func (r *Registry) Server() *Server {
16+
cmd := &cobra.Command{
17+
Use: "server",
18+
Short: "Envoy server commands",
19+
}
20+
cmd.AddCommand(r.BuildServerInfoCommand())
21+
return &Server{
22+
Command: cmd,
23+
}
1324
}
1425

15-
func BuildServerInfoCommand(rootCmd *cobra.Command) {
16-
rootCmd.AddCommand(&cobra.Command{
17-
Use: "server info",
18-
Short: "Envoy server information",
26+
func (r *Registry) BuildServerInfoCommand() *cobra.Command {
27+
return &cobra.Command{
28+
Use: "info",
29+
Short: "Display envoy server information",
1930
Long: `Display server information about the envoy sidecar`,
20-
Args: cobra.MinimumNArgs(2),
31+
Args: cobra.MinimumNArgs(1),
2132
RunE: func(cmd *cobra.Command, args []string) error {
2233
controller := ServerInfoController{
23-
ServiceName: args[1],
34+
ServiceName: args[0],
2435
}
2536
return controller.Run()
2637
},
27-
})
38+
}
39+
}
40+
41+
type ServerInfoController struct {
42+
ServiceName string
2843
}
2944

3045
func (s *ServerInfoController) Run() error {

commands/version.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
func BuildVersionCommand(rootCmd *cobra.Command) {
10-
rootCmd.AddCommand(&cobra.Command{
11-
Use: "version",
12-
Short: "Display Bonvoy version",
13-
Long: `Display the Bonvoy CLI version`,
14-
RunE: func(cmd *cobra.Command, args []string) error {
15-
fmt.Println(version.Version)
16-
return nil
9+
type Version struct {
10+
Command *cobra.Command
11+
}
12+
13+
func (r *Registry) Version() *Version {
14+
return &Version{
15+
Command: &cobra.Command{
16+
Use: "version",
17+
Short: "Display Bonvoy version",
18+
Long: `Display the Bonvoy CLI version`,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
fmt.Println(version.Version)
21+
return nil
22+
},
1723
},
18-
})
24+
}
1925
}

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var GitCommit string
55
var GitDescribe string
66

77
// The main version number that is being run at the moment.
8-
const Version = "0.0.1"
8+
const Version = "0.0.2"
99

1010
// A pre-release marker for the version. If this is "" (empty string)
1111
// then it means that it is a final release. Otherwise, this is a pre-release

0 commit comments

Comments
 (0)