Skip to content

Commit

Permalink
add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Koryukov committed Dec 10, 2022
1 parent c9e83c0 commit 2057383
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
32 changes: 32 additions & 0 deletions actions/component_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions cmd/elc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func InitCobra() *cobra.Command {
NewUpdateCommand(rootCmd)
NewFixUpdateCommand(rootCmd)
NewServiceCloneCommand(rootCmd)
NewServiceListCommand(rootCmd)

return rootCmd
}
Expand Down Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions core/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2057383

Please sign in to comment.