sq/cli/cmd_list.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

90 lines
2.2 KiB
Go

package cli
import (
"github.com/neilotoole/sq/cli/flag"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/source"
"github.com/spf13/cobra"
)
func newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ls [GROUP]",
Short: "List sources and groups",
Long: `List data sources for active group. If GROUP is specified, list for only that group.
If --group is set, list groups instead of sources.
The source list includes all descendants of the group: direct children, and also
any further descendants.
`,
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: completeGroup(1),
RunE: execList,
Example: ` # List sources in active group
$ sq ls
# List sources in the "prod" group
$ sq ls prod
# List sources in the root group (will list all sources)
$ sq ls /
# List groups (all) instead of sources
$ sq ls -g
# Print verbose group details
$ sq ls -gv
# List subgroups in "prod" group
$ sq ls -g prod`,
}
cmd.Flags().BoolP(flag.Header, flag.HeaderShort, false, flag.HeaderUsage)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.ListGroup, flag.ListGroupShort, false, flag.ListGroupUsage)
return cmd
}
func execList(cmd *cobra.Command, args []string) error {
rc := RunContextFrom(cmd.Context())
coll := rc.Config.Collection
if cmdFlagTrue(cmd, flag.ListGroup) {
// We're listing groups, not sources.
var fromGroup string
switch len(args) {
case 0:
fromGroup = source.RootGroup
case 1:
if err := source.ValidGroup(args[0]); err != nil {
return errz.Wrapf(err, "invalid value for --%s", flag.ListGroup)
}
fromGroup = args[0]
default:
return errz.Errorf("invalid: --%s takes a max of 1 arg", flag.ListGroup)
}
tree, err := coll.Tree(fromGroup)
if err != nil {
return err
}
return rc.writers.srcw.Groups(tree)
}
// We're listing sources, not groups.
if len(args) == 1 {
// We want to list the sources in a group. To do this, we
// (temporarily) set the active group, and then continue below.
// $ sq ls prod
if err := coll.SetActiveGroup(args[0]); err != nil {
return err
}
}
return rc.writers.srcw.Collection(coll)
}