Skip to content

Commit

Permalink
add clone command, tags, dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
MadridianFox committed Aug 28, 2022
1 parent f9c0413 commit cedce02
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 133 deletions.
225 changes: 127 additions & 98 deletions actions/component_actions.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package actions

import (
"errors"
"fmt"
"github.com/madridianfox/elc/core"
"path"
)

func resolveCompNames(ws *core.Workspace, options *core.GlobalOptions, namesFromArgs []string) ([]string, error) {
var compNames []string

if options.Tag != "" {
compNames = ws.FindComponentNamesByTag(options.Tag)
if len(compNames) == 0 {
return nil, errors.New(fmt.Sprintf("components with tag %s not found", options.Tag))
}
} else if options.ComponentName != "" {
compNames = []string{options.ComponentName}
} else if len(namesFromArgs) > 0 {
compNames = namesFromArgs
} else {
currentCompName, err := ws.ComponentNameByPath()
if err != nil {
return nil, err
}
compNames = []string{currentCompName}
}

return compNames, nil
}

func StartServiceAction(options *core.GlobalOptions, svcNames []string) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
}

if options.ComponentName != "" {
svcNames = append(svcNames, options.ComponentName)
compNames, err := resolveCompNames(ws, options, svcNames)
if err != nil {
return err
}

if len(svcNames) > 0 {
for _, svcName := range svcNames {
comp, err := ws.ComponentByName(svcName)
if err != nil {
return err
}

err = comp.Start(options)
if err != nil {
return err
}
}
} else {
comp, err := ws.ComponentByPath()
for _, compName := range compNames {
comp, err := ws.ComponentByName(compName)
if err != nil {
return err
}
Expand All @@ -49,35 +62,22 @@ func StopServiceAction(stopAll bool, svcNames []string, destroy bool, options *c
return err
}

if options.ComponentName != "" {
svcNames = append(svcNames, options.ComponentName)
}
var compNames []string

if stopAll {
svcNames = ws.GetComponentNames()
}

if len(svcNames) > 0 {
for _, svcName := range svcNames {
comp, err := ws.ComponentByName(svcName)
if err != nil {
return err
}
if destroy {
err = comp.Destroy(options)
} else {
err = comp.Stop(options)
}
if err != nil {
return err
}
}
compNames = ws.GetComponentNames()
} else {
comp, err := ws.ComponentByPath()
compNames, err = resolveCompNames(ws, options, svcNames)
if err != nil {
return err
}
}

for _, compName := range compNames {
comp, err := ws.ComponentByName(compName)
if err != nil {
return err
}
if destroy {
err = comp.Destroy(options)
} else {
Expand All @@ -97,20 +97,13 @@ func RestartServiceAction(hardRestart bool, svcNames []string, options *core.Glo
return err
}

if len(svcNames) > 0 {
for _, svcName := range svcNames {
comp, err := ws.ComponentByName(svcName)
if err != nil {
return err
}
compNames, err := resolveCompNames(ws, options, svcNames)
if err != nil {
return err
}

err = comp.Restart(hardRestart, options)
if err != nil {
return err
}
}
} else {
comp, err := ws.ComponentByPath()
for _, compName := range compNames {
comp, err := ws.ComponentByName(compName)
if err != nil {
return err
}
Expand All @@ -124,24 +117,24 @@ func RestartServiceAction(hardRestart bool, svcNames []string, options *core.Glo
return nil
}

func PrintVarsAction(svcNames []string) error {
func PrintVarsAction(options *core.GlobalOptions, svcNames []string) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
}

var comp *core.Component
compNames, err := resolveCompNames(ws, options, svcNames)
if err != nil {
return err
}

if len(svcNames) > 0 {
comp, err = ws.ComponentByName(svcNames[0])
if err != nil {
return err
}
} else {
comp, err = ws.ComponentByPath()
if err != nil {
return err
}
if len(compNames) > 1 {
return errors.New("too many components for show")
}

comp, err := ws.ComponentByName(compNames[0])
if err != nil {
return err
}

err = comp.DumpVars()
Expand All @@ -152,93 +145,108 @@ func PrintVarsAction(svcNames []string) error {
return nil
}

func ComposeCommandAction(args []string, composeParams core.GlobalOptions) error {
func ComposeCommandAction(options *core.GlobalOptions, args []string) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
}

composeParams.Cmd = args
compNames, err := resolveCompNames(ws, options, []string{})
if err != nil {
return err
}

if composeParams.ComponentName == "" {
composeParams.ComponentName, err = ws.ComponentNameByPath()
if err != nil {
return err
}
if len(compNames) > 1 {
return errors.New("too many components")
}

comp, err := ws.ComponentByName(composeParams.ComponentName)
comp, err := ws.ComponentByName(compNames[0])
if err != nil {
return err
}

_, err = comp.Compose(&composeParams)
options.Cmd = args

_, err = comp.Compose(options)
if err != nil {
return err
}

return nil
}

func WrapCommandAction(globalOptions core.GlobalOptions, command []string) error {
func WrapCommandAction(options *core.GlobalOptions, command []string) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
}

var comp *core.Component

svcName := globalOptions.ComponentName
compNames, err := resolveCompNames(ws, options, []string{})
if err != nil {
return err
}

if svcName == "" {
comp, err = ws.ComponentByPath()
} else {
comp, err = ws.ComponentByName(svcName)
if len(compNames) > 1 {
return errors.New("too many components")
}

comp, err := ws.ComponentByName(compNames[0])
if err != nil {
return err
}

var hostName string

if comp.Config.HostedIn != "" {
svcName = comp.Config.HostedIn
hostName = comp.Config.HostedIn
} else {
svcName = comp.Name
hostName = comp.Name
}

hostComp, err := ws.ComponentByName(svcName)
hostComp, err := ws.ComponentByName(hostName)
if err != nil {
return err
}

_, err = hostComp.Wrap(command)
_, err = hostComp.Wrap(command, options)
if err != nil {
return err
}

return nil
}

func ExecAction(options core.GlobalOptions) error {
func ExecAction(options *core.GlobalOptions) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
}

var comp *core.Component
compNames, err := resolveCompNames(ws, options, []string{})
if err != nil {
return err
}

if options.ComponentName == "" {
comp, err = ws.ComponentByPath()
} else {
comp, err = ws.ComponentByName(options.ComponentName)
if len(compNames) > 1 {
return errors.New("too many components")
}

comp, err := ws.ComponentByName(compNames[0])
if err != nil {
return err
}

var hostName string

if comp.Config.HostedIn != "" {
options.ComponentName = comp.Config.HostedIn
hostName = comp.Config.HostedIn
} else {
options.ComponentName = comp.Name
hostName = comp.Name
}

hostComp, err := ws.ComponentByName(hostName)
if err != nil {
return err
}

if comp.Config.ExecPath != "" {
Expand All @@ -248,12 +256,7 @@ func ExecAction(options core.GlobalOptions) error {
}
}

hostComp, err := ws.ComponentByName(options.ComponentName)
if err != nil {
return err
}

_, err = hostComp.Exec(&options)
_, err = hostComp.Exec(options)
if err != nil {
return err
}
Expand Down Expand Up @@ -287,3 +290,29 @@ func SetGitHooksAction(scriptsFolder string, elcBinary string) error {

return nil
}

func CloneComponentAction(options *core.GlobalOptions, svcNames []string) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
}

compNames, err := resolveCompNames(ws, options, svcNames)
if err != nil {
return err
}

for _, compName := range compNames {
comp, err := ws.ComponentByName(compName)
if err != nil {
return err
}

err = comp.Clone(options)
if err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit cedce02

Please sign in to comment.