-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* in process of implementing validation * validate depends * validated command * split command parse function into files, one for each command section * capture os env when run command * fix staticcheck compalians * add dockerfile for easier testing locally
- Loading branch information
Showing
16 changed files
with
276 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package command | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
func parseAndValidateCmd(cmd interface{}, newCmd *Command) error { | ||
switch cmd := cmd.(type) { | ||
case string: | ||
newCmd.Cmd = cmd | ||
case []interface{}: | ||
cmdList := make([]string, len(cmd)) | ||
for _, v := range cmd { | ||
cmdList = append(cmdList, v.(string)) | ||
} | ||
cmdList = append(cmdList, os.Args[1:]...) | ||
newCmd.Cmd = strings.Join(cmdList, " ") | ||
default: | ||
return newCommandError( | ||
"must be either string or list of string", | ||
newCmd.Name, | ||
CMD, | ||
"", | ||
) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package command | ||
|
||
func parseAndValidateDepends(depends interface{}, newCmd *Command) error { | ||
if depends, ok := depends.([]interface{}); ok { | ||
for _, value := range depends { | ||
if value, ok := value.(string); ok { | ||
// TODO validate if command is really exists - in validate | ||
newCmd.Depends = append(newCmd.Depends, value) | ||
} else { | ||
return newCommandError( | ||
"value of depends list must be a string", | ||
newCmd.Name, | ||
DEPENDS, | ||
"", | ||
) | ||
} | ||
} | ||
} else { | ||
return newCommandError( | ||
"must be a list of string (commands)", | ||
newCmd.Name, | ||
DEPENDS, | ||
"", | ||
) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package command | ||
|
||
func parseAndValidateDescription(desc interface{}, newCmd *Command) error { | ||
if value, ok := desc.(string); ok { | ||
newCmd.Description = value | ||
} else { | ||
return newCommandError( | ||
"must be a string", | ||
newCmd.Name, | ||
DESCRIPTION, | ||
"", | ||
) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package command | ||
|
||
import "os/exec" | ||
|
||
func evalEnvVariable(rawCmd string) (string, error) { | ||
cmd := exec.Command("sh", "-c", rawCmd) | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
func parseAndValidateEnv(env interface{}, newCmd *Command) error { | ||
for name, value := range env.(map[interface{}]interface{}) { | ||
nameKey := name.(string) | ||
if value, ok := value.(string); ok { | ||
newCmd.Env[nameKey] = value | ||
} else { | ||
return newCommandError( | ||
"must be a string", | ||
newCmd.Name, | ||
ENV, | ||
nameKey, | ||
) | ||
} | ||
} | ||
return string(out), nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package command | ||
|
||
import "os/exec" | ||
|
||
func evalEnvVariable(rawCmd string) (string, error) { | ||
cmd := exec.Command("sh", "-c", rawCmd) | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(out), nil | ||
} | ||
|
||
func parseAndValidateEvalEnv(evalEnv interface{}, newCmd *Command) error { | ||
for name, value := range evalEnv.(map[interface{}]interface{}) { | ||
nameKey := name.(string) | ||
if value, ok := value.(string); ok { | ||
if computedVal, err := evalEnvVariable(value); err != nil { | ||
return err | ||
} else { | ||
newCmd.Env[nameKey] = computedVal | ||
} | ||
} else { | ||
return newCommandError( | ||
"must be a string", | ||
newCmd.Name, | ||
EVAL_ENV, | ||
nameKey, | ||
) | ||
} | ||
if computedVal, err := evalEnvVariable(value.(string)); err != nil { | ||
// TODO we have to fail here and log error for user | ||
} else { | ||
newCmd.Env[name.(string)] = computedVal | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package command | ||
|
||
func parseAndValidateOptions(options interface{}, newCmd *Command) error { | ||
if value, ok := options.(string); ok { | ||
newCmd.RawOptions = value | ||
} else { | ||
return newCommandError( | ||
"must be a string", | ||
newCmd.Name, | ||
OPTIONS, | ||
"", | ||
) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.