Example #1
0
func execCommand() {
	requestedCmd := ""
	if len(os.Args) > 2 {
		requestedCmd = os.Args[2]
	}
	var cmd *Command
	for _, c := range config.Commands {
		if c.Name == requestedCmd {
			cmd = c
			break
		}
	}

	if cmd == nil {
		fmt.Fprintf(os.Stderr, "ERROR: Unknown command '%s'. ", requestedCmd)
		if len(config.Commands) == 0 {
			fmt.Fprintf(os.Stderr, "There are no commands configured!\n")
		} else {
			fmt.Fprintf(os.Stderr, "Valid commands are:\n")
			for _, cmd = range config.Commands {
				fmt.Fprintf(os.Stderr, "    %s\n", cmd.Name)
			}
		}
		os.Exit(1)
	}

	// Exec the command
	c := new(run.CommandConfig)
	c.Path = pathutils.FindLastFile(cmd.Path)
	if len(os.Args) > 3 {
		c.Args = append(cmd.Args, os.Args[3:]...)
	} else {
		c.Args = cmd.Args
	}
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	c.Logger = nil // No logger as we're in console mode (e.g. end-user)

	exitCode, err := run.RunCommand(c, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	os.Exit(exitCode)
}