2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newSrcCommand() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
2021-02-22 10:37:00 +03:00
|
|
|
Use: "src [@HANDLE]",
|
|
|
|
RunE: execSrc,
|
2022-12-25 07:04:18 +03:00
|
|
|
Example: ` # Get active data source
|
|
|
|
$ sq src
|
2021-02-22 10:37:00 +03:00
|
|
|
|
2022-12-25 07:04:18 +03:00
|
|
|
# Set @my1 as active data source
|
|
|
|
$ sq src @my1`,
|
2021-02-22 10:37:00 +03:00
|
|
|
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.`,
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
2023-05-05 17:32:50 +03:00
|
|
|
cmd.Flags().Bool(flag.Pretty, true, flag.PrettyUsage)
|
|
|
|
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
|
2023-01-01 06:17:44 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
return cmd
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +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
|
2023-04-19 08:28:09 +03:00
|
|
|
src := cfg.Collection.Active()
|
2020-08-06 20:58:47 +03:00
|
|
|
if src == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
return rc.writers.srcw.Source(cfg.Collection, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +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
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
return rc.writers.srcw.Source(cfg.Collection, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|