2023-03-29 12:58:09 +00:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
"github.com/databricks/bricks/libs/cmdio"
|
2023-03-29 12:58:09 +00:00
|
|
|
"github.com/databricks/bricks/libs/flags"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
const envBricksProgressFormat = "BRICKS_PROGRESS_FORMAT"
|
|
|
|
|
|
|
|
func resolveModeDefault(format flags.ProgressLogFormat) flags.ProgressLogFormat {
|
|
|
|
if (logLevel.String() == "disabled" || logFile.String() != "stderr") &&
|
|
|
|
term.IsTerminal(int(os.Stderr.Fd())) {
|
|
|
|
return flags.ModeInplace
|
|
|
|
}
|
|
|
|
return flags.ModeAppend
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeProgressLogger(ctx context.Context) (context.Context, error) {
|
|
|
|
if logLevel.String() != "disabled" && logFile.String() == "stderr" &&
|
|
|
|
progressFormat == flags.ModeInplace {
|
|
|
|
return nil, fmt.Errorf("inplace progress logging cannot be used when log-file is stderr")
|
|
|
|
}
|
|
|
|
|
|
|
|
format := progressFormat
|
|
|
|
if format == flags.ModeDefault {
|
|
|
|
format = resolveModeDefault(format)
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
progressLogger := cmdio.NewLogger(format)
|
|
|
|
return cmdio.NewContext(ctx, progressLogger), nil
|
2023-03-29 12:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var progressFormat = flags.NewProgressLogFormat()
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Configure defaults from environment, if applicable.
|
|
|
|
// If the provided value is invalid it is ignored.
|
|
|
|
if v, ok := os.LookupEnv(envBricksProgressFormat); ok {
|
|
|
|
progressFormat.Set(v)
|
|
|
|
}
|
|
|
|
RootCmd.PersistentFlags().Var(&progressFormat, "progress-format", "format for progress logs (append, inplace, json)")
|
|
|
|
RootCmd.RegisterFlagCompletionFunc("progress-format", progressFormat.Complete)
|
|
|
|
}
|