sq/cli/cmd_remove.go
Neil O'Toole ed9aa38a67
Improvements to source commands (#139)
* Expose source.Set.Data() method

* jsonw.writeJSON cleaned up

* sq add now respects --json

* Location strings are subject to more scrutiny

* Ignore .db files in project dir

* sq add is more restrictive about location string

* source.RedactedLocation now uses 'xxxxx' per stdlib url.URL.Redacted()

* Update changelog for v0.23.0

* typos
2022-12-31 20:17:44 -07:00

55 lines
1.1 KiB
Go

package cli
import (
"github.com/neilotoole/sq/libsq/source"
"github.com/samber/lo"
"github.com/spf13/cobra"
)
func newSrcRemoveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rm @HANDLE1 [@HANDLE2...]",
Example: ` # Remove @my1 data source
$ sq rm @my1
# Remove multiple data sources
$ sq rm @my1 @pg1 @sqlserver1`,
Short: "Remove data source",
Long: "Remove data source.",
Args: cobra.MinimumNArgs(1),
RunE: execSrcRemove,
ValidArgsFunction: completeHandle(0),
}
cmd.Flags().BoolP(flagJSON, flagJSONShort, false, flagJSONUsage)
return cmd
}
func execSrcRemove(cmd *cobra.Command, args []string) error {
rc := RunContextFrom(cmd.Context())
cfg := rc.Config
args = lo.Uniq(args)
srcs := make([]*source.Source, len(args))
for i := range args {
src, err := cfg.Sources.Get(args[i])
if err != nil {
return err
}
err = cfg.Sources.Remove(src.Handle)
if err != nil {
return err
}
srcs[i] = src
}
if err := rc.ConfigStore.Save(cfg); err != nil {
return err
}
return rc.writers.srcw.Removed(srcs...)
}