github.com/WindomZ/go-commander@v1.2.2/actor.go (about) 1 package commander 2 3 // actor Assigned to execute the command 4 type actor struct { 5 names []string // the keys contain one of names than execute action 6 triggers map[string]bool // the keys contain all true values and none false value in triggers than execute action 7 action Action // executed command 8 ignore bool // ignore this action 9 break_off bool // break off both actions 10 } 11 12 // addIncludeKeys append include keys to actor.triggers 13 func (a *actor) addIncludeKeys(keys []string) { 14 if keys != nil && len(keys) != 0 { 15 if a.triggers == nil { 16 a.triggers = make(map[string]bool) 17 } 18 for _, key := range keys { 19 a.triggers[key] = true 20 } 21 } 22 } 23 24 // getIncludeKeys get list of include keys 25 func (a actor) getIncludeKeys() (keys []string) { 26 for key, ok := range a.triggers { 27 if ok { 28 keys = append(keys, key) 29 } 30 } 31 return keys 32 } 33 34 // addExcludeKeys append exclude keys to actor.triggers 35 func (a *actor) addExcludeKeys(keys []string) { 36 if keys != nil && len(keys) != 0 { 37 if a.triggers == nil { 38 a.triggers = make(map[string]bool) 39 } 40 for _, key := range keys { 41 if _, ok := a.triggers[key]; !ok { 42 a.triggers[key] = false 43 } 44 } 45 } 46 } 47 48 // getExcludeKeys get list of exclude keys 49 func (a actor) getExcludeKeys() (keys []string) { 50 for key, ok := range a.triggers { 51 if !ok { 52 keys = append(keys, key) 53 } 54 } 55 return keys 56 } 57 58 // setAction set executive function to actor.action 59 // arg is ACTION function, see ./action.go 60 func (a *actor) setAction(arg interface{}) { 61 if action := parseAction(arg); !emptyAction(action) { 62 a.action = action 63 } 64 } 65 66 // hasAction there is a legitimate actor.action 67 func (a actor) hasAction() bool { 68 return a.action != nil 69 } 70 71 // Action set executive function to actor.action and include keys to actor.triggers 72 // action is ACTION function, see ./action.go 73 func (a *actor) Action(action interface{}, keys ...[]string) { 74 a.setAction(action) 75 if len(keys) != 0 { 76 a.addIncludeKeys(keys[0]) 77 } 78 } 79 80 // allow Determine whether meet the requirements(actor.names or actor.triggers) for the execution 81 func (a actor) allow(c Context) (pass bool) { 82 //defer func() { 83 // fmt.Printf("----------allow----------"+ 84 // "\n 1.actor %#v\n 2.argv %v\n 3.action %v\n 4.pass %v\n", 85 // a, c.Map(), a.action != nil, pass) 86 //}() 87 for key, ok := range a.triggers { 88 if !ok && c.Contain(key) { 89 pass = false 90 return 91 } 92 } 93 94 for _, key := range a.names { 95 if c.Contain(key) { 96 pass = true 97 return 98 } 99 } 100 101 for key, ok := range a.triggers { 102 if ok && !c.Contain(key) { 103 pass = false 104 return 105 } 106 } 107 pass = len(a.triggers) != 0 108 return 109 } 110 111 // run Common external function, if allow() than execute actor.action 112 func (a actor) run(c Context, force ...bool) (result _Result) { 113 //defer func() { 114 // fmt.Printf("----------run----------"+ 115 // "\n 1.actor %#v\n 2.argv %v\n 3.action %v\n 4.result %#v\n", 116 // a, c.Map(), a.action != nil, result) 117 //}() 118 if a.action == nil { 119 return 120 } else if len(force) != 0 && force[0] { 121 } else if !a.allow(c) { 122 return 123 } else if a.ignore { 124 return resultPass() 125 } 126 result = a.action(c) 127 if a.break_off && result != nil && !result.Break() { 128 result.setBreak() 129 } 130 return 131 }