Skip to content

Commit

Permalink
add workspace remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
MadridianFox committed Aug 28, 2022
1 parent eb11887 commit f9c0413
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 30 deletions.
11 changes: 11 additions & 0 deletions actions/workspace_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ func AddWorkspaceAction(name string, wsPath string) error {
return nil
}

func RemoveWorkspaceAction(name string) error {
hc, err := core.CheckAndLoadHC()
if err != nil {
return err
}

_, _ = core.Pc.Printf("workspace '%s' is removed\n", name)

return hc.RemoveWorkspace(name)
}

func ShowCurrentWorkspaceAction() error {
hc, err := core.CheckAndLoadHC()
if err != nil {
Expand Down
88 changes: 58 additions & 30 deletions cmd/elc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ func parseExecFlags(cmd *cobra.Command) {
func InitCobra() *cobra.Command {
globalOptions = core.GlobalOptions{}
var rootCmd = &cobra.Command{
Use: "elc",
Args: cobra.MinimumNArgs(1),
Use: "elc [command]",
Args: cobra.MinimumNArgs(0),
SilenceUsage: true,
SilenceErrors: true,
Version: core.Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
core.Pc = &core.RealPC{}
},
RunE: func(cmd *cobra.Command, args []string) error {
globalOptions.Cmd = args
return actions.ExecAction(globalOptions)
if len(args) == 0 {
return cmd.Help()
} else {
globalOptions.Cmd = args
return actions.ExecAction(globalOptions)
}
},
}

Expand Down Expand Up @@ -68,6 +72,7 @@ func NewWorkspaceCommand(parentCommand *cobra.Command) {
}
NewWorkspaceListCommand(command)
NewWorkspaceAddCommand(command)
NewWorkspaceRemoveCommand(command)
NewWorkspaceShowCommand(command)
NewWorkspaceSelectCommand(command)
parentCommand.AddCommand(command)
Expand All @@ -88,7 +93,7 @@ func NewWorkspaceListCommand(parentCommand *cobra.Command) {

func NewWorkspaceAddCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "add",
Use: "add [NAME] [PATH]",
Short: "Register new workspace",
Long: "Register new workspace.",
Args: cobra.ExactArgs(2),
Expand All @@ -102,6 +107,21 @@ func NewWorkspaceAddCommand(parentCommand *cobra.Command) {
parentCommand.AddCommand(command)
}

func NewWorkspaceRemoveCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "remove [NAME]",
Short: "Remove workspace from ~/.elc.yaml",
Long: "Remove workspace from ~/.elc.yaml.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

return actions.RemoveWorkspaceAction(name)
},
}
parentCommand.AddCommand(command)
}

func NewWorkspaceShowCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "show",
Expand All @@ -117,7 +137,7 @@ func NewWorkspaceShowCommand(parentCommand *cobra.Command) {

func NewWorkspaceSelectCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "select",
Use: "select [NAME]",
Short: "Set current workspace",
Long: "Set workspace with name NAME as current.",
Args: cobra.ExactArgs(1),
Expand All @@ -131,9 +151,9 @@ func NewWorkspaceSelectCommand(parentCommand *cobra.Command) {

func NewServiceStartCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "start",
Use: "start [OPTIONS] [NAME]",
Short: "Start one or more services",
Long: "By default starts service found with current directory, but you can pass one or more service names instead.",
Long: "Start one or more services.\nBy default starts service found with current directory, but you can pass one or more service names instead.",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.StartServiceAction(&globalOptions, args)
Expand All @@ -146,9 +166,9 @@ func NewServiceStartCommand(parentCommand *cobra.Command) {
func NewServiceStopCommand(parentCommand *cobra.Command) {
var stopAll bool
var command = &cobra.Command{
Use: "stop",
Use: "stop [OPTIONS] [NAME]",
Short: "Stop one or more services",
Long: "By default stops service found with current directory, but you can pass one or more service names instead.",
Long: "Stop one or more services.\nBy default stops service found with current directory, but you can pass one or more service names instead.",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.StopServiceAction(stopAll, args, false, &globalOptions)
Expand All @@ -161,9 +181,9 @@ func NewServiceStopCommand(parentCommand *cobra.Command) {
func NewServiceDestroyCommand(parentCommand *cobra.Command) {
var destroyAll bool
var command = &cobra.Command{
Use: "destroy",
Use: "destroy [OPTIONS] [NAME]",
Short: "Stop and remove containers of one or more services",
Long: "By default destroys service found with current directory, but you can pass one or more service names instead.",
Long: "Stop and remove containers of one or more services.\nBy default destroys service found with current directory, but you can pass one or more service names instead.",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.StopServiceAction(destroyAll, args, true, &globalOptions)
Expand All @@ -176,9 +196,9 @@ func NewServiceDestroyCommand(parentCommand *cobra.Command) {
func NewServiceRestartCommand(parentCommand *cobra.Command) {
var hardRestart bool
var command = &cobra.Command{
Use: "restart",
Use: "restart [OPTIONS] [NAME]",
Short: "Restart one or more services",
Long: "By default restart service found with current directory, but you can pass one or more service names instead.",
Long: "Restart one or more services.\nBy default restart service found with current directory, but you can pass one or more service names instead.",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.RestartServiceAction(hardRestart, args, &globalOptions)
Expand All @@ -190,9 +210,9 @@ func NewServiceRestartCommand(parentCommand *cobra.Command) {

func NewServiceVarsCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "vars",
Use: "vars [NAME]",
Short: "Print all variables computed for service",
Long: "By default uses service found with current directory, but you can pass name of another service instead.",
Long: "Print all variables computed for service.\nBy default uses service found with current directory, but you can pass name of another service instead.",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.PrintVarsAction(args)
Expand All @@ -203,12 +223,16 @@ func NewServiceVarsCommand(parentCommand *cobra.Command) {

func NewServiceComposeCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "compose",
Use: "compose [OPTIONS] [COMMAND]",
Short: "Run docker-compose command",
Long: "By default uses service found with current directory.",
Args: cobra.MinimumNArgs(1),
Long: "Run docker-compose command.\nBy default uses service found with current directory.",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return actions.ComposeCommandAction(args, globalOptions)
if len(args) == 0 {
return cmd.Help()
} else {
return actions.ComposeCommandAction(args, globalOptions)
}
},
}
command.Flags().SetInterspersed(false)
Expand All @@ -217,9 +241,9 @@ func NewServiceComposeCommand(parentCommand *cobra.Command) {

func NewServiceWrapCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "wrap",
Use: "wrap [COMMAND]",
Short: "Execute command on host with env variables for service. For module uses variables of linked service",
Long: "By default uses service/module found with current directory.",
Long: "Execute command on host with env variables for service.\nFor module uses variables of linked service.\nBy default uses service/module found with current directory.",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.WrapCommandAction(globalOptions, args)
Expand All @@ -230,13 +254,17 @@ func NewServiceWrapCommand(parentCommand *cobra.Command) {

func NewServiceExecCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "exec",
Use: "exec [OPTIONS] [COMMAND]",
Short: "Execute command in container. For module uses container of linked service",
Long: "By default uses service/module found with current directory. Starts service if it is not running.",
Args: cobra.MinimumNArgs(1),
Long: "Execute command in container. For module uses container of linked service.\nBy default uses service/module found with current directory. Starts service if it is not running.",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
globalOptions.Cmd = args
return actions.ExecAction(globalOptions)
if len(args) == 0 {
return cmd.Usage()
} else {
globalOptions.Cmd = args
return actions.ExecAction(globalOptions)
}
},
}
command.Flags().SetInterspersed(false)
Expand All @@ -247,9 +275,9 @@ func NewServiceExecCommand(parentCommand *cobra.Command) {

func NewServiceSetHooksCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "set-hooks",
Use: "set-hooks [HOOKS_DIR]",
Short: "Install hooks from specified folder to .git/hooks",
Long: "HOOKS_PATH must contain subdirectories with names as git hooks, eg. 'pre-commit'.\nOne subdirectory can contain one or many scripts with .sh extension.\nEvery script wil be wrapped with 'elc --tag=hook' command.",
Long: "Install hooks from specified folder to .git/hooks.\nHOOKS_PATH must contain subdirectories with names as git hooks, eg. 'pre-commit'.\nOne subdirectory can contain one or many scripts with .sh extension.\nEvery script wil be wrapped with 'elc --tag=hook' command.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return actions.SetGitHooksAction(args[0], os.Args[0])
Expand All @@ -263,7 +291,7 @@ func NewUpdateCommand(parentCommand *cobra.Command) {
var command = &cobra.Command{
Use: "update",
Short: "Update elc binary",
Long: "Download new version of ELC, place it to /opt/elc/ and update symlink at /usr/local/bin.",
Long: "Update elc binary.\nDownload new version of ELC, place it to /opt/elc/ and update symlink at /usr/local/bin.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.UpdateBinaryAction(version)
Expand Down
17 changes: 17 additions & 0 deletions core/home-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -60,6 +61,22 @@ func (hc *HomeConfig) AddWorkspace(name string, path string) error {
return SaveHomeConfig(hc)
}

func (hc *HomeConfig) RemoveWorkspace(name string) error {
foundWsIndex := -1
for index, ws := range hc.Workspaces {
if ws.Name == name {
foundWsIndex = index
}
}

if foundWsIndex == -1 {
return errors.New(fmt.Sprintf("Workspace %s doesn't exists", name))
}

hc.Workspaces = append(hc.Workspaces[:foundWsIndex], hc.Workspaces[foundWsIndex+1:]...)
return SaveHomeConfig(hc)
}

func (hc *HomeConfig) GetCurrentWsPath() (string, error) {
if hc.CurrentWorkspace == "" {
return "", errors.New("current workspace is not set")
Expand Down

0 comments on commit f9c0413

Please sign in to comment.