Switched more flags over to options mechanism (#394)

* Switched more flags over to options mechanism

* flag.IngestHeader

* flag.CSVEmptyAsNull and flag.CSVDelim

* flag.PingTimeout

* more flags

* flag.LogEnabled

* logging flags
This commit is contained in:
Neil O'Toole 2024-02-09 11:54:37 -07:00 committed by GitHub
parent 1adabaa5ed
commit fabc46c758
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 91 additions and 108 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source"
"github.com/neilotoole/sq/libsq/source/drivertype"
"github.com/neilotoole/sq/libsq/source/location"
@ -159,7 +160,7 @@ More examples:
cmdMarkRequiresConfigLock(cmd)
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().StringP(flag.AddDriver, flag.AddDriverShort, "", flag.AddDriverUsage)
@ -170,11 +171,13 @@ More examples:
cmd.Flags().Bool(flag.SkipVerify, false, flag.SkipVerifyUsage)
cmd.Flags().BoolP(flag.AddActive, flag.AddActiveShort, false, flag.AddActiveUsage)
cmd.Flags().Bool(flag.IngestHeader, false, flag.IngestHeaderUsage)
cmd.Flags().Bool(flag.CSVEmptyAsNull, true, flag.CSVEmptyAsNullUsage)
cmd.Flags().String(flag.CSVDelim, flag.CSVDelimDefault, flag.CSVDelimUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.CSVDelim, completeStrings(-1, csv.NamedDelims()...)))
addOptionFlag(cmd.Flags(), driver.OptIngestHeader)
// cmd.Flags().Bool(flag.IngestHeader, false, flag.IngestHeaderUsage)
addOptionFlag(cmd.Flags(), csv.OptEmptyAsNull)
addOptionFlag(cmd.Flags(), csv.OptDelim)
// cmd.Flags().Bool(flag.CSVEmptyAsNull, true, flag.CSVEmptyAsNullUsage)
// cmd.Flags().String(flag.CSVDelim, flag.CSVDelimDefault, flag.CSVDelimUsage)
panicOn(cmd.RegisterFlagCompletionFunc(csv.OptDelim.Flag(), completeStrings(-1, csv.NamedDelims()...)))
return cmd
}

View File

@ -32,7 +32,7 @@ just for that source.`,
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().String(flag.ConfigSrc, "", flag.ConfigSrcUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.ConfigSrc, completeHandle(1, true)))

View File

@ -40,7 +40,7 @@ just for that source.`,
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().String(flag.ConfigSrc, "", flag.ConfigSrcUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.ConfigSrc, completeHandle(1, true)))

View File

@ -44,7 +44,7 @@ Use "sq config ls -v" to list available options.`,
cmdMarkRequiresConfigLock(cmd)
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().String(flag.ConfigSrc, "", flag.ConfigSrcUsage)

View File

@ -99,7 +99,7 @@ func execDBDumpCatalog(cmd *cobra.Command, args []string) error {
var (
errPrefix = fmt.Sprintf("db dump catalog: %s", src.Handle)
dumpVerbose = cmdFlagBool(cmd, flag.Verbose)
dumpVerbose = OptVerbose.Get(src.Options)
dumpNoOwner = cmdFlagBool(cmd, flag.DBDumpNoOwner)
dumpLongFlags = cmdFlagBool(cmd, flag.DBPrintLongToolCmd)
dumpFile string
@ -211,7 +211,7 @@ func execDBDumpCluster(cmd *cobra.Command, args []string) error {
var (
errPrefix = fmt.Sprintf("db dump cluster: %s", src.Handle)
dumpVerbose = cmdFlagBool(cmd, flag.Verbose)
dumpVerbose = OptVerbose.Get(src.Options)
dumpNoOwner = cmdFlagBool(cmd, flag.DBDumpNoOwner)
dumpLongFlags = cmdFlagBool(cmd, flag.DBPrintLongToolCmd)
dumpFile string

View File

@ -83,8 +83,6 @@ func execDBExec(cmd *cobra.Command, args []string) error {
// scriptString is the optional SQL command string.
// If empty, scriptFile or stdin is used.
cmdString string
verbose = cmdFlagBool(cmd, flag.Verbose)
)
if src, err = getCmdSource(cmd, args); err != nil {
@ -113,7 +111,7 @@ func execDBExec(cmd *cobra.Command, args []string) error {
switch src.Type { //nolint:exhaustive
case drivertype.Pg:
params := &postgres.ExecToolParams{
Verbose: verbose,
Verbose: OptVerbose.Get(src.Options),
ScriptFile: scriptFile,
CmdString: cmdString,
LongFlags: cmdFlagChanged(cmd, flag.DBPrintLongToolCmd),

View File

@ -100,7 +100,7 @@ func execDBRestoreCatalog(cmd *cobra.Command, args []string) error {
return err
}
verbose := cmdFlagBool(cmd, flag.Verbose)
verbose := OptVerbose.Get(src.Options)
noOwner := cmdFlagBool(cmd, flag.DBRestoreNoOwner)
var execCmd *execz.Cmd
@ -213,7 +213,7 @@ func execDBRestoreCluster(cmd *cobra.Command, args []string) error {
return err
}
verbose := cmdFlagBool(cmd, flag.Verbose)
verbose := OptVerbose.Get(src.Options)
// FIXME: get rid of noOwner from this command?
// noOwner := cmdFlagBool(cmd, flag.RestoreNoOwner)

View File

@ -35,7 +35,7 @@ func newDriverListCmd() *cobra.Command {
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
return cmd
}

View File

@ -36,7 +36,7 @@ Use 'sq ls -g' to list groups.`,
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
return cmd

View File

@ -90,7 +90,7 @@ formats both show extensive detail.`,
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().BoolP(flag.InspectOverview, flag.InspectOverviewShort, false, flag.InspectOverviewUsage)
@ -221,7 +221,7 @@ func execInspect(cmd *cobra.Command, args []string) error {
// This is a bit hacky, but it works... if not "--verbose", then just zap
// the DBVars, as we usually don't want to see those
if !cmdFlagIsSetTrue(cmd, flag.Verbose) {
if !OptVerbose.Get(src.Options) {
srcMeta.DBProperties = nil
}

View File

@ -43,7 +43,7 @@ any further descendants.
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
cmd.Flags().BoolP(flag.ListGroup, flag.ListGroupShort, false, flag.ListGroupUsage)

View File

@ -45,7 +45,7 @@ source handles are files, and groups are directories.`,
cmdMarkRequiresConfigLock(cmd)
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
return cmd

View File

@ -68,9 +68,10 @@ The exit code is 1 if ping fails for any of the sources.`,
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.CSV, flag.CSVShort, false, flag.CSVUsage)
cmd.Flags().Bool(flag.TSV, false, flag.TSVUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().Duration(flag.PingTimeout, time.Second*10, flag.PingTimeoutUsage)
addOptionFlag(cmd.Flags(), OptPingCmdTimeout)
// cmd.Flags().Duration(flag.PingTimeout, time.Second*10, flag.PingTimeoutUsage)
return cmd
}

View File

@ -37,7 +37,7 @@ may have changed, if that source or group was removed.`,
cmdMarkRequiresConfigLock(cmd)
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
return cmd
}

View File

@ -96,24 +96,27 @@ See docs and more: https://sq.io`,
cmd.PersistentFlags().Bool(flag.Help, false, "Show help")
addQueryCmdFlags(cmd)
cmd.Flags().Bool(flag.Version, false, flag.VersionUsage)
cmd.PersistentFlags().BoolP(flag.Monochrome, flag.MonochromeShort, false, flag.MonochromeUsage)
addOptionFlag(cmd.PersistentFlags(), OptMonochrome)
addOptionFlag(cmd.PersistentFlags(), OptProgress)
addOptionFlag(cmd.PersistentFlags(), OptRedact)
addOptionFlag(cmd.PersistentFlags(), OptVerbose)
// TODO: Move the rest of the option flags over to addOptionFlag
cmd.PersistentFlags().BoolP(flag.Verbose, flag.VerboseShort, false, flag.VerboseUsage)
// flag.Config can't use the option flag mechanism, because... well,
// because it's the config flag, and it exists above the realm of
// options. It's the flag that tells us where to find the config file,
// thus it can't be an option stored in the config file.
cmd.PersistentFlags().String(flag.Config, "", flag.ConfigUsage)
cmd.PersistentFlags().Bool(flag.LogEnabled, false, flag.LogEnabledUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.LogEnabled, completeBool))
cmd.PersistentFlags().String(flag.LogFile, "", flag.LogFileUsage)
addOptionFlag(cmd.PersistentFlags(), OptLogEnabled)
panicOn(cmd.RegisterFlagCompletionFunc(OptLogEnabled.Flag(), completeBool))
cmd.PersistentFlags().String(flag.LogLevel, "", flag.LogLevelUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.LogLevel, completeStrings(
addOptionFlag(cmd.PersistentFlags(), OptLogFile)
addOptionFlag(cmd.PersistentFlags(), OptLogLevel)
panicOn(cmd.RegisterFlagCompletionFunc(OptLogLevel.Flag(), completeStrings(
1,
slog.LevelDebug.String(),
slog.LevelInfo.String(),
@ -121,11 +124,12 @@ See docs and more: https://sq.io`,
slog.LevelError.String(),
)))
cmd.PersistentFlags().String(flag.LogFormat, "", flag.LogFormatUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.LogFormat, completeStrings(
addOptionFlag(cmd.PersistentFlags(), OptLogFormat)
panicOn(cmd.RegisterFlagCompletionFunc(OptLogFormat.Flag(), completeStrings(
1,
string(format.Text),
string(format.JSON),
)))
return cmd
}

View File

@ -307,6 +307,15 @@ func preprocessUserSLQ(ctx context.Context, ru *run.Run, args []string) (string,
// addTextFormatFlags adds the flags for --text format.
func addTextFormatFlags(cmd *cobra.Command) {
cmd.Flags().BoolP(flag.Text, flag.TextShort, false, flag.TextUsage)
// Note that we don't use:
//
// addOptionFlag(cmd.Flags(), OptPrintHeader)
//
// This is because, unlike other flags, printing the header is controllable
// via both --header (-h) and --no-header (-H). At the time of implementation,
// it seemed the ergonomics were better when both -h and -H were available.
// So, it'll probably stay this way.
cmd.Flags().BoolP(flag.Header, flag.HeaderShort, true, flag.HeaderUsage)
cmd.Flags().BoolP(flag.NoHeader, flag.NoHeaderShort, false, flag.NoHeaderUsage)
cmd.MarkFlagsMutuallyExclusive(flag.Header, flag.NoHeader)
@ -347,11 +356,11 @@ func addQueryCmdFlags(cmd *cobra.Command) {
cmd.Flags().String(flag.IngestDriver, "", flag.IngestDriverUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.IngestDriver, completeDriverType))
cmd.Flags().Bool(flag.IngestHeader, false, flag.IngestHeaderUsage)
addOptionFlag(cmd.Flags(), driver.OptIngestHeader)
addOptionFlag(cmd.Flags(), driver.OptIngestCache)
cmd.Flags().Bool(flag.CSVEmptyAsNull, true, flag.CSVEmptyAsNullUsage)
cmd.Flags().String(flag.CSVDelim, flag.CSVDelimDefault, flag.CSVDelimUsage)
panicOn(cmd.RegisterFlagCompletionFunc(flag.CSVDelim, completeStrings(-1, csv.NamedDelims()...)))
addOptionFlag(cmd.Flags(), csv.OptDelim)
panicOn(cmd.RegisterFlagCompletionFunc(csv.OptDelim.Key(), completeStrings(-1, csv.NamedDelims()...)))
addOptionFlag(cmd.Flags(), csv.OptEmptyAsNull)
}
// addResultFormatFlags adds the individual flags that control result
@ -373,7 +382,7 @@ func addResultFormatFlags(cmd *cobra.Command) {
cmd.MarkFlagsMutuallyExclusive(flag.OutputFormatFlags...)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
}
// extractFlagArgsValues returns a map {key:value} of predefined variables

View File

@ -10,10 +10,10 @@ import (
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/cli/flag"
"github.com/neilotoole/sq/cli/testrun"
"github.com/neilotoole/sq/drivers/userdriver"
"github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source"
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/proj"
@ -122,6 +122,8 @@ func TestCmdSQL_SelectFromUserDriver(t *testing.T) {
func TestCmdSQL_StdinQuery(t *testing.T) {
t.Parallel()
flagIngestHeader := driver.OptIngestHeader.Flag()
testCases := []struct {
fpath string
tbl string
@ -131,25 +133,25 @@ func TestCmdSQL_StdinQuery(t *testing.T) {
}{
{
fpath: proj.Abs(sakila.PathCSVActorNoHeader),
flags: map[string]string{flag.IngestHeader: "false"},
flags: map[string]string{flagIngestHeader: "false"},
tbl: source.MonotableName,
wantCount: sakila.TblActorCount,
},
{
fpath: proj.Abs(sakila.PathCSVActor),
flags: map[string]string{flag.IngestHeader: "true"},
flags: map[string]string{flagIngestHeader: "true"},
tbl: source.MonotableName,
wantCount: sakila.TblActorCount,
},
{
fpath: proj.Abs(sakila.PathXLSXActorHeader),
flags: map[string]string{flag.IngestHeader: "true"},
flags: map[string]string{flagIngestHeader: "true"},
tbl: sakila.TblActor,
wantCount: sakila.TblActorCount,
},
{
fpath: proj.Abs(sakila.PathXLSXSubset),
flags: map[string]string{flag.IngestHeader: "true"},
flags: map[string]string{flagIngestHeader: "true"},
tbl: sakila.TblActor,
wantCount: sakila.TblActorCount,
},

View File

@ -25,7 +25,7 @@ source. Otherwise, set @HANDLE as the active data source.`,
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
return cmd

View File

@ -58,7 +58,7 @@ func newTblCopyCmd() *cobra.Command {
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().Bool(flag.TblData, true, flag.TblDataUsage)
return cmd

View File

@ -70,7 +70,7 @@ Before upgrading, check the changelog: https://sq.io/changelog`,
addTextFormatFlags(cmd)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
addOptionFlag(cmd.Flags(), OptCompact)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
return cmd

View File

@ -1,4 +1,5 @@
// Package flag holds CLI flags.
// Package flag holds CLI flags. Note that many CLI flags are defined
// via the options package, and are not listed here.
package flag
const (
@ -28,7 +29,6 @@ const (
Header = "header"
HeaderShort = "h"
HeaderUsage = "Print header row"
NoHeader = "no-header"
NoHeaderShort = "H"
NoHeaderUsage = "Don't print header row"
@ -63,10 +63,6 @@ const (
AddActiveShort = "a"
AddActiveUsage = "Make this the active source"
Monochrome = "monochrome"
MonochromeShort = "M"
MonochromeUsage = "Don't colorize output"
FileOutput = "output"
FileOutputShort = "o"
FileOutputUsage = "Write output to <file> instead of stdout"
@ -92,10 +88,6 @@ const (
CacheTreeSizeShort = "s"
CacheTreeSizeUsage = "Show sizes in cache tree"
Compact = "compact"
CompactShort = "c"
CompactUsage = "Compact instead of pretty-printed output"
Raw = "raw"
RawShort = "r"
RawUsage = "Output each record field in raw format without any encoding or delimiter"
@ -116,13 +108,6 @@ const (
TblData = "data"
TblDataUsage = "Copy table data"
PingTimeout = "timeout"
PingTimeoutUsage = "Max time to wait for ping"
Verbose = "verbose"
VerboseShort = "v"
VerboseUsage = "Verbose output"
Version = "version"
VersionUsage = "Print version info"
@ -146,16 +131,6 @@ const (
Config = "config"
ConfigUsage = "Load config from here"
IngestHeader = "ingest.header"
IngestHeaderUsage = "Treat first row of ingest data as header"
CSVEmptyAsNull = "driver.csv.empty-as-null"
CSVEmptyAsNullUsage = "Treat empty CSV fields as null"
CSVDelim = "driver.csv.delim"
CSVDelimUsage = "CSV delimiter: one of comma, space, pipe, tab, colon, semi, period"
CSVDelimDefault = "comma"
ConfigDelete = "delete"
ConfigDeleteShort = "D"
ConfigDeleteUsage = "Reset this option to default value"
@ -172,18 +147,6 @@ const (
InspectSchemataShort = "S"
InspectSchemataUsage = "List schemas (in current catalog) only"
LogEnabled = "log"
LogEnabledUsage = "Enable logging"
LogFile = "log.file"
LogFileUsage = "Path to log file; empty disables logging"
LogLevel = "log.level"
LogLevelUsage = "Log level: one of DEBUG, INFO, WARN, ERROR"
LogFormat = "log.format"
LogFormatUsage = `Log format: one of "text" or "json"`
DiffOverview = "overview"
DiffOverviewShort = "O"
DiffOverviewUsage = "Compare source overview"

View File

@ -14,7 +14,6 @@ import (
"github.com/spf13/cobra"
"github.com/neilotoole/sq/cli/config"
"github.com/neilotoole/sq/cli/flag"
"github.com/neilotoole/sq/cli/output/format"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/ioz/httpz"
@ -217,18 +216,19 @@ func getLogEnabled(ctx context.Context, osArgs []string, cfg *config.Config) boo
bootLog := lg.FromContext(ctx)
var enabled bool
val, ok, err := getBootstrapFlagValue(flag.LogEnabled, "", flag.LogEnabledUsage, osArgs)
flg := OptLogEnabled.Flag()
val, ok, err := getBootstrapFlagValue(flg, "", OptLogEnabled.Usage(), osArgs)
if err != nil {
bootLog.Warn("Reading log 'enabled' from flag", lga.Flag, flag.LogEnabled, lga.Err, err)
bootLog.Warn("Reading log 'enabled' from flag", lga.Flag, flg, lga.Err, err)
}
if ok {
bootLog.Debug("Using log 'enabled' specified via flag", lga.Flag, flag.LogEnabled, lga.Val, val)
bootLog.Debug("Using log 'enabled' specified via flag", lga.Flag, flg, lga.Val, val)
enabled, err = stringz.ParseBool(val)
if err != nil {
bootLog.Error(
"Reading bool flag",
lga.Flag, flag.LogEnabled,
lga.Flag, flg,
lga.Val, val,
)
// When in doubt, enable logging?
@ -275,19 +275,20 @@ func getLogEnabled(ctx context.Context, osArgs []string, cfg *config.Config) boo
func getLogLevel(ctx context.Context, osArgs []string, cfg *config.Config) slog.Level {
bootLog := lg.FromContext(ctx)
val, ok, err := getBootstrapFlagValue(flag.LogLevel, "", flag.LogLevelUsage, osArgs)
flg := OptLogLevel.Flag()
val, ok, err := getBootstrapFlagValue(flg, "", OptLogLevel.Usage(), osArgs)
if err != nil {
bootLog.Warn("Reading log level from flag", lga.Flag, flag.LogLevel, lga.Err, err)
bootLog.Warn("Reading log level from flag", lga.Flag, flg, lga.Err, err)
}
if ok {
bootLog.Debug("Using log level specified via flag", lga.Flag, flag.LogLevel, lga.Val, val)
bootLog.Debug("Using log level specified via flag", lga.Flag, flg, lga.Val, val)
lvl := new(slog.Level)
if err = lvl.UnmarshalText([]byte(val)); err == nil {
return *lvl
}
bootLog.Error("Invalid log level specified via flag",
lga.Flag, flag.LogLevel, lga.Val, val, lga.Err, err)
lga.Flag, flg, lga.Val, val, lga.Err, err)
}
val, ok = os.LookupEnv(config.EnvarLogLevel)
@ -319,12 +320,13 @@ func getLogLevel(ctx context.Context, osArgs []string, cfg *config.Config) slog.
func getLogFormat(ctx context.Context, osArgs []string, cfg *config.Config) format.Format {
bootLog := lg.FromContext(ctx)
val, ok, err := getBootstrapFlagValue(flag.LogFormat, "", flag.LogFormatUsage, osArgs)
flg := OptLogFormat.Flag()
val, ok, err := getBootstrapFlagValue(flg, "", OptLogFormat.Usage(), osArgs)
if err != nil {
bootLog.Warn("Error reading log format from flag", lga.Flag, flag.LogFormat, lga.Err, err)
bootLog.Warn("Error reading log format from flag", lga.Flag, flg, lga.Err, err)
}
if ok {
bootLog.Debug("Using log format specified via flag", lga.Flag, flag.LogFormat, lga.Val, val)
bootLog.Debug("Using log format specified via flag", lga.Flag, flg, lga.Val, val)
f := new(format.Format)
if err = f.UnmarshalText([]byte(val)); err == nil {
@ -335,7 +337,7 @@ func getLogFormat(ctx context.Context, osArgs []string, cfg *config.Config) form
}
}
bootLog.Error("Invalid log format specified via flag",
lga.Flag, flag.LogFormat, lga.Val, val, lga.Err, err)
lga.Flag, flg, lga.Val, val, lga.Err, err)
}
val, ok = os.LookupEnv(config.EnvarLogFormat)
@ -371,12 +373,13 @@ func getLogFormat(ctx context.Context, osArgs []string, cfg *config.Config) form
func getLogFilePath(ctx context.Context, osArgs []string, cfg *config.Config) string {
bootLog := lg.FromContext(ctx)
fp, ok, err := getBootstrapFlagValue(flag.LogFile, "", flag.LogFileUsage, osArgs)
flg := OptLogFile.Flag()
fp, ok, err := getBootstrapFlagValue(flg, "", OptLogFile.Usage(), osArgs)
if err != nil {
bootLog.Warn("Reading log file from flag", lga.Flag, flag.LogFile, lga.Err, err)
bootLog.Warn("Reading log file from flag", lga.Flag, flg, lga.Err, err)
}
if ok {
bootLog.Debug("Log file specified via flag", lga.Flag, flag.LogFile, lga.Path, fp)
bootLog.Debug("Log file specified via flag", lga.Flag, flg, lga.Path, fp)
return fp
}