Skip to content

Commit

Permalink
tasks params order fix
Browse files Browse the repository at this point in the history
  • Loading branch information
speier committed Nov 6, 2023
1 parent 59dbf70 commit ad51905
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 68 deletions.
47 changes: 3 additions & 44 deletions Lunafile
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,7 @@ def docs():
sh('echo $VERSION @ $FOO', env=['FOO=BAR'])
sh('go generate -v ./internal/api')

# ex: luna new stack sandbox
def new(kind, name):
if kind == 'stack':
sh('terramate create stack/$name')

# ex:
# luna stacks dev init -upgrade
# luna stacks dev k8s apply --auto-approve
# luna stacks dev output
def stacks(stack, command, *args):
sh('echo $stack $command ${@:2}')

def kube(stack='abc'):
sh('echo $stack')

# register service

# configure air for watch
# air(air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" --build.exclude_dir "templates,build")

# stella run [service] [--watch]

# stella build [service]

# #

# def kube(command, stack):
# if command == 'config'
# kubeconfig_merge('stacks/'+stack)

# stella kube config dev
# infra/kube.sh config dev


# def _fix_target(s):
# return s


# def tags(fname, bar=1, baz=2):
# fname = _fix_target(fname)
# # kustomize_setimage(tags)
# # shell_exec('echo $fname')
# print('>>>', fname, bar, baz)
# sh('echo "hello world!"')
# return 'xxx1'
print(stack, command)
sh('echo $stack $command')
sh('echo $1 $2')
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.3.3
VERSION=v0.3.4

release:
@git tag -a ${VERSION} -m "Release ${VERSION}" && git push origin ${VERSION}
Expand Down
53 changes: 30 additions & 23 deletions internal/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var (
}
)

type fnParam struct {
name string
value string
}

func Load(fname string, command *cobra.Command) error {
builtins.ConfigTasks(command, groupID, groupTitle)

Expand Down Expand Up @@ -89,25 +94,19 @@ func addCommand(command *cobra.Command, fn *starlark.Function) {
GroupID: groupID,
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
// set params as positional params for later usage in shell
support.TaskParams = args

// set params as env vars for later usage in shell
pos := 0
for name, val := range params {
if len(args) > pos && len(args[pos]) > 0 {
val = args[pos]
}
os.Setenv(name, val)
pos++
}

// add to task params for later usage as params in shell
support.TaskParams = args
paramsWithArgsToEnv(params, args)

// call the function
out, err := starlark.Call(thread, fn, starlarkArgs(args), nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

// report output
_, none := (out).(starlark.NoneType)
if out != nil && !none {
fmt.Println(strings.Trim(out.String(), `"`))
Expand All @@ -116,8 +115,8 @@ func addCommand(command *cobra.Command, fn *starlark.Function) {
})
}

func parseParams(fn *starlark.Function) (int, map[string]string) {
params := make(map[string]string)
func parseParams(fn *starlark.Function) (int, []*fnParam) {
var params []*fnParam
minParams := fn.NumParams()

if fn.HasVarargs() {
Expand All @@ -129,31 +128,39 @@ func parseParams(fn *starlark.Function) (int, map[string]string) {

p := minParams
for i := 0; i < p; i++ {
param, _ := fn.Param(i)
defval := fn.ParamDefault(i)
name, _ := fn.Param(i)
value := fn.ParamDefault(i)

if defval != nil {
params[param] = defval.String()
// has default value, less required params
if value != nil {
params = append(params, &fnParam{name, value.String()})
minParams--
} else {
params[param] = ""
params = append(params, &fnParam{name, ""})
}
}

return minParams, params
}

func fmtParams(s string, params map[string]string) string {
for name, defval := range params {
if defval == "" {
s += fmt.Sprintf(" <%s>", name)
func fmtParams(s string, params []*fnParam) string {
for _, p := range params {
if p.value == "" {
s += fmt.Sprintf(" <%s>", p.name)
} else {
s += fmt.Sprintf(" [%s=%s]", name, defval)
s += fmt.Sprintf(" [%s=%s]", p.name, p.value)
}
}
return s
}

func paramsWithArgsToEnv(params []*fnParam, args []string) {
for i, p := range params {
v := args[i]
os.Setenv(p.name, v)
}
}

func starlarkArgs(args []string) starlark.Tuple {
sargs := starlark.Tuple{}
for _, v := range args {
Expand Down

0 comments on commit ad51905

Please sign in to comment.