We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grlx/ingredients/cmd/cmd.go
Line 21 in f2b2b3a
import ( "context" "encoding/json" "errors" "fmt" "github.com/gogrlx/grlx/ingredients" "github.com/gogrlx/grlx/types" ) var ErrCmdMethodUndefined = fmt.Errorf("cmd method undefined") type Cmd struct { id string method string params map[string]interface{} } // TODO parse out the map here func (c Cmd) Parse(id, method string, params map[string]interface{}) (types.RecipeCooker, error) { if params == nil { params = map[string]interface{}{} } return Cmd{ id: id, method: method, params: params, }, nil } func (c Cmd) validate() error { set, err := c.PropertiesForMethod(c.method) if err != nil { return err } propSet, err := ingredients.PropMapToPropSet(set) if err != nil { return err } for _, v := range propSet { if v.IsReq { if v.Key == "name" { name, ok := c.params[v.Key].(string) if !ok { return types.ErrMissingName } if name == "" { return types.ErrMissingName } } else { if _, ok := c.params[v.Key]; !ok { return fmt.Errorf("missing required property %s", v.Key) } } } } return nil } func (c Cmd) Test(ctx context.Context) (types.Result, error) { return types.Result{ Succeeded: true, Failed: false, Changed: false, Notes: []fmt.Stringer{types.SimpleNote("cmd would have been executed")}, }, nil } func (c Cmd) Apply(ctx context.Context) (types.Result, error) { switch c.method { case "run": fallthrough default: return types.Result{Succeeded: false, Failed: true, Changed: false, Notes: nil}, errors.Join(ErrCmdMethodUndefined, fmt.Errorf("method %s undefined", c.method)) } } // TODO create map for method: type func (c Cmd) PropertiesForMethod(method string) (map[string]string, error) { switch method { case "run": return ingredients.MethodPropsSet{ ingredients.MethodProps{Key: "name", Type: "string", IsReq: true}, ingredients.MethodProps{Key: "args", Type: "string", IsReq: false}, ingredients.MethodProps{Key: "env", Type: "[]string", IsReq: false}, ingredients.MethodProps{Key: "cwd", Type: "string", IsReq: false}, ingredients.MethodProps{Key: "runas", Type: "string", IsReq: false}, ingredients.MethodProps{Key: "path", Type: "string", IsReq: false}, ingredients.MethodProps{Key: "timeout", Type: "string", IsReq: false}, }.ToMap(), nil default: return nil, fmt.Errorf("method %s undefined", method) } } func (c Cmd) Methods() (string, []string) { return "cmd", []string{"run"} } func (c Cmd) Properties() (map[string]interface{}, error) { m := map[string]interface{}{} b, err := json.Marshal(c.params) if err != nil { return m, err } err = json.Unmarshal(b, &m) return m, err } func init() { ingredients.RegisterAllMethods(Cmd{}) }
The text was updated successfully, but these errors were encountered:
taigrr
No branches or pull requests
grlx/ingredients/cmd/cmd.go
Line 21 in f2b2b3a
The text was updated successfully, but these errors were encountered: