sq/cli/cmd_scratch.go
Neil O'Toole 98b47a2666
#199 - Config, refactoring (#204)
* refactor: moved cli flags to pkg cli/flag

* testh: add OptLongDB for long-running tests

* implement 'sq config dir'

* legacy dir migration: probably a bad idea

* cleanup

* Refactored SQ_CONFIG and --config

* added yaml writer

* Dialing in tests

* YAML output for 'sq driver ls'

* Significant refactoring of config

* Minor test for ioz

* Rename source.Set to source.Collection

* Cleaning up references to source.Set
2023-04-18 23:28:09 -06:00

85 lines
2.3 KiB
Go

package cli
import (
"github.com/neilotoole/sq/drivers/sqlite3"
"github.com/neilotoole/sq/libsq/source"
"github.com/spf13/cobra"
)
// TODO: dump all this "internal" stuff: make the options as follows: @HANDLE, file, memory
func newScratchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "scratch [@HANDLE|internal|internal:file|internal:mem|@scratch]",
// This command is likely to be ditched in favor of a generalized "config" cmd
// such as "sq config scratchdb=@my1"
Hidden: true,
Args: cobra.ExactArgs(1),
RunE: execScratch,
Example: ` # get scratch data source
$ sq scratch
# set @my1 as scratch data source
$ sq scratch @my1
# use the default embedded db
$ sq scratch internal
# explicitly specify use of embedded file db
$ sq scratch internal:file
# explicitly specify use of embedded memory db
$ sq scratch internal:mem
# restore default scratch db (equivalent to "internal")
$ sq scratch @scratch`,
Short: "Get or set scratch data source",
Long: `Get or set scratch data source. The scratch db is used internally by sq for multiple purposes such as
importing non-SQL data, or cross-database joins. If no argument provided, get the current scratch data
source. Otherwise, set @HANDLE or an internal db as the scratch data source. The reserved handle "@scratch" resets the
`,
}
return cmd
}
func execScratch(cmd *cobra.Command, args []string) error {
rc := RunContextFrom(cmd.Context())
cfg := rc.Config
var src *source.Source
var err error
defaultScratch := &source.Source{
Handle: source.ScratchHandle,
Location: "internal:file",
Type: sqlite3.Type,
}
if len(args) == 0 {
// Print the scratch src
src = cfg.Collection.Scratch()
if src == nil {
src = defaultScratch
}
return rc.writers.srcw.Source(cfg.Collection, src)
}
// Collection the scratch src
switch args[0] {
case "internal", "internal:file", "internal:mem":
// TODO: currently only supports file sqlite3 db, fairly trivial to do mem as well
_, _ = cfg.Collection.SetScratch("")
src = defaultScratch
default:
src, err = cfg.Collection.SetScratch(args[0])
if err != nil {
return err
}
}
err = rc.ConfigStore.Save(rc.Config)
if err != nil {
return err
}
return rc.writers.srcw.Source(cfg.Collection, src)
}