From fabc46c758a4322c7d551e4e162cbb3b0c0c55d7 Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Fri, 9 Feb 2024 11:54:37 -0700 Subject: [PATCH] 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 --- cli/cmd_add.go | 15 ++++++++------ cli/cmd_config_get.go | 2 +- cli/cmd_config_ls.go | 2 +- cli/cmd_config_set.go | 2 +- cli/cmd_db_dump.go | 4 ++-- cli/cmd_db_exec.go | 4 +--- cli/cmd_db_restore.go | 4 ++-- cli/cmd_driver.go | 2 +- cli/cmd_group.go | 2 +- cli/cmd_inspect.go | 4 ++-- cli/cmd_ls.go | 2 +- cli/cmd_mv.go | 2 +- cli/cmd_ping.go | 5 +++-- cli/cmd_remove.go | 2 +- cli/cmd_root.go | 28 +++++++++++++++----------- cli/cmd_slq.go | 19 ++++++++++++----- cli/cmd_sql_test.go | 12 ++++++----- cli/cmd_src.go | 2 +- cli/cmd_tbl.go | 2 +- cli/cmd_version.go | 2 +- cli/flag/flag.go | 47 +++++-------------------------------------- cli/logging.go | 35 +++++++++++++++++--------------- 22 files changed, 91 insertions(+), 108 deletions(-) diff --git a/cli/cmd_add.go b/cli/cmd_add.go index 51ed2733..a29be5de 100644 --- a/cli/cmd_add.go +++ b/cli/cmd_add.go @@ -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 } diff --git a/cli/cmd_config_get.go b/cli/cmd_config_get.go index 3380d691..143e4c56 100644 --- a/cli/cmd_config_get.go +++ b/cli/cmd_config_get.go @@ -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))) diff --git a/cli/cmd_config_ls.go b/cli/cmd_config_ls.go index 0a5bca42..fc3d807b 100644 --- a/cli/cmd_config_ls.go +++ b/cli/cmd_config_ls.go @@ -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))) diff --git a/cli/cmd_config_set.go b/cli/cmd_config_set.go index 4dcb9852..8b34ef33 100644 --- a/cli/cmd_config_set.go +++ b/cli/cmd_config_set.go @@ -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) diff --git a/cli/cmd_db_dump.go b/cli/cmd_db_dump.go index 669ff7f4..07e51c41 100644 --- a/cli/cmd_db_dump.go +++ b/cli/cmd_db_dump.go @@ -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 diff --git a/cli/cmd_db_exec.go b/cli/cmd_db_exec.go index 06750d4d..c4cc2f0d 100644 --- a/cli/cmd_db_exec.go +++ b/cli/cmd_db_exec.go @@ -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), diff --git a/cli/cmd_db_restore.go b/cli/cmd_db_restore.go index 221d7a76..d08036ac 100644 --- a/cli/cmd_db_restore.go +++ b/cli/cmd_db_restore.go @@ -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) diff --git a/cli/cmd_driver.go b/cli/cmd_driver.go index 1797486f..472d663a 100644 --- a/cli/cmd_driver.go +++ b/cli/cmd_driver.go @@ -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 } diff --git a/cli/cmd_group.go b/cli/cmd_group.go index e88155a8..98b99f3a 100644 --- a/cli/cmd_group.go +++ b/cli/cmd_group.go @@ -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 diff --git a/cli/cmd_inspect.go b/cli/cmd_inspect.go index 77f2c270..330624d8 100644 --- a/cli/cmd_inspect.go +++ b/cli/cmd_inspect.go @@ -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 } diff --git a/cli/cmd_ls.go b/cli/cmd_ls.go index d722345d..ba3f5946 100644 --- a/cli/cmd_ls.go +++ b/cli/cmd_ls.go @@ -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) diff --git a/cli/cmd_mv.go b/cli/cmd_mv.go index 11c5061f..637cfd0f 100644 --- a/cli/cmd_mv.go +++ b/cli/cmd_mv.go @@ -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 diff --git a/cli/cmd_ping.go b/cli/cmd_ping.go index 9edc0b0d..19f0fd6c 100644 --- a/cli/cmd_ping.go +++ b/cli/cmd_ping.go @@ -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 } diff --git a/cli/cmd_remove.go b/cli/cmd_remove.go index ade3d48b..0128b30f 100644 --- a/cli/cmd_remove.go +++ b/cli/cmd_remove.go @@ -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 } diff --git a/cli/cmd_root.go b/cli/cmd_root.go index 86e87671..c558b018 100644 --- a/cli/cmd_root.go +++ b/cli/cmd_root.go @@ -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 } diff --git a/cli/cmd_slq.go b/cli/cmd_slq.go index 1fc757a8..89678511 100644 --- a/cli/cmd_slq.go +++ b/cli/cmd_slq.go @@ -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 diff --git a/cli/cmd_sql_test.go b/cli/cmd_sql_test.go index 0f611ff3..3a7ff1ee 100644 --- a/cli/cmd_sql_test.go +++ b/cli/cmd_sql_test.go @@ -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, }, diff --git a/cli/cmd_src.go b/cli/cmd_src.go index bfe80ff8..8dc46fbb 100644 --- a/cli/cmd_src.go +++ b/cli/cmd_src.go @@ -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 diff --git a/cli/cmd_tbl.go b/cli/cmd_tbl.go index fc3bfb7f..3d7a5a3d 100644 --- a/cli/cmd_tbl.go +++ b/cli/cmd_tbl.go @@ -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 diff --git a/cli/cmd_version.go b/cli/cmd_version.go index 2e9f75e0..1432226f 100644 --- a/cli/cmd_version.go +++ b/cli/cmd_version.go @@ -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 diff --git a/cli/flag/flag.go b/cli/flag/flag.go index e6dedbbd..802c4581 100644 --- a/cli/flag/flag.go +++ b/cli/flag/flag.go @@ -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 ( @@ -25,10 +26,9 @@ const ( HTML = "html" HTMLUsage = "Output HTML table" - Header = "header" - HeaderShort = "h" - HeaderUsage = "Print header row" - + 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 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" diff --git a/cli/logging.go b/cli/logging.go index c42a2f96..075655f1 100644 --- a/cli/logging.go +++ b/cli/logging.go @@ -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 }