Skip to content

Commit

Permalink
fix: refactor error handling around loading in tasks (#108)
Browse files Browse the repository at this point in the history
## Description

Make the error more obvious if a run command is failing because the file
doesnt exist rather than there is an issue with the yaml itself.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/maru-runner/blob/main/CONTRIBUTING.md)
followed

---------

Co-authored-by: Wayne Starr <Racer159@users.noreply.github.com>
  • Loading branch information
zachariahmiller and Racer159 authored Jun 8, 2024
1 parent 5bcee5d commit b2896bd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var runCmd = &cobra.Command{

err := utils.ReadYaml(config.TaskFileLocation, &tasksFile)
if err != nil {
message.Fatalf(err, "Cannot unmarshal %s", config.TaskFileLocation)
message.Fatalf(err, "Failed to open file: %s", err.Error())
}

// ensure vars are uppercase
Expand Down Expand Up @@ -190,7 +190,7 @@ func loadTasksFromLocalIncludes(includeFileLocation string) types.TasksFile {
}
err := utils.ReadYaml(fullPath, &includedTasksFile)
if err != nil {
message.Fatalf(err, "Cannot unmarshal %s", fullPath)
message.Fatalf(err, "Failed to load file: %s", err.Error())
}
return includedTasksFile
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *Runner) importTasks(includes []map[string]string, dir string, setVariab
}

if err := utils.ReadYaml(includePath, &tasksFile); err != nil {
return fmt.Errorf("unable to read included file %s: %w", includePath, err)
return fmt.Errorf("unable to read included file: %w", err)
}

// prefix task names and actions with the includes key
Expand Down
11 changes: 8 additions & 3 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ func FormatEnvVar(name, value string) string {
func ReadYaml(path string, destConfig any) error {
file, err := os.ReadFile(path)
if err != nil {
return err
return fmt.Errorf("cannot %s", err.Error())
}

return goyaml.Unmarshal(file, destConfig)
err = goyaml.Unmarshal(file, destConfig)
if err != nil {
errStr := err.Error()
lines := strings.SplitN(errStr, "\n", 2)
return fmt.Errorf("cannot unmarshal %s: %s", path, lines[0])
}
return nil
}

// MakeTempDir creates a temp directory with the maru- prefix.
Expand Down

0 comments on commit b2896bd

Please sign in to comment.