Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"sort"

"github.com/gomicro/forge/cmd/config"
Expand Down Expand Up @@ -61,12 +63,19 @@ reference sub-steps, or define pre/post hooks. Pass --verbose to see each comman
Args: cobra.MinimumNArgs(1),
RunE: rootFunc,
ValidArgsFunction: validArgsFunc,
SilenceUsage: true,
SilenceErrors: true,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}

os.Exit(1)
}
}
Expand Down
29 changes: 26 additions & 3 deletions confile/step.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package confile

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -125,14 +126,36 @@ func executeCmd(cmdString string, stepEnvs, projectEnvs map[string]string, vars

cmd.Env = append(cmd.Env, os.Environ()...)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Start()
if err != nil {
return fmt.Errorf("execute: %w", err)
}

return cmd.Wait()
waitErr := cmd.Wait()

if stdout.Len() > 0 {
scrb.PrintLines(&stdout)
}

if stderr.Len() > 0 {
if viper.GetBool("verbose") {
scrb.BeginDescribe("\033[1;31mstderr\033[0m")
scrb.PrintLines(&stderr)
scrb.EndDescribe()
} else {
fmt.Fprintf(os.Stderr, "\033[1;31mstderr\033[0m\n%s", stderr.String())
}
}

if waitErr != nil {
return fmt.Errorf("execute: %w", waitErr)
}

return nil
}

func toSlice(e map[string]string) []string {
Expand Down
Loading