Skip to content

Commit

Permalink
better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aaabramov committed Nov 7, 2021
1 parent 88eeb84 commit 5f64bb4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
goal
# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,36 @@ func main() {
defaultFilename := "goal.yaml"

if _, err := os.Stat(defaultFilename); errors.Is(err, os.ErrNotExist) {
panic(fmt.Sprintf("%s does not exist!", defaultFilename))
errorFatal("%s does not exist!", defaultFilename)
}

file, err := ioutil.ReadFile("goal.yaml")
if err != nil {
panic(err)
errorFatal("Failed to commands from %s file.", defaultFilename)
}
commands, _ := parseCommands(file)

if len(os.Args) == 1 {
commands.render()
} else {
// TODO: support several commands
name := strings.TrimSpace(os.Args[1])
command, exists := commands.get(name)
if exists {
exec(command)
} else {
_, _ = os.Stderr.WriteString("No such command in goal.yaml: " + name + "\n")
errorFatal("No such command in goal.yaml: %s", name)
}
}

}

func errorFatal(message string, args ...string) {
msg := fmt.Sprintf(message, args)
_, _ = os.Stderr.WriteString(msg + "\n")
os.Exit(1)
}

func exec(command *Command) {
cmd := osexec.Command(command.Cmd, command.Args...)
cmd.Stdout = os.Stdout
Expand All @@ -119,5 +126,9 @@ func exec(command *Command) {

if err != nil {
fmt.Println(fmt.Sprint(err))
// TODO: code from child program
os.Exit(1)
} else {
os.Exit(0)
}
}

0 comments on commit 5f64bb4

Please sign in to comment.