Skip to content

Commit

Permalink
Remove positional args for cli commands (#56)
Browse files Browse the repository at this point in the history
remove positional args for cli commands
  • Loading branch information
hbeckmann authored Sep 20, 2024
1 parent 0f4c68d commit effb5ac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 19 deletions.
15 changes: 2 additions & 13 deletions checks/command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checks

import (
"fmt"
"os/exec"
"strings"

Expand All @@ -10,15 +9,13 @@ import (

func CLICommand(
lesson api.Lesson,
optionalPositionalArgs []string,
) []api.CLICommandResult {
data := lesson.Lesson.LessonDataCLICommand.CLICommandData
responses := make([]api.CLICommandResult, len(data.Commands))
for i, command := range data.Commands {
finalCommand := interpolateArgs(command.Command, optionalPositionalArgs)
responses[i].FinalCommand = finalCommand
responses[i].FinalCommand = command.Command

cmd := exec.Command("sh", "-c", "LANG=en_US.UTF-8 "+finalCommand)
cmd := exec.Command("sh", "-c", "LANG=en_US.UTF-8 "+command.Command)
b, err := cmd.Output()
if ee, ok := err.(*exec.ExitError); ok {
responses[i].ExitCode = ee.ExitCode()
Expand All @@ -29,11 +26,3 @@ func CLICommand(
}
return responses
}

func interpolateArgs(rawCommand string, optionalPositionalArgs []string) string {
// replace $1, $2, etc. with the optional positional args
for i, arg := range optionalPositionalArgs {
rawCommand = strings.ReplaceAll(rawCommand, fmt.Sprintf("$%d", i+1), arg)
}
return rawCommand
}
6 changes: 1 addition & 5 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
isSubmit := cmd.Name() == "submit" || forceSubmit
lessonUUID := args[0]
optionalPositionalArgs := []string{}
if len(args) > 1 {
optionalPositionalArgs = args[1:]
}

lesson, err := api.FetchLesson(lessonUUID)
if err != nil {
Expand All @@ -53,7 +49,7 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
render.HTTPRun(data, results)
}
case "type_cli_command":
results := checks.CLICommand(*lesson, optionalPositionalArgs)
results := checks.CLICommand(*lesson)
data := *lesson.Lesson.LessonDataCLICommand
if isSubmit {
failure, err := api.SubmitCLICommandLesson(lessonUUID, results)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.9.0
v1.9.1

0 comments on commit effb5ac

Please sign in to comment.