backend: Add realtime output on building phase

The realtime output could be configured through
LUET_GENERAL__SHOW_BUILD_OUTPUT environment
variable or related config option or through
`--live-output` option.
This commit is contained in:
Daniele Rondina
2021-02-01 19:10:16 +01:00
parent 2854c68209
commit c1fe3278fa
5 changed files with 108 additions and 14 deletions

View File

@@ -16,8 +16,15 @@
package backend
import (
"github.com/google/go-containerregistry/pkg/crane"
"fmt"
"os/exec"
"github.com/mudler/luet/pkg/compiler"
"github.com/mudler/luet/pkg/config"
. "github.com/mudler/luet/pkg/logger"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/pkg/errors"
)
const (
@@ -41,3 +48,45 @@ func NewBackend(s string) compiler.CompilerBackend {
}
return compilerBackend
}
func runCommand(cmd *exec.Cmd) (string, error) {
ans := ""
writer := NewBackendWriter(!config.LuetCfg.GetGeneral().ShowBuildOutput)
if config.LuetCfg.GetGeneral().ShowBuildOutput {
// We have realtime output from command. Quiet spinner.
SpinnerStop()
}
cmd.Stdout = writer
cmd.Stderr = writer
err := cmd.Start()
if err != nil {
return "", errors.Wrap(err, "Failed starting build")
}
err = cmd.Wait()
if err != nil {
return "", errors.Wrap(err, "Failed waiting for building command")
}
res := cmd.ProcessState.ExitCode()
if res != 0 {
errMsg := fmt.Sprintf("Failed building image (exiting with %d)", res)
if !config.LuetCfg.GetGeneral().ShowBuildOutput {
errMsg = fmt.Sprintf("Failed building image (exiting with %d): %s",
res, writer.GetCombinedOutput())
}
return "", errors.Wrap(err, errMsg)
}
if config.LuetCfg.GetGeneral().ShowBuildOutput {
Spinner(22)
} else {
ans = writer.GetCombinedOutput()
}
return ans, nil
}