Skip to content
New issue

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

parse out the map here #91

Open
github-actions bot opened this issue Nov 7, 2023 · 0 comments
Open

parse out the map here #91

github-actions bot opened this issue Nov 7, 2023 · 0 comments
Assignees
Labels

Comments

@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2023

// TODO parse out the map here

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{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant