Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing interpretation of in option #289

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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.")
Expand All @@ -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).")
Expand All @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down