Skip to content

Commit

Permalink
builder: Print load commands output on default
Browse files Browse the repository at this point in the history
Now load command output will be printed on default.
To disable this add "hide_load_output: 1" to ~/.newt/newtrc.yml
  • Loading branch information
m-gorecki committed Nov 25, 2024
1 parent c6bf556 commit e5adbee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
8 changes: 5 additions & 3 deletions newt/builder/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,18 @@ func Load(binBasePath string, bspPkg *pkg.BspPackage,
binBasePath,
}

util.StatusMessage(util.VERBOSITY_VERBOSE, "Load command: %s\n",
util.StatusMessage(util.VERBOSITY_DEFAULT, "Load command: %s\n",
strings.Join(cmd, " "))
util.StatusMessage(util.VERBOSITY_VERBOSE, "Environment:\n")
for _, v := range env {
util.StatusMessage(util.VERBOSITY_VERBOSE, "* %s\n", v)
}
if _, err := util.ShellCommand(cmd, env); err != nil {

if err := util.ShellCommandStreamOutput(cmd, env, true, !util.HideLoadCmdOutput); err != nil {
return err
}
util.StatusMessage(util.VERBOSITY_VERBOSE, "Successfully loaded image.\n")

util.StatusMessage(util.VERBOSITY_DEFAULT, "Successfully loaded image.\n")

return nil
}
Expand Down
1 change: 1 addition & 0 deletions newt/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func processNewtrc(yc ycfg.YCfg) {

util.SkipNewtCompat, _ = yc.GetValBoolDflt("skip_newt_compat", nil, false)
util.SkipSyscfgRepoHash, _ = yc.GetValBoolDflt("skip_syscfg_repo_hash", nil, false)
util.HideLoadCmdOutput, _ = yc.GetValBoolDflt("hide_load_output", nil, false)
}

func readNewtrc() ycfg.YCfg {
Expand Down
96 changes: 71 additions & 25 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var ShallowCloneDepth int
var logFile *os.File
var SkipNewtCompat bool
var SkipSyscfgRepoHash bool
var HideLoadCmdOutput bool

func ParseEqualsPair(v string) (string, string, error) {
s := strings.Split(v, "=")
Expand Down Expand Up @@ -352,38 +353,14 @@ func EnvironAsMap() (map[string]string, error) {
return m, nil
}

// Execute the specified process and block until it completes. Additionally,
// the amount of combined stdout+stderr output to be logged to the debug log
// can be restricted to a maximum number of characters.
//
// @param cmdStrs The "argv" strings of the command to execute.
// @param env Additional key,value pairs to inject into the
// child process's environment. Specify null
// to just inherit the parent environment.
// @param logCmd Whether to log the command being executed.
// @param maxDbgOutputChrs The maximum number of combined stdout+stderr
// characters to write to the debug log.
// Specify -1 for no limit; 0 for no output.
//
// @return []byte Combined stdout and stderr output of process.
// @return error NewtError on failure. Use IsExit() to
// determine if the command failed to execute
// or if it just returned a non-zero exit
// status.
func ShellCommandLimitDbgOutput(
cmdStrs []string, env map[string]string, logCmd bool,
maxDbgOutputChrs int) ([]byte, error) {
func ShellCommandInit(cmdStrs []string, env map[string]string) (*exec.Cmd, error) {

var name string
var args []string

// Escape special characters for Windows.
fixupCmdArgs(cmdStrs)

if logCmd {
LogShellCmd(cmdStrs, env)
}

if ExecuteShell && (runtime.GOOS == "linux" || runtime.GOOS == "darwin") {
cmd := strings.Join(cmdStrs, " ")
name = "/bin/sh"
Expand Down Expand Up @@ -418,6 +395,75 @@ func ShellCommandLimitDbgOutput(
cmd.Env = EnvVarsToSlice(m)
}

return cmd, nil
}

// Execute the specified process and block until it completes. Process'
// output will be redirected to the stdout and stderr if output log is enabled
//
// @param cmdStrs The "argv" strings of the command to execute.
// @param env Additional key,value pairs to inject into the
// child process's environment. Specify null
// to just inherit the parent environment.
// @param logCmd Whether to log the command being executed.
// @param maxDbgOutputChrs Whether to log the output of the process
//
// @return error NewtError on failure. Use IsExit() to
// determine if the command failed to execute
// or if it just returned a non-zero exit
// status.
func ShellCommandStreamOutput(
cmdStrs []string, env map[string]string, logCmd bool,
logOutput bool) error {

cmd, err := ShellCommandInit(cmdStrs, env)
if err != nil {
return err
}

if logCmd {
LogShellCmd(cmdStrs, env)
}

if logOutput {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}

return cmd.Run()
}

// Execute the specified process and block until it completes. Additionally,
// the amount of combined stdout+stderr output to be logged to the debug log
// can be restricted to a maximum number of characters.
//
// @param cmdStrs The "argv" strings of the command to execute.
// @param env Additional key,value pairs to inject into the
// child process's environment. Specify null
// to just inherit the parent environment.
// @param logCmd Whether to log the command being executed.
// @param maxDbgOutputChrs The maximum number of combined stdout+stderr
// characters to write to the debug log.
// Specify -1 for no limit; 0 for no output.
//
// @return []byte Combined stdout and stderr output of process.
// @return error NewtError on failure. Use IsExit() to
// determine if the command failed to execute
// or if it just returned a non-zero exit
// status.
func ShellCommandLimitDbgOutput(
cmdStrs []string, env map[string]string, logCmd bool,
maxDbgOutputChrs int) ([]byte, error) {

cmd, err := ShellCommandInit(cmdStrs, env)
if err != nil {
return nil, err
}

if logCmd {
LogShellCmd(cmdStrs, env)
}

o, err := cmd.CombinedOutput()

if maxDbgOutputChrs < 0 || len(o) <= maxDbgOutputChrs {
Expand Down

0 comments on commit e5adbee

Please sign in to comment.