Skip to content

Commit

Permalink
fix: add excludes params to fix action bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
WindomZ committed Mar 22, 2017
1 parent b20887b commit 94c6799
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 31 deletions.
62 changes: 45 additions & 17 deletions actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package commander

// actor Assigned to execute the command
type actor struct {
names []string // if true keys contain one of names than execute action
musts []string // if true keys contain all of musts than execute action
action Action // execute the command
names []string // the keys contain one of names than execute action
musts []string // the keys contain all of musts than execute action
excludes []string // the keys contain one of excludes than execute action no longer
action Action // executed command
}

// addMustKeys append keys to actor.musts
Expand All @@ -14,6 +15,19 @@ func (a *actor) addMustKeys(keys []string) {
}
}

// addExcludeKeys append keys to actor.excludes
func (a *actor) addExcludeKeys(keys []string) {
if keys != nil && len(keys) != 0 {
for _, key := range keys {
for _, must := range a.musts {
if key != must {
a.excludes = append(a.excludes, key)
}
}
}
}
}

// setAction set executive function to actor.action
// arg is ACTION function, see ./action.go
func (a *actor) setAction(arg interface{}) {
Expand All @@ -32,31 +46,45 @@ func (a *actor) Action(action interface{}, keys ...[]string) {
}

// allow Determine whether meet the requirements(actor.names or actor.musts) for the execution
func (a actor) allow(c Context) bool {
//println(fmt.Sprintf("allow:\n1 %#v\n2 %v\n3 %v",
// a, c.String(), a.action != nil))
func (a actor) allow(c Context) (pass bool) {
//defer func() {
// println(fmt.Sprintf("allow:\n 1 %#v\n 2 %v\n 3 %v\n 4 %v",
// a, c.String(), a.action != nil, pass))
//}()
for _, key := range a.excludes {
if c.Contain(key) {
pass = false
return
}
}

for _, key := range a.names {
if c.Contain(key) {
return true
pass = true
return
}
}

for _, key := range a.musts {
if !c.Contain(key) {
return false
pass = false
return
}
}
return len(a.musts) != 0
pass = len(a.musts) != 0
return
}

// run Common external function, if allow() than execute actor.action
func (a actor) run(c Context) _Result {
//println(fmt.Sprintf("run:\n1 %#v\n2 %v\n3 %v",
// a, c.String(), a.action != nil))
func (a actor) run(c Context) (reuslt _Result) {
//defer func() {
// println(fmt.Sprintf("run:\n 1 %#v\n 2 %v\n 3 %v\n 4 %#v",
// a, c.String(), a.action != nil, reuslt))
//}()
if !a.allow(c) || a.action == nil {
} else if r := a.action(c); r != nil {
//println(fmt.Sprintf("run:\n4 %v\n5 pass", true))
return r
reuslt = nil
} else {
reuslt = a.action(c)
}
//println(fmt.Sprintf("run:\n4 %v\n5 stop", false))
return resultPass
return
}
28 changes: 19 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,27 @@ func (c *_Command) Action(action interface{}, keys ...[]string) Commander {
return c
}

func (c *_Command) addCommand(cmd *_Command) bool {
if cmd.Valid() {
for _, _cmd := range c.commands {
_cmd.addExcludeKeys(cmd.musts)
}
c.commands = append(c.commands, cmd)
return true
} else if c.errFunc != nil {
c.errFunc(errCommand, cmd)
}
return false
}

func (c *_Command) Command(usage string, args ...interface{}) Commander {
if c.clone {
usage = c.usage + " " + usage
} else if c.Valid() {
cmd := newCommand(false)
cmd.Usage(usage, args...)
if cmd.Valid() {
c.commands = append(c.commands, cmd)
} else if c.errFunc != nil {
c.errFunc(errCommand, cmd)
}
cmd.addMustKeys(cmd.names)
c.addCommand(cmd)
return cmd
}
return c.Usage(usage, args...)
Expand Down Expand Up @@ -133,7 +143,7 @@ func (c *_Command) LineArgument(usage string, args ...interface{}) Commander {
return cmd
}
cmd.addMustKeys(cmd.arguments.Get())
c.commands = append(c.commands, cmd)
c.addCommand(cmd)
return cmd
}

Expand All @@ -143,7 +153,7 @@ func (c *_Command) LineOption(usage string, args ...interface{}) Commander {
if cmd.options.IsEmpty() {
return cmd
}
c.commands = append(c.commands, cmd)
c.addCommand(cmd)
return cmd
}

Expand Down Expand Up @@ -235,10 +245,10 @@ func (c _Command) Parse(args ...[]string) (Context, error) {

func (c _Command) run(context Context) _Result {
if c.root || c.allow(context) {
if r := c.options.run(context); r != nil && r.Break() {
if r := c.commands.run(context); r != nil {
return r
}
if r := c.commands.run(context); r != nil && r.Break() {
if r := c.options.run(context); r != nil && r.Break() {
return r
}
return c.actor.run(context)
Expand Down
5 changes: 3 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commander
// _Commands Command line commands implementation
type _Commands []*_Command

// OptionsString
func (c _Commands) OptionsString() (r []string) {
for _, cmd := range c {
r = append(r, cmd.OptionsString()...)
Expand All @@ -12,9 +13,9 @@ func (c _Commands) OptionsString() (r []string) {

func (c _Commands) run(context Context) _Result {
for _, cmd := range c {
if r := cmd.run(context); r != nil && r.Break() {
if r := cmd.run(context); r != nil {
return r
}
}
return resultPass
return nil
}
5 changes: 4 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,8 @@ func (o _Option) OptionString() (s string) {
}

func (o _Option) run(c Context) _Result {
return o.actor.run(c)
if r := o.actor.run(c); r != nil && r.Break() {
return r
}
return nil
}
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (o _Options) OptionsString() (r []string) {

func (o _Options) run(c Context) _Result {
for _, opt := range o {
if r := opt.run(c); r != nil && r.Break() {
if r := opt.run(c); r != nil {
return r
}
}
return resultPass
return nil
}

0 comments on commit 94c6799

Please sign in to comment.