diff --git a/actions/component_actions.go b/actions/component_actions.go index 69479b1..f32863f 100644 --- a/actions/component_actions.go +++ b/actions/component_actions.go @@ -30,6 +30,20 @@ func resolveCompNames(ws *core.Workspace, options *core.GlobalOptions, namesFrom return compNames, nil } +func ListCompNames(ws *core.Workspace, options *core.GlobalOptions) ([]string, error) { + var compNames []string + if options.Tag == "" { + return ws.GetComponentNamesList(), nil + } + + compNames = ws.FindComponentNamesByTag(options.Tag) + if len(compNames) == 0 { + return nil, errors.New(fmt.Sprintf("components with tag %s not found", options.Tag)) + } + + return compNames, nil +} + func StartServiceAction(options *core.GlobalOptions, svcNames []string) error { ws, err := core.GetWorkspaceConfig(options.WorkspaceName) if err != nil { @@ -316,3 +330,21 @@ func CloneComponentAction(options *core.GlobalOptions, svcNames []string, noHook return nil } + +func ListServicesAction(options *core.GlobalOptions) error { + ws, err := core.GetWorkspaceConfig(options.WorkspaceName) + if err != nil { + return err + } + + compNames, err := ListCompNames(ws, options) + if err != nil { + return err + } + + for _, compName := range compNames { + _, _ = core.Pc.Println(compName) + } + + return nil +} diff --git a/cmd/elc.go b/cmd/elc.go index 7600c50..b7b967d 100644 --- a/cmd/elc.go +++ b/cmd/elc.go @@ -65,6 +65,7 @@ func InitCobra() *cobra.Command { NewUpdateCommand(rootCmd) NewFixUpdateCommand(rootCmd) NewServiceCloneCommand(rootCmd) + NewServiceListCommand(rootCmd) return rootCmd } @@ -348,3 +349,16 @@ func NewServiceCloneCommand(parentCommand *cobra.Command) { command.Flags().BoolVar(&noHook, "no-hook", false, "do not execute hook script after cloning") parentCommand.AddCommand(command) } + +func NewServiceListCommand(parentCommand *cobra.Command) { + var command = &cobra.Command{ + Use: "list [OPTIONS]", + Short: "Show list of services", + Long: "Show list of services.\nCan be used in scripts for loops.", + Args: cobra.ArbitraryArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return actions.ListServicesAction(&globalOptions) + }, + } + parentCommand.AddCommand(command) +} diff --git a/core/workspace.go b/core/workspace.go index 664b8a9..715076b 100644 --- a/core/workspace.go +++ b/core/workspace.go @@ -177,3 +177,14 @@ func (ws *Workspace) FindComponentNamesByTag(tag string) []string { return result } + +func (ws *Workspace) GetComponentNamesList() []string { + result := make([]string, 0) + for name, comp := range ws.Components { + if !comp.Config.IsTemplate { + result = append(result, name) + } + } + + return result +}