@@ -24,6 +24,18 @@ type workceptorCommand struct {
2424 params map [string ]interface {}
2525}
2626
27+ const (
28+ errFieldMissing = "field %s missing"
29+ )
30+
31+ const (
32+ cmdCancel = "cancel"
33+ cmdRelease = "release"
34+ cmdForceRelease = "force-release"
35+ cmdStatus = "status"
36+ cmdList = "list"
37+ )
38+
2739func (t * workceptorCommandType ) InitFromString (params string ) (controlsvc.ControlCommand , error ) {
2840 tokens := strings .Split (params , " " )
2941 if len (tokens ) == 0 {
@@ -44,11 +56,11 @@ func (t *workceptorCommandType) InitFromString(params string) (controlsvc.Contro
4456 if len (tokens ) > 3 {
4557 c .params ["params" ] = strings .Join (tokens [3 :], " " )
4658 }
47- case "list" :
59+ case cmdList :
4860 if len (tokens ) > 1 {
4961 c .params ["unitid" ] = tokens [1 ]
5062 }
51- case "status" , "cancel" , "release" , "force-release" :
63+ case cmdStatus , cmdCancel , cmdRelease , cmdForceRelease :
5264 if len (tokens ) < 2 {
5365 return nil , fmt .Errorf ("work %s requires a unit ID" , c .subcommand )
5466 }
@@ -82,7 +94,7 @@ func (t *workceptorCommandType) InitFromString(params string) (controlsvc.Contro
8294func strFromMap (config map [string ]interface {}, name string ) (string , error ) {
8395 value , ok := config [name ]
8496 if ! ok {
85- return "" , fmt .Errorf ("field %s missing" , name )
97+ return "" , fmt .Errorf (errFieldMissing , name )
8698 }
8799 valueStr , ok := value .(string )
88100 if ! ok {
@@ -96,7 +108,7 @@ func strFromMap(config map[string]interface{}, name string) (string, error) {
96108func intFromMap (config map [string ]interface {}, name string ) (int64 , error ) {
97109 value , ok := config [name ]
98110 if ! ok {
99- return 0 , fmt .Errorf ("field %s missing" , name )
111+ return 0 , fmt .Errorf (errFieldMissing , name )
100112 }
101113 valueInt , ok := value .(int64 )
102114 if ok {
@@ -120,7 +132,7 @@ func intFromMap(config map[string]interface{}, name string) (int64, error) {
120132func boolFromMap (config map [string ]interface {}, name string ) (bool , error ) {
121133 value , ok := config [name ]
122134 if ! ok {
123- return false , fmt .Errorf ("field %s missing" , name )
135+ return false , fmt .Errorf (errFieldMissing , name )
124136 }
125137 valueBoolStr , ok := value .(string )
126138 if ! ok {
@@ -163,7 +175,7 @@ func (t *workceptorCommandType) InitFromJSON(config map[string]interface{}) (con
163175 if err != nil {
164176 return nil , err
165177 }
166- case "status" , "cancel" , "release" , "force-release" :
178+ case cmdStatus , cmdCancel , cmdRelease , cmdForceRelease :
167179 c .params ["unitid" ], err = strFromMap (config , "unitid" )
168180 if err != nil {
169181 return nil , err
@@ -172,7 +184,7 @@ func (t *workceptorCommandType) InitFromJSON(config map[string]interface{}) (con
172184 if err == nil {
173185 c .params ["signature" ] = signature
174186 }
175- case "list" :
187+ case cmdList :
176188 unitID , err := strFromMap (config , "unitid" )
177189 if err == nil {
178190 c .params ["unitid" ] = unitID
@@ -327,7 +339,7 @@ func (c *workceptorCommand) ControlFunc(ctx context.Context, nc controlsvc.Netce
327339 }
328340
329341 return cfr , nil
330- case "list" :
342+ case cmdList :
331343 var unitList []string
332344 targetUnitID , ok := c .params ["unitid" ].(string )
333345 if ok {
@@ -346,7 +358,7 @@ func (c *workceptorCommand) ControlFunc(ctx context.Context, nc controlsvc.Netce
346358 }
347359
348360 return cfr , nil
349- case "status" :
361+ case cmdStatus :
350362 unitid , err := strFromMap (c .params , "unitid" )
351363 if err != nil {
352364 return nil , err
@@ -357,7 +369,7 @@ func (c *workceptorCommand) ControlFunc(ctx context.Context, nc controlsvc.Netce
357369 }
358370
359371 return cfr , nil
360- case "cancel" , "release" , "force-release" :
372+ case cmdCancel , cmdRelease , cmdForceRelease :
361373 unitid , err := strFromMap (c .params , "unitid" )
362374 if err != nil {
363375 return nil , err
@@ -369,7 +381,7 @@ func (c *workceptorCommand) ControlFunc(ctx context.Context, nc controlsvc.Netce
369381 cfr := make (map [string ]interface {})
370382 var pendingMsg string
371383 var completeMsg string
372- if c .subcommand == "cancel" {
384+ if c .subcommand == cmdCancel {
373385 pendingMsg = "cancel pending"
374386 completeMsg = "cancelled"
375387 } else {
@@ -388,10 +400,10 @@ func (c *workceptorCommand) ControlFunc(ctx context.Context, nc controlsvc.Netce
388400 if err != nil {
389401 return nil , err
390402 }
391- if c .subcommand == "cancel" {
403+ if c .subcommand == cmdCancel {
392404 err = unit .Cancel ()
393405 } else {
394- err = unit .Release (c .subcommand == "force-release" )
406+ err = unit .Release (c .subcommand == cmdForceRelease )
395407 }
396408 if err != nil && ! IsPending (err ) {
397409 return nil , err
0 commit comments