2023-03-29 12:58:09 +00:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/flags"
|
2023-07-26 11:17:09 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-03-29 12:58:09 +00:00
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
2023-05-22 14:40:50 +00:00
|
|
|
const envProgressFormat = "DATABRICKS_CLI_PROGRESS_FORMAT"
|
2023-03-29 12:58:09 +00:00
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
type progressLoggerFlag struct {
|
|
|
|
flags.ProgressLogFormat
|
|
|
|
|
|
|
|
log *logFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *progressLoggerFlag) resolveModeDefault(format flags.ProgressLogFormat) flags.ProgressLogFormat {
|
|
|
|
if (f.log.level.String() == "disabled" || f.log.file.String() != "stderr") &&
|
2023-03-29 12:58:09 +00:00
|
|
|
term.IsTerminal(int(os.Stderr.Fd())) {
|
|
|
|
return flags.ModeInplace
|
|
|
|
}
|
|
|
|
return flags.ModeAppend
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
func (f *progressLoggerFlag) initializeContext(ctx context.Context) (context.Context, error) {
|
2024-08-12 14:00:20 +00:00
|
|
|
// No need to initialize the logger if it's already set in the context. This
|
|
|
|
// happens in unit tests where the logger is setup as a fixture.
|
|
|
|
if _, ok := cmdio.FromContext(ctx); ok {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
if f.log.level.String() != "disabled" && f.log.file.String() == "stderr" &&
|
|
|
|
f.ProgressLogFormat == flags.ModeInplace {
|
2023-03-29 12:58:09 +00:00
|
|
|
return nil, fmt.Errorf("inplace progress logging cannot be used when log-file is stderr")
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
format := f.ProgressLogFormat
|
2023-03-29 12:58:09 +00:00
|
|
|
if format == flags.ModeDefault {
|
2023-07-26 11:17:09 +00:00
|
|
|
format = f.resolveModeDefault(format)
|
2023-03-29 12:58:09 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
func initProgressLoggerFlag(cmd *cobra.Command, logFlags *logFlags) *progressLoggerFlag {
|
|
|
|
f := progressLoggerFlag{
|
|
|
|
ProgressLogFormat: flags.NewProgressLogFormat(),
|
|
|
|
|
|
|
|
log: logFlags,
|
|
|
|
}
|
2023-03-29 12:58:09 +00:00
|
|
|
|
|
|
|
// Configure defaults from environment, if applicable.
|
|
|
|
// If the provided value is invalid it is ignored.
|
2023-09-11 08:18:43 +00:00
|
|
|
if v, ok := env.Lookup(cmd.Context(), envProgressFormat); ok {
|
2023-07-26 11:17:09 +00:00
|
|
|
f.Set(v)
|
2023-03-29 12:58:09 +00:00
|
|
|
}
|
2023-07-26 11:17:09 +00:00
|
|
|
|
2023-11-08 08:29:22 +00:00
|
|
|
flags := cmd.PersistentFlags()
|
|
|
|
flags.Var(&f.ProgressLogFormat, "progress-format", "format for progress logs (append, inplace, json)")
|
|
|
|
flags.MarkHidden("progress-format")
|
2023-07-26 11:17:09 +00:00
|
|
|
cmd.RegisterFlagCompletionFunc("progress-format", f.ProgressLogFormat.Complete)
|
|
|
|
return &f
|
2023-03-29 12:58:09 +00:00
|
|
|
}
|