Skip to content

Commit

Permalink
feat: update command builder and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Nov 30, 2023
1 parent cd7b56c commit cb62315
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 36 deletions.
79 changes: 51 additions & 28 deletions cli/common/command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,70 +13,93 @@ func NewDiveCommandBuilder() *diveCommandBuilder {
}

// AddCommand adds a subcommand to the command.
func (d *diveCommandBuilder) AddCommand(cmd *cobra.Command) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddCommand(cmd *cobra.Command) CommandBuilder {
dc.cmd.AddCommand(cmd)
return dc
}

// Add Persistent Bool Flag
func (d *diveCommandBuilder) AddBoolPersistentFlag(p *bool, name string, value bool, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddBoolPersistentFlag(boolV *bool, name string, value bool, usage string) CommandBuilder {
dc.cmd.PersistentFlags().BoolVar(boolV, name, value, usage)
return dc
}

// Add Persistent Bool Flag with Short hand
func (d *diveCommandBuilder) AddBoolPersistentFlagWithShortHand(p *bool, name string, value bool, usage string, shorthand string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddBoolPersistentFlagWithShortHand(boolV *bool, name string, value bool, usage string, shorthand string) CommandBuilder {

dc.cmd.PersistentFlags().BoolVarP(boolV, name, shorthand, value, usage)
return dc
}

// Add Persistent String Flag
func (d *diveCommandBuilder) AddStringPersistentFlag(p *string, name string, value string, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddStringPersistentFlag(stringV *string, name string, value string, usage string) CommandBuilder {
dc.cmd.PersistentFlags().StringVar(stringV, name, value, usage)
return dc
}

// Add Persistent String Flag with Short hand
func (d *diveCommandBuilder) AddStringPersistentFlagWithShortHand(p *string, name string, shorthand string, value string, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddStringPersistentFlagWithShortHand(stringV *string, name string, shorthand string, value string, usage string) CommandBuilder {
dc.cmd.PersistentFlags().StringVarP(stringV, name, shorthand, value, usage)
return dc
}

// Add StringFlag adds a string flag to the command that persists
func (d *diveCommandBuilder) AddStringFlag(name string, value string, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddStringFlag(stringV *string, name string, value string, usage string) CommandBuilder {
dc.cmd.Flags().StringVar(stringV, name, value, usage)
return dc
}

// Add StringFlag adds a string flag to the command that persists with short hand
func (d *diveCommandBuilder) AddStringFlagWithShortHand(p *string, name string, shorthand string, value string, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddStringFlagWithShortHand(stringV *string, name string, shorthand string, value string, usage string) CommandBuilder {
dc.cmd.Flags().StringVarP(stringV, name, shorthand, value, usage)
return dc
}

// Add BooFlag adds a boolean flag to the command that persists
func (d *diveCommandBuilder) AddBoolFlag(name string, value bool, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddBoolFlag(boolV *bool, name string, value bool, usage string) CommandBuilder {
dc.cmd.Flags().BoolVar(boolV, name, value, usage)
return dc
}

func (d *diveCommandBuilder) AddBoolFlagWithShortHand(name string, shorthand string, value bool, usage string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) AddBoolFlagWithShortHand(boolV *bool, name string, shorthand string, value bool, usage string) CommandBuilder {
dc.cmd.Flags().BoolVarP(boolV, name, shorthand, value, usage)
return dc
}

// Build constructs and returns the Cobra command.
func (d *diveCommandBuilder) Build() *cobra.Command {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) Build() *cobra.Command {
dc.cmd.CompletionOptions.DisableDefaultCmd = true
dc.cmd.CompletionOptions.DisableNoDescFlag = true
return dc.cmd
}

// SetUse sets the Use field of the command.
func (d *diveCommandBuilder) SetUse(use string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) SetUse(use string) CommandBuilder {
dc.cmd.Use = use
return dc
}

// SetShort sets the Short field of the command.
func (d *diveCommandBuilder) SetShort(short string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) SetShort(short string) CommandBuilder {
dc.cmd.Short = short
return dc
}

// SetLong sets the Long field of the command.
func (d *diveCommandBuilder) SetLong(long string) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) SetLong(long string) CommandBuilder {
dc.cmd.Long = long
return dc
}

// SetRun sets the Run field of the command.
func (d *diveCommandBuilder) SetRun(run func(cmd *cobra.Command, args []string)) CommandBuilder {
panic("not implemented") // TODO: Implement
func (dc *diveCommandBuilder) SetRun(run func(cmd *cobra.Command, args []string)) CommandBuilder {

dc.cmd.Run = run

return dc
}
func (dc *diveCommandBuilder) ToggleHelpCommand(enable bool) CommandBuilder {

dc.cmd.SetHelpCommand(&cobra.Command{Hidden: enable})
return dc
}
59 changes: 59 additions & 0 deletions cli/common/command_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package common

import (
"bytes"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestDiveCommandBuilder(t *testing.T) {
// Initialize a new DiveCommandBuilder
builder := NewDiveCommandBuilder()

// Set command attributes
builder.SetUse("testCommand").
SetShort("Short description").
SetLong("Long description").
SetRun(func(cmd *cobra.Command, args []string) {
cmd.Println("test function")
})

// Add flags
boolFlag := false
builder.AddBoolFlag(&boolFlag, "boolFlag", false, "Bool flag usage")

stringFlag := ""
builder.AddStringFlag(&stringFlag, "stringFlag", "default", "String flag usage")

// Build the command
cmd := builder.Build()

// Run tests
assert.Equal(t, "testCommand", cmd.Use)
assert.Equal(t, "Short description", cmd.Short)
assert.Equal(t, "Long description", cmd.Long)

// Test flag addition
assert.NotNil(t, cmd.Flags().Lookup("boolFlag"))
assert.NotNil(t, cmd.Flags().Lookup("stringFlag"))

// Test flag default values
assert.False(t, boolFlag)
assert.Equal(t, "default", stringFlag)

// Test the Run function

var outputBuffer bytes.Buffer
cmd.SetOut(&outputBuffer)

// Run the command
cmd.Execute()

// Check the output
expectedOutput := "test function\n"
actualOutput := outputBuffer.String()

assert.Equal(t, expectedOutput, actualOutput)
}
18 changes: 10 additions & 8 deletions cli/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ type CommandBuilder interface {
AddCommand(cmd *cobra.Command) CommandBuilder

// Add Persistent Bool Flag
AddBoolPersistentFlag(p *bool, name string, value bool, usage string) CommandBuilder
AddBoolPersistentFlag(boolV *bool, name string, value bool, usage string) CommandBuilder

// Add Persistent Bool Flag with Short hand
AddBoolPersistentFlagWithShortHand(p *bool, name string, value bool, usage string, shorthand string) CommandBuilder
AddBoolPersistentFlagWithShortHand(boolV *bool, name string, value bool, usage string, shorthand string) CommandBuilder

// Add Persistent String Flag
AddStringPersistentFlag(p *string, name string, value string, usage string) CommandBuilder
AddStringPersistentFlag(stringV *string, name string, value string, usage string) CommandBuilder

// Add Persistent String Flag with Short hand
AddStringPersistentFlagWithShortHand(p *string, name string, shorthand string, value string, usage string) CommandBuilder
AddStringPersistentFlagWithShortHand(stringV *string, name string, shorthand string, value string, usage string) CommandBuilder

// Add StringFlag adds a string flag to the command that persists
AddStringFlag(name string, value string, usage string) CommandBuilder
AddStringFlag(stringV *string, name string, value string, usage string) CommandBuilder

// Add StringFlag adds a string flag to the command that persists with short hand
AddStringFlagWithShortHand(p *string, name string, shorthand string, value string, usage string) CommandBuilder
AddStringFlagWithShortHand(stringV *string, name string, shorthand string, value string, usage string) CommandBuilder

// Add BooFlag adds a boolean flag to the command that persists
AddBoolFlag(name string, value bool, usage string) CommandBuilder
AddBoolFlag(boolV *bool, name string, value bool, usage string) CommandBuilder

AddBoolFlagWithShortHand(name string, shorthand string, value bool, usage string) CommandBuilder
AddBoolFlagWithShortHand(boolV *bool, name string, shorthand string, value bool, usage string) CommandBuilder

// Build constructs and returns the Cobra command.
Build() *cobra.Command
Expand All @@ -93,4 +93,6 @@ type CommandBuilder interface {

// SetRun sets the Run field of the command.
SetRun(run func(cmd *cobra.Command, args []string)) CommandBuilder

ToggleHelpCommand(enable bool) CommandBuilder
}

0 comments on commit cb62315

Please sign in to comment.