Skip to content

Commit

Permalink
Hack cmd/agents to nest thrid level command only once
Browse files Browse the repository at this point in the history
  • Loading branch information
iknite committed Feb 19, 2019
1 parent 13993a1 commit d0a9444
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
44 changes: 31 additions & 13 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package cmd

import (
"regexp"

"github.com/spf13/cobra"
v "github.com/spf13/viper"

"github.com/bbva/qed/gossip"
)

func newAgentCommand(cmdCtx *cmdContext) *cobra.Command {
func newAgentCommand(cmdCtx cmdContext, args []string) *cobra.Command {

config := gossip.DefaultConfig()

Expand All @@ -39,7 +41,14 @@ func newAgentCommand(cmdCtx *cmdContext) *cobra.Command {
f.StringSliceVar(&config.StartJoin, "join", []string{}, "Comma-delimited list of nodes ([host]:port), through which a cluster can be joined")
f.StringSliceVar(&config.AlertsUrls, "alertsUrls", []string{}, "Comma-delimited list of Alert servers ([host]:port), through which an agent can post alerts")

agentPreRun := func(cmd *cobra.Command, args []string) {
// Lookups
v.BindPFlag("agent.node", f.Lookup("node"))
v.BindPFlag("agent.bind", f.Lookup("bind"))
v.BindPFlag("agent.advertise", f.Lookup("advertise"))
v.BindPFlag("agent.join", f.Lookup("join"))
v.BindPFlag("agent.alert_urls", f.Lookup("alertsUrls"))

agentPreRun := func(config gossip.Config) gossip.Config {
config.EnableCompression = true
config.NodeName = v.GetString("agent.node")
config.BindAddr = v.GetString("agent.bind")
Expand All @@ -51,20 +60,29 @@ func newAgentCommand(cmdCtx *cmdContext) *cobra.Command {
markStringRequired(config.BindAddr, "bind")
markSliceStringRequired(config.StartJoin, "join")
markSliceStringRequired(config.AlertsUrls, "alertsUrls")

return config
}

cmd.AddCommand(
newAgentMonitorCommand(cmdCtx, config, agentPreRun),
newAgentAuditorCommand(cmdCtx, config, agentPreRun),
newAgentPublisherCommand(cmdCtx, config, agentPreRun),
)
var kind string
re := regexp.MustCompile("^monitor$|^auditor$|^publisher$")
for _, arg := range args {
if re.MatchString(arg) {
kind = arg
break
}
}

// Lookups
v.BindPFlag("agent.node", f.Lookup("node"))
v.BindPFlag("agent.bind", f.Lookup("bind"))
v.BindPFlag("agent.advertise", f.Lookup("advertise"))
v.BindPFlag("agent.join", f.Lookup("join"))
v.BindPFlag("agent.alert_urls", f.Lookup("alertsUrls"))
switch kind {
case "publisher":
cmd.AddCommand(newAgentPublisherCommand(cmdCtx, *config, agentPreRun))

case "auditor":
cmd.AddCommand(newAgentAuditorCommand(cmdCtx, *config, agentPreRun))

case "monitor":
cmd.AddCommand(newAgentMonitorCommand(cmdCtx, *config, agentPreRun))
}

return cmd

Expand Down
6 changes: 3 additions & 3 deletions cmd/agent_auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/bbva/qed/util"
)

func newAgentAuditorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun func(*cobra.Command, []string)) *cobra.Command {
func newAgentAuditorCommand(ctx cmdContext, config gossip.Config, agentPreRun func(gossip.Config) gossip.Config) *cobra.Command {

auditorConfig := auditor.DefaultConfig()

Expand All @@ -38,7 +38,7 @@ func newAgentAuditorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun

// WARN: PersitentPreRun can't be nested and we're using it in cmd/root so inbetween preRuns
// must be curried.
agentPreRun(cmd, args)
config = agentPreRun(config)

// Bindings
auditorConfig.QEDUrls = v.GetStringSlice("agent.server_urls")
Expand All @@ -57,7 +57,7 @@ func newAgentAuditorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun
log.Fatalf("Failed to start the QED monitor: %v", err)
}

agent, err := gossip.NewAgent(config, []gossip.Processor{auditor})
agent, err := gossip.NewAgent(&config, []gossip.Processor{auditor})
if err != nil {
log.Fatalf("Failed to start the QED auditor: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/agent_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/bbva/qed/util"
)

func newAgentMonitorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun func(*cobra.Command, []string)) *cobra.Command {
func newAgentMonitorCommand(ctx cmdContext, config gossip.Config, agentPreRun func(gossip.Config) gossip.Config) *cobra.Command {

monitorConfig := monitor.DefaultConfig()

Expand All @@ -38,7 +38,7 @@ func newAgentMonitorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun

// WARN: PersitentPreRun can't be nested and we're using it in cmd/root so inbetween preRuns
// must be curried.
agentPreRun(cmd, args)
config = agentPreRun(config)

// Bindings
monitorConfig.QEDUrls = v.GetStringSlice("agent.server_urls")
Expand All @@ -57,7 +57,7 @@ func newAgentMonitorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun
log.Fatalf("Failed to start the QED monitor: %v", err)
}

agent, err := gossip.NewAgent(config, []gossip.Processor{monitor})
agent, err := gossip.NewAgent(&config, []gossip.Processor{monitor})
if err != nil {
log.Fatalf("Failed to start the QED monitor: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/agent_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/bbva/qed/util"
)

func newAgentPublisherCommand(ctx *cmdContext, config *gossip.Config, agentPreRun func(*cobra.Command, []string)) *cobra.Command {
func newAgentPublisherCommand(ctx cmdContext, config gossip.Config, agentPreRun func(gossip.Config) gossip.Config) *cobra.Command {

var endpoints []string

Expand All @@ -38,7 +38,7 @@ func newAgentPublisherCommand(ctx *cmdContext, config *gossip.Config, agentPreRu

// WARN: PersitentPreRun can't be nested and we're using it in
// cmd/root so inbetween preRuns must be curried.
agentPreRun(cmd, args)
config = agentPreRun(config)

// Bindings
endpoints = v.GetStringSlice("agent.snapshots_store_urls")
Expand All @@ -55,7 +55,7 @@ func newAgentPublisherCommand(ctx *cmdContext, config *gossip.Config, agentPreRu
log.Fatalf("Failed to start the QED publisher: %v", err)
}

agent, err := gossip.NewAgent(config, []gossip.Processor{publisher})
agent, err := gossip.NewAgent(&config, []gossip.Processor{publisher})
if err != nil {
log.Fatalf("Failed to start the QED publisher: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
)

// NewRootCommand is the main Parser for the qed cli.
func NewRootCommand() *cobra.Command {
func NewRootCommand(args []string) *cobra.Command {
ctx := &cmdContext{}

cmd := &cobra.Command{
Use: "qed",
Short: "QED is a client for the verifiable log server",
TraverseChildren: true,
Use: "qed",
Short: "QED is a client for the verifiable log server",
// TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {

if ctx.configFile != "" {
Expand Down Expand Up @@ -83,7 +83,7 @@ func NewRootCommand() *cobra.Command {
cmd.AddCommand(
newStartCommand(ctx),
newClientCommand(ctx),
newAgentCommand(ctx),
newAgentCommand(*ctx, args),
)

return cmd
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

func main() {
if err := cmd.NewRootCommand().Execute(); err != nil {
rootCmd := cmd.NewRootCommand(os.Args[1:])
if err := rootCmd.Execute(); err != nil {
os.Exit(-1)
}
}

0 comments on commit d0a9444

Please sign in to comment.