Skip to content

Commit

Permalink
use an array instead of a map for the arguments, to avoid issues with…
Browse files Browse the repository at this point in the history
… processing order
  • Loading branch information
dreadl0ck committed Feb 18, 2021
1 parent 4b20459 commit 7a209ff
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 24 deletions.
26 changes: 18 additions & 8 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ type commandArg struct {

// validate arguments string from CommandsFile
// and return the validatedArgs as map
func (c *CommandsFile) validateArgs(args []string) (map[string]*commandArg, error) {
func (c *CommandsFile) validateArgs(args []string) ([]*commandArg, error) {

// init map
validatedArgs := make(map[string]*commandArg, 0)
var validatedArgs []*commandArg

// empty string - empty args
if len(args) == 0 {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (c *CommandsFile) validateArgs(args []string) (map[string]*commandArg, erro
g.Unlock()

// check for duplicate argument names
if a, ok := validatedArgs[argumentName]; ok {
if a, ok := containsArg(validatedArgs, argumentName); ok {
Log.Error("argument label ", a.name, " was used twice")
return nil, ErrDuplicateArgumentNames
}
Expand Down Expand Up @@ -138,12 +138,12 @@ func (c *CommandsFile) validateArgs(args []string) (map[string]*commandArg, erro
}

// add to validatedArgs
validatedArgs[argumentName] = &commandArg{
validatedArgs = append(validatedArgs, &commandArg{
name: argumentName,
argType: k,
optional: opt,
defaultValue: defaultValue,
}
})
} else {
return nil, errors.New("invalid argument declaration: " + s)
}
Expand Down Expand Up @@ -184,7 +184,8 @@ func (c *command) parseArguments(args []string) (string, map[string]string, erro
return "", argValues, errors.New("invalid argument: " + val)
}

if cmdArg, ok = c.args[argSlice[0]]; !ok {
cmdArg, ok = containsArg(c.args, argSlice[0])
if !ok {
return "", argValues, errors.New(ErrInvalidArgumentLabel.Error() + ": " + ansi.Red + argSlice[0] + cp.Reset)
}

Expand All @@ -202,8 +203,8 @@ func (c *command) parseArguments(args []string) (string, map[string]string, erro
return "", argValues, errors.New(ErrInvalidArgumentType.Error() + ": " + err.Error() + ", label=" + cmdArg.name + ", value=" + argSlice[1])
}

// set value in c.args
c.args[argSlice[0]].value = argSlice[1]
// temporarily set value on arg
cmdArg.value = argSlice[1]
} else {
return "", argValues, errors.New("invalid argument: " + val)
}
Expand Down Expand Up @@ -382,3 +383,12 @@ func resolveEnvironment(input string) string {

return input
}

func containsArg(args []*commandArg, name string) (*commandArg, bool) {
for _, a := range args {
if a.name == name {
return a, true
}
}
return nil, false
}
14 changes: 10 additions & 4 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main

import (
"io/ioutil"
"log"
"os"
"os/exec"
"sort"
Expand Down Expand Up @@ -251,7 +252,7 @@ func printLine(line string, lastElem, lastItem bool) {
}

// format commandArg map into human readable string
func getArgumentString(args map[string]*commandArg) string {
func getArgumentString(args []*commandArg) string {

if len(args) == 0 {
return ""
Expand All @@ -265,13 +266,18 @@ func getArgumentString(args map[string]*commandArg) string {
)

// sort args alphabetically before printing, so the order does not change in between multiple runs.
for name := range args {
names = append(names, name)
for _, a := range args {
names = append(names, a.name)
}
sort.Strings(names)

for _, name := range names {
arg := args[name]

arg, ok := containsArg(args, name)
if !ok {
log.Fatal("could not find arg: " + name)
}

var t = cp.CmdArgType + strings.Title(arg.argType.String())
if arg.optional {
if arg.defaultValue != "" {
Expand Down
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type command struct {

// arguments for the command
// mapped labels to commandArg instances
args map[string]*commandArg
args []*commandArg

// short descriptive text
description string
Expand Down Expand Up @@ -762,7 +762,7 @@ func initScript(path string) error {
cmd := &command{
path: path,
name: name,
args: make(map[string]*commandArg, 0),
args: []*commandArg{},
description: "",
help: "",
buildNumber: false,
Expand Down
2 changes: 1 addition & 1 deletion events.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
ErrInvalidUsage = errors.New("invalid usage")
)

// temporarely disable change event
// temporarily disable change event
func blockWriteEvent() {
disableWriteEventMutex.Lock()
disableWriteEvent = true
Expand Down
2 changes: 1 addition & 1 deletion tests/zeus/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# /_____ \\___ >____//____ >
# \/ \/ \/
# Build System
# v0.9.1
# v0.9.7
#

autoFormat: false
Expand Down
6 changes: 3 additions & 3 deletions tests/zeus/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
# /_____ \\___ >____//____ >
# \/ \/ \/
# Build System
# v0.9.1
# v0.9.7
#

buildNumber: 3
deadline: ""
milestones: []
aliases: {}
events:
0e01507e29e37318:
1184e2da72dbcd1b:
name: config watcher
id: 0e01507e29e37318
id: 1184e2da72dbcd1b
path: tests/zeus/config.yml
op: 2
fileextension: .yml
Expand Down
6 changes: 3 additions & 3 deletions tests/zeus/generated/build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash
# generated by ZEUS v0.9.1
# Timestamp: [Thu Feb 11 15:52:04 2021]
# generated by ZEUS v0.9.7
# Timestamp: [Thu Feb 18 17:21:55 2021]

binaryName="zeus"
buildDir="bin"
version="0.9.1"
version="0.9.7"



Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package main

// zeus version
// generated with the gen-version command
var version = "0.9.7"
var version = "0.9.8"
2 changes: 1 addition & 1 deletion zeus/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ language: bash
globals:
binaryName: zeus
buildDir: bin
version: 0.9.7
version: 0.9.8

# all commands
# available fields:
Expand Down

0 comments on commit 7a209ff

Please sign in to comment.