Skip to content

Commit

Permalink
Merge pull request #38 from tcnksm/fix-flag-with-dash
Browse files Browse the repository at this point in the history
Fix flag with dash
  • Loading branch information
tcnksm committed Oct 13, 2015
2 parents 36c30ae + 6172997 commit bdb6d7a
Show file tree
Hide file tree
Showing 30 changed files with 274 additions and 103 deletions.
22 changes: 6 additions & 16 deletions command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ func (c *ApplyCommand) Run(args []string) int {
return 1
}

// validate executable
if err := executable.Fix(); err != nil {
c.UI.Error(fmt.Sprintf(
"Failed to fix input value: %s", err))
return 1
}

// validate executable
if errs := executable.Validate(); len(errs) > 0 {
c.UI.Error(fmt.Sprintf(
"%q is not valid template file. It has %d errors:", designFile, len(errs)))
Expand Down Expand Up @@ -105,21 +110,6 @@ func (c *ApplyCommand) Run(args []string) int {
return 1
}

// Run fix flag struct. complement empty variable.
if len(executable.Flags) > 0 {
fixedFlags := []skeleton.Flag{}
for _, f := range executable.Flags {
if err := f.Fix(); err != nil {
c.UI.Error(fmt.Sprintf(
"Failed to fix flag struct: %s", err.Error()))
return 1
}
fixedFlags = append(fixedFlags, f)
}

executable.Flags = fixedFlags
}

if len(name) != 0 {
executable.Name = name
output = name
Expand Down
8 changes: 4 additions & 4 deletions command/design.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (c *DesignCommand) Run(args []string) int {
var (
output string
owner string
commands []skeleton.Command
flags []skeleton.Flag
commands []*skeleton.Command
flags []*skeleton.Flag
frameworkStr string
)

Expand Down Expand Up @@ -88,8 +88,8 @@ func (c *DesignCommand) Run(args []string) int {
// If no commands are specified, set emply value so that
// user can understand how to write
if len(commands) < 1 && len(flags) < 1 {
commands = []skeleton.Command{
{
commands = []*skeleton.Command{
&skeleton.Command{
Name: "",
},
}
Expand Down
11 changes: 8 additions & 3 deletions command/flag_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// CommandFlag implements the flag.Value interface and allows multiple
// calls to the same variable to append a list. It parses string and set them
// as skeleton.Command.
type CommandFlag []skeleton.Command
type CommandFlag []*skeleton.Command

// String
func (c *CommandFlag) String() string {
Expand Down Expand Up @@ -41,11 +41,16 @@ func (c *CommandFlag) Set(v string) error {
synopsis = strings.Trim(synopsis, "'")
}

*c = append(*c, skeleton.Command{
command := &skeleton.Command{
Name: name,
Synopsis: synopsis,
})
}

if err := command.Fix(); err != nil {
return err
}

*c = append(*c, command)
}

return nil
Expand Down
26 changes: 15 additions & 11 deletions command/flag_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,36 @@ func TestCommandFlag_Set(t *testing.T) {
{
arg: `add:"Add new task"`,
success: true,
expect: []skeleton.Command{
{Name: "add", Synopsis: "Add new task"},
expect: []*skeleton.Command{
{
Name: "add",
FunctionName: "add",
Synopsis: "Add new task",
},
},
},
{
arg: `add:"Add new task",delete:"Delete task"`,
success: true,
expect: []skeleton.Command{
{Name: "add", Synopsis: "Add new task"},
{Name: "delete", Synopsis: "Delete task"},
expect: []*skeleton.Command{
{Name: "add", FunctionName: "add", Synopsis: "Add new task"},
{Name: "delete", FunctionName: "delete", Synopsis: "Delete task"},
},
},
{
arg: `add,delete,list`,
success: true,
expect: []skeleton.Command{
{Name: "add"},
{Name: "delete"},
{Name: "list"},
expect: []*skeleton.Command{
{Name: "add", FunctionName: "add"},
{Name: "delete", FunctionName: "delete"},
{Name: "list", FunctionName: "list"},
},
},
{
arg: `include:"Include " character inside"`,
success: true,
expect: []skeleton.Command{
{Name: "include", Synopsis: "Include \" character inside"},
expect: []*skeleton.Command{
{Name: "include", FunctionName: "include", Synopsis: "Include \" character inside"},
},
},
}
Expand Down
5 changes: 2 additions & 3 deletions command/flag_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
// FlagFlag implements the flag.Value interface and allows multiple
// calls to the same variable to append a list. It parses string and set them
// as skeleton.Flag.
type FlagFlag []skeleton.Flag
type FlagFlag []*skeleton.Flag

// String
func (f *FlagFlag) String() string {
Expand Down Expand Up @@ -49,10 +49,9 @@ func (f *FlagFlag) Set(v string) error {
// TODO, this should not here..? or extract this as other function
desc = strings.Trim(desc, "\"")
desc = strings.Trim(desc, "'")

}

flag := skeleton.Flag{
flag := &skeleton.Flag{
LongName: name,
TypeString: typeString,
Description: desc,
Expand Down
23 changes: 12 additions & 11 deletions command/flag_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ func TestFlagFlag_Set(t *testing.T) {
{
arg: `debug:string:"Run as debug mode"`,
success: true,
expect: []skeleton.Flag{
expect: []*skeleton.Flag{
{
Name: "debug",
LongName: "debug",
ShortName: "d",
TypeString: skeleton.TypeStringString,
Default: "",
Description: "Run as debug mode",
Name: "debug",
LongName: "debug",
VariableName: "debug",
ShortName: "d",
TypeString: skeleton.TypeStringString,
Default: "",
Description: "Run as debug mode",
},
},
},
{
arg: `debug,help,test`,
success: true,
expect: []skeleton.Flag{
{Name: "debug", LongName: "debug", ShortName: "d", TypeString: skeleton.TypeStringString, Default: ""},
{Name: "help", LongName: "help", ShortName: "h", TypeString: skeleton.TypeStringString, Default: ""},
{Name: "test", LongName: "test", ShortName: "t", TypeString: skeleton.TypeStringString, Default: ""},
expect: []*skeleton.Flag{
{Name: "debug", LongName: "debug", VariableName: "debug", ShortName: "d", TypeString: skeleton.TypeStringString, Default: ""},
{Name: "help", LongName: "help", VariableName: "help", ShortName: "h", TypeString: skeleton.TypeStringString, Default: ""},
{Name: "test", LongName: "test", VariableName: "test", ShortName: "t", TypeString: skeleton.TypeStringString, Default: ""},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions command/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type NewCommand struct {
func (c *NewCommand) Run(args []string) int {

var (
commands []skeleton.Command
flags []skeleton.Flag
commands []*skeleton.Command
flags []*skeleton.Flag
frameworkStr string
owner string
skipTest bool
Expand Down
11 changes: 11 additions & 0 deletions skeleton/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type Command struct {
// Name is command name.
Name string

// FunctionName is name used for function decralation.
// in generating souce code. Name may contain invalid charactor
// like `-` so it holds valid name for it.
FunctionName string

// Flags are flag for the command.
Flags []Flag

Expand All @@ -27,3 +32,9 @@ type Command struct {
// TODO: https://github.com/BurntSushi/toml/pull/90
DebugOutput string `toml:",omitempty"`
}

// Fix fixes user input
func (c *Command) Fix() error {
c.FunctionName = camelCase(c.Name)
return nil
}
47 changes: 47 additions & 0 deletions skeleton/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package skeleton

import "testing"
import "reflect"

func TestCommand_Fix(t *testing.T) {
tests := []struct {
in, exp *Command
success bool
}{
{
in: &Command{
Name: "server-start",
Flags: []Flag{},
},

exp: &Command{
Name: "server-start",
FunctionName: "serverStart",
Flags: []Flag{},
},

success: true,
},
}

for i, tt := range tests {

err := tt.in.Fix()
if err != nil && !tt.success {
continue
}

if err == nil && !tt.success {
t.Fatalf("#%d expect Fix to fail", i)
}

if err != nil {
t.Fatalf("#%d expect Fix not to fail but %q", i, err.Error())
}

if !reflect.DeepEqual(*tt.in, *tt.exp) {
t.Errorf("#%d expect %v to eq %v", i, tt.in, tt.exp)
}
}

}
23 changes: 20 additions & 3 deletions skeleton/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type Executable struct {
Owner string

// Commands are commands of the executable.
Commands []Command
Commands []*Command

// Flags are flags of the executable.
Flags []Flag
Flags []*Flag

// Version is initial version.
Version string
Expand All @@ -45,6 +45,23 @@ func NewExecutable() *Executable {
}
}

// Fix fixes user inputs for using
func (e *Executable) Fix() error {

for _, c := range e.Commands {
if err := c.Fix(); err != nil {
return err
}
}
for _, f := range e.Flags {
if err := f.Fix(); err != nil {
return err
}
}

return nil
}

// Validate validates Executalbe has required field or not.
// If not returns, errors as slice.
func (e *Executable) Validate() (errs []error) {
Expand Down Expand Up @@ -101,7 +118,7 @@ func (e *Executable) Validate() (errs []error) {
func (e *Executable) Overwrite(key string, v interface{}) error {
// Check
switch v.(type) {
case string, []Command, []Flag:
case string, []*Command, []*Flag:
default:
return fmt.Errorf("unexpected value: %#v", v)
}
Expand Down
13 changes: 7 additions & 6 deletions skeleton/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ func TestOverwrite(t *testing.T) {
{
initExecutable: &Executable{
Name: "todo",
Commands: []Command{
Commands: []*Command{
{Name: "add"},
},
},
inputKey: "Commands",
inputValue: []Command{
inputValue: []*Command{
{Name: "list"},
},
success: true,
expt: &Executable{
Name: "todo",
Commands: []Command{
Commands: []*Command{
{Name: "list"},
},
},
},
{
initExecutable: &Executable{
Name: "todo",
Flags: []Flag{
Flags: []*Flag{
{Name: "add"},
},
},
inputKey: "Flags",
inputValue: []Flag{
inputValue: []*Flag{
{Name: "list"},
},
success: true,
expt: &Executable{
Name: "todo",
Flags: []Flag{
Flags: []*Flag{
{Name: "list"},
},
},
Expand All @@ -81,6 +81,7 @@ func TestOverwrite(t *testing.T) {
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(out, tt.expt) {
t.Errorf("#%d expects %#v to be eq %#v", i, out, tt.expt)
}
Expand Down
6 changes: 6 additions & 0 deletions skeleton/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type Flag struct {
// This is generated automatically from LongName
ShortName string

// VariableName is variable name which is usded for
// holding a flag value in generating source code
VariableName string

// TypeString is flag type. This must be provided by user
TypeString string

Expand All @@ -50,6 +54,8 @@ func (f *Flag) Fix() error {
// Name is same as LongName by default
f.Name = f.LongName

f.VariableName = camelCase(f.LongName)

// ShortName is first character of LongName
// TODO, when same first character is provided.
f.ShortName = string(f.LongName[0])
Expand Down
Loading

0 comments on commit bdb6d7a

Please sign in to comment.