Skip to content

Commit

Permalink
chore: refactor code to manage flags like kubectl #13
Browse files Browse the repository at this point in the history
  • Loading branch information
SLedunois committed Mar 7, 2022
1 parent ef56a63 commit b3b5b7a
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 45 deletions.
35 changes: 19 additions & 16 deletions pkg/cmd/instances/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@ import (
"github.com/spf13/cobra"
)

var url string
var secret string
type AddCmd struct {
Command *cobra.Command
Flags *AddFlags
}

// NewAddCmd return the instances add subcommand
func NewAddCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Add a BigBlueButton instance",
Long: `Add a BigBlueButton instance in your B3LB cluster`,
RunE: add,
cmd := &AddCmd{
Command: &cobra.Command{
Use: "add",
Short: "Add a BigBlueButton instance",
Long: `Add a BigBlueButton instance in your B3LB cluster`,
},
Flags: NewAddFlags(),
}

cmd.Flags().StringVarP(&url, "url", "u", "", "BigBlueButton instance url")
cmd.MarkFlagRequired("url")
cmd.Flags().StringVarP(&secret, "secret", "s", "", "BigBlueButton secret")
cmd.MarkFlagRequired("secret")
cmd.Command.RunE = cmd.process

cmd.ApplyFlags()

return cmd
return cmd.Command
}

func add(cmd *cobra.Command, args []string) error {
if err := admin.API.Add(url, secret); err != nil {
cmd.SilenceUsage = true
func (cmd *AddCmd) process(command *cobra.Command, args []string) error {
if err := admin.API.Add(cmd.Flags.URL, cmd.Flags.Secret); err != nil {
command.SilenceUsage = true
return fmt.Errorf("unable to add BigBlueButton instance to your cluster: %s", err.Error())
}

cmd.Println(fmt.Sprintf(`instance %s added`, url))
command.Println(fmt.Sprintf(`instance %s added`, cmd.Flags.URL))
return nil
}
40 changes: 40 additions & 0 deletions pkg/cmd/instances/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package instances

// ListFlags contains all List command flags
type ListFlags struct {
CSV bool
JSON bool
}

// NewListFlags initialize a new ListFlags object
func NewListFlags() *ListFlags {
return &ListFlags{
CSV: false,
JSON: false,
}
}

// Apply apply ListFlags to provided command
func (cmd *ListCmd) ApplyFlags() {
cmd.Command.Flags().BoolVarP(&cmd.Flags.CSV, "csv", "c", cmd.Flags.CSV, "csv output")
cmd.Command.Flags().BoolVarP(&cmd.Flags.JSON, "json", "j", cmd.Flags.JSON, "json output")
}

// AddFlags contains all Add command flags
type AddFlags struct {
URL string
Secret string
}

// NewAddFlags initialize a new AddFlags object
func NewAddFlags() *AddFlags {
return &AddFlags{}
}

// ApplyFlags apply command flags to cobra command
func (cmd *AddCmd) ApplyFlags() {
cmd.Command.Flags().StringVarP(&cmd.Flags.URL, "url", "u", "", "BigBlueButton instance url")
cmd.Command.MarkFlagRequired("url")
cmd.Command.Flags().StringVarP(&cmd.Flags.Secret, "secret", "s", "", "BigBlueButton secret")
cmd.Command.MarkFlagRequired("secret")
}
39 changes: 22 additions & 17 deletions pkg/cmd/instances/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,49 @@ import (
"github.com/spf13/cobra"
)

var jsonOutput bool
var csvOutput bool
type ListCmd struct {
Command *cobra.Command
Flags *ListFlags
}

// NewListCmd return the instances list subcommand
func NewListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List BigBlueButton instances",
Long: `List all BigBlueButton instances available in your B3LB cluster`,
RunE: list,
cmd := &ListCmd{
Command: &cobra.Command{
Use: "list",
Short: "List BigBlueButton instances",
Long: `List all BigBlueButton instances available in your B3LB cluster`,
},
Flags: NewListFlags(),
}

cmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "json output")
cmd.Flags().BoolVarP(&csvOutput, "csv", "c", false, "csv output")
cmd.Command.RunE = cmd.list

cmd.ApplyFlags()

return cmd
return cmd.Command
}

func list(cmd *cobra.Command, args []string) error {
func (cmd *ListCmd) list(command *cobra.Command, args []string) error {
instances, err := admin.API.List()
if err != nil {
return fmt.Errorf("an error occured when getting remote instances: %s", err.Error())
}

if jsonOutput {
renderJSON(cmd, instances)
} else if csvOutput {
renderTable(cmd, instances, true)
if cmd.Flags.JSON {
renderJSON(command, instances)
} else if cmd.Flags.CSV {
renderTable(command, instances, true)
} else {
renderTable(cmd, instances, false)
renderTable(command, instances, false)
}

return nil
}

func renderTable(cmd *cobra.Command, instances []api.BigBlueButtonInstance, csv bool) {
t := table.NewWriter()
t.AppendHeader(table.Row{"URL", "Secret"})
t.AppendHeader(table.Row{"Url", "Secret"})

for _, instance := range instances {
t.AppendRow(table.Row{instance.URL, instance.Secret})
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/instances/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestListCmd(t *testing.T) {
assert.Nil(t, err)
out, outErr := ioutil.ReadAll(output)
assert.Nil(t, outErr)
expected := fmt.Sprintf(`URL,Secret
expected := fmt.Sprintf(`Url,Secret
%s,%s`, url, secret)
assert.Equal(t, expected, strings.TrimSpace(string(out)))
},
Expand All @@ -91,7 +91,7 @@ func TestListCmd(t *testing.T) {
assert.Nil(t, err)
out, outErr := ioutil.ReadAll(output)
assert.Nil(t, outErr)
expected := fmt.Sprintf(`URL Secret
expected := fmt.Sprintf(`Url Secret
%s %s`, url, secret)
assert.Equal(t, expected, strings.TrimSpace(string(out)))
},
Expand Down
25 changes: 25 additions & 0 deletions pkg/cmd/root/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package root

import (
"fmt"

"github.com/SLedunois/b3lbctl/pkg/config"
"github.com/spf13/viper"
)

// RootFlags contains all root command flags
type RootFlags struct {
ConfigPath string
}

// NewRootFlags initialize root command flags. It returns a RootFlags struct
func NewRootFlags() *RootFlags {
return &RootFlags{}
}

// ApplyFlags apply RootFlags to provided command
func (cmd *RootCmd) ApplyFlags() {
cmd.Command.PersistentFlags().StringVar(&cmd.Flags.ConfigPath, "config", config.DefaultConfigPath(), fmt.Sprintf("config file (default is %s)", config.DefaultConfigPath()))
cmd.Command.MarkFlagRequired("config")
viper.BindPFlag("config", cmd.Command.PersistentFlags().Lookup("config"))
}
26 changes: 16 additions & 10 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/spf13/viper"
)

var configPath string
type RootCmd struct {
Command *cobra.Command
Flags *RootFlags
}

// NewCmd initialize the root command
func NewCmd() *cobra.Command {
Expand All @@ -27,16 +30,19 @@ func NewCmd() *cobra.Command {
}
})

cmd := &cobra.Command{
Use: "b3lbctl <command> [flags]",
Short: "B3LB controller cli",
Long: `Manage your B3LB cluster from the command line`,
Run: func(cmd *cobra.Command, args []string) {},
cmd := &RootCmd{
Command: &cobra.Command{
Use: "b3lbctl <command> [flags]",
Short: "B3LB controller cli",
Long: `Manage your B3LB cluster from the command line`,
Run: func(cmd *cobra.Command, args []string) {},
},
Flags: NewRootFlags(),
}

cmd.PersistentFlags().StringVar(&configPath, "config", config.DefaultConfigPath(), fmt.Sprintf("config file (default is %s)", config.DefaultConfigPath()))
viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
cmd.AddCommand(instances.NewCmd())
cmd.ApplyFlags()

cmd.Command.AddCommand(instances.NewCmd())

return cmd
return cmd.Command
}

0 comments on commit b3b5b7a

Please sign in to comment.