From ef7d844aefc08ece794abfddce0e9494d7e41534 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Wed, 14 Aug 2024 13:38:34 +0900 Subject: [PATCH] Fixed missing interpretation of in option Fixed the trdsql package not actually specifying two of the in options. update modules. --- cmd/root.go | 39 +++++++++++++++++++++++++++++---------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 1fb4ea9..233457f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -38,14 +38,15 @@ var ( onlySQL string tableName string - inFormat string - inDelimiter string - inHeader bool - inSkip int - inPreRead int - inJQuery string - inLimitRead int - inNull nilString + inFormat string + inDelimiter string + inHeader bool + inSkip int + inPreRead int + inJQuery string + inLimitReadF bool + inLimitRead int + inNull nilString outFormat string outFile string @@ -137,6 +138,17 @@ Supports multiple databases(%s). return } + if inLimitRead > 0 { + inLimitReadF = true + inPreRead = inLimitRead + if inSkip > 0 { + inPreRead += inSkip + } + if inHeader { + inPreRead++ + } + } + if analyze != "" || onlySQL != "" { if err := analyzeSQL(writer, dbCfg, analyze, onlySQL); err != nil { log.Println(err) @@ -174,9 +186,11 @@ func run(writer io.Writer, cfg *dbConfig, args []string) error { driver, dsn := getDB(cfg, cDB, cDriver, cDSN) importer := trdsql.NewImporter( trdsql.InFormat(strToFormat(inFormat)), + trdsql.InDelimiter(inDelimiter), trdsql.InHeader(inHeader), trdsql.InSkip(inSkip), trdsql.InJQ(inJQuery), + trdsql.InLimitRead(inLimitReadF), trdsql.InNeedNULL(inNull.valid), trdsql.InNULL(inNull.str), ) @@ -320,7 +334,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is XDG_CONFIG_HOME/trdsql/config.json)") + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $XDG_CONFIG_HOME/trdsql/config.json)") rootCmd.PersistentFlags().BoolVarP(&version, "version", "v", false, "display version information.") rootCmd.PersistentFlags().BoolVarP(&usage, "help", "h", false, "display usage information.") rootCmd.PersistentFlags().BoolVar(&Debug, "debug", false, "debug print.") @@ -345,6 +359,9 @@ func init() { rootCmd.PersistentFlags().Var(&inNull, "null", "value(string) to convert to null on input.") rootCmd.PersistentFlags().StringVarP(&inFormat, "in", "i", "GUESS", "format for input. [CSV|LTSV|JSON|YAML|TBLN|WIDTH]") + rootCmd.RegisterFlagCompletionFunc("in", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return []string{"CSV", "LTSV", "JSON", "YAML", "TBLN", "WIDTH"}, cobra.ShellCompDirectiveDefault + }) rootCmd.PersistentFlags().StringVar(&outDelimiter, "out-delimiter", ",", "field delimiter for output.") rootCmd.PersistentFlags().StringVar(&outFile, "out-file", "", "output file name.") rootCmd.PersistentFlags().BoolVar(&outWithoutGuess, "out-without-guess", false, "output without guessing (when using -out).") @@ -357,7 +374,9 @@ func init() { rootCmd.PersistentFlags().Var(&outNull, "out-null", "value(string) to convert from null on output.") rootCmd.PersistentFlags().StringVarP(&outFormat, "out", "o", "GUESS", "format for output. [CSV|LTSV|JSON|JSONL|RAW|MD|AT|YAML|TBLN]") - + rootCmd.RegisterFlagCompletionFunc("out", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return []string{"CSV", "LTSV", "JSON", "JSONL", "RAW", "MD", "AT", "YAML", "TBLN"}, cobra.ShellCompDirectiveDefault + }) rootCmd.PersistentFlags().StringVarP(&completion, "completion", "", "", "generate completion script [bash|zsh|fish|powershell]") _ = viper.BindPFlag("database", rootCmd.PersistentFlags().Lookup("database")) diff --git a/go.mod b/go.mod index 69af64d..d1befa7 100644 --- a/go.mod +++ b/go.mod @@ -62,7 +62,7 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a // indirect - modernc.org/libc v1.59.1 // indirect + modernc.org/libc v1.59.3 // indirect modernc.org/mathutil v1.6.0 // indirect modernc.org/memory v1.8.0 // indirect modernc.org/strutil v1.2.0 // indirect diff --git a/go.sum b/go.sum index 2723e2d..7505001 100644 --- a/go.sum +++ b/go.sum @@ -193,8 +193,8 @@ modernc.org/gc/v2 v2.5.0 h1:bJ9ChznK1L1mUtAQtxi0wi5AtAs5jQuw4PrPHO5pb6M= modernc.org/gc/v2 v2.5.0/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU= modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a h1:CfbpOLEo2IwNzJdMvE8aiRbPMxoTpgAJeyePh0SmO8M= modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= -modernc.org/libc v1.59.1 h1:ZVsgtxNu5OyIsX0Qq6T4aN7IxP1YTmvKZuf8T5fV0Ro= -modernc.org/libc v1.59.1/go.mod h1:EY/egGEU7Ju66eU6SBqCNYaFUDuc4npICkMWnU5EE3A= +modernc.org/libc v1.59.3 h1:A4QAp1lRSn2/b4aU+wBtq+yeKgq/2BUevrj0p1ZNy2M= +modernc.org/libc v1.59.3/go.mod h1:EY/egGEU7Ju66eU6SBqCNYaFUDuc4npICkMWnU5EE3A= modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=