sq/cli/cmd_src.go

57 lines
1.3 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package cli
import (
"github.com/neilotoole/sq/cli/flag"
2020-08-06 20:58:47 +03:00
"github.com/spf13/cobra"
)
func newSrcCommand() *cobra.Command {
2020-08-06 20:58:47 +03:00
cmd := &cobra.Command{
Use: "src [@HANDLE]",
RunE: execSrc,
Example: ` # Get active data source
$ sq src
# Set @my1 as active data source
$ sq src @my1`,
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: completeHandle(1),
Short: "Get or set active data source",
2020-08-06 20:58:47 +03:00
Long: `Get or set active data source. If no argument provided, get the active data
source. Otherwise, set @HANDLE as the active data source.`,
}
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().Bool(flag.Pretty, true, flag.PrettyUsage)
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
return cmd
2020-08-06 20:58:47 +03:00
}
func execSrc(cmd *cobra.Command, args []string) error {
rc := RunContextFrom(cmd.Context())
2020-08-06 20:58:47 +03:00
cfg := rc.Config
if len(args) == 0 {
// Get the active data source
src := cfg.Collection.Active()
2020-08-06 20:58:47 +03:00
if src == nil {
return nil
}
return rc.writers.srcw.Source(cfg.Collection, src)
2020-08-06 20:58:47 +03:00
}
src, err := cfg.Collection.SetActive(args[0], false)
2020-08-06 20:58:47 +03:00
if err != nil {
return err
}
2023-04-22 16:37:07 +03:00
err = rc.ConfigStore.Save(cmd.Context(), cfg)
2020-08-06 20:58:47 +03:00
if err != nil {
return err
}
return rc.writers.srcw.Source(cfg.Collection, src)
2020-08-06 20:58:47 +03:00
}