Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
clean profile cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 19, 2020
1 parent 979e9f1 commit b912b3c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 47 deletions.
49 changes: 32 additions & 17 deletions internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,56 @@ import (
)

var createCmd = &cobra.Command{
Use: "create",
Use: "create [name]",
Short: "Create named service selection",
Long: "Create named service selection",
Example: "dockma profile create",
Args: cobra.NoArgs,
Example: "dockma profile create my-profile",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
match, err := survey.CheckName(args[0])

if !match {
return fmt.Errorf("Given name does not match regex: %s", survey.NameRegex)
}

if err != nil {
return errors.New("Invalid input")
}
}

if len(args) > 1 {
return errors.New("Command only takes one argument")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
envHomeDir := activeEnv.GetHomeDir()

profileName := survey.InputName("Enter name for profile", "")

if activeEnv.HasProfile(profileName) {
utils.ErrorAndExit(errors.New("Profile name already taken. Use 'update' to reselect services"))
var profileName string
if len(args) == 0 {
profileName = survey.InputName("Enter name for profile", "")
} else {
profileName = args[0]
}

// FIXME use regex
if profileName == "" || profileName == "-" {
utils.ErrorAndExit(errors.New("Invalid profile name"))
if activeEnv.HasProfile(profileName) {
utils.ErrorAndExit(errors.New("Profile name already taken"))
}

services, err := dockercompose.GetServices(envHomeDir)
utils.ErrorAndExit(err)

if err != nil {
utils.ErrorAndExit(errors.New("Could not read services"))
}

selected := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, nil)
selected := survey.MultiSelect(fmt.Sprintf("Select services for profile %s", chalk.Cyan.Color(profileName)), services.All, nil)

if len(selected) == 0 {
fmt.Printf("%sNo services selected%s\n\n", chalk.Yellow, chalk.ResetColor)
utils.Warn("No services selected.")
}

viper.Set(fmt.Sprintf("envs.%s.profiles.%s", activeEnv.GetName(), profileName), selected)

config.Save(fmt.Sprintf("Saved profile: %s%s%s [%s]", chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to save profile '%s'", profileName))
config.Save(fmt.Sprintf("Saved profile to %s environment: %s", chalk.Bold.TextStyle(activeEnv.GetName()), chalk.Cyan.Color(profileName)), fmt.Errorf("Failed to save profile '%s'", profileName))
},
}

Expand Down
30 changes: 26 additions & 4 deletions internal/commands/profilecmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package profilecmd

import (
"errors"
"fmt"
"os"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
Expand All @@ -17,26 +19,46 @@ var deleteCmd = &cobra.Command{
Short: "Delete a profile of active environment",
Long: "Delete a profile of active environment",
Example: "dockma profile delete",
Args: cobra.NoArgs,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 && !config.GetActiveEnv().HasProfile(args[0]) {
return fmt.Errorf("No such profile '%s'", args[0])
}

if len(args) > 1 {
return errors.New("Command only takes one argument")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
profileNames := activeEnv.GetProfileNames()

if len(profileNames) == 0 {
fmt.Printf("%sNo profiles created in environment: %s%s\n", chalk.Cyan, activeEnv, chalk.ResetColor)
fmt.Printf("No profiles in environment: %s\n", chalk.Cyan.Color(activeEnv.GetName()))

os.Exit(0)
}

profileName := survey.Select("Select profile to be deleted", profileNames)
var profileName string
if len(args) == 0 {
profileName = survey.Select("Select profile to be deleted", profileNames)
} else {
profileName = args[0]

proceed := survey.Confirm("Are you sure", true)
if !proceed {
utils.Abort()
}
}

profileMap := viper.GetStringMap(fmt.Sprintf("envs.%s.profiles", activeEnv.GetName()))

profileMap[profileName] = nil

viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv.GetName()), profileMap)

config.Save(fmt.Sprintf("Deleted profile: %s%s%s [%s]", chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to delete profile '%s'", profileName))
config.Save(fmt.Sprintf("Deleted profile from %s environment: %s", chalk.Bold.TextStyle(activeEnv.GetName()), chalk.Cyan.Color(profileName)), fmt.Errorf("Failed to delete profile '%s'", profileName))
},
}

Expand Down
14 changes: 9 additions & 5 deletions internal/commands/profilecmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,27 @@ var listCmd = &cobra.Command{
fmt.Printf("No profiles in %s. Create one with %s.\n", chalk.Cyan.Color(activeEnv.GetName()), chalk.Cyan.Color("dockma profile create"))
}

for _, profileName := range profileNames {
fmt.Printf("%s%s%s\n", chalk.Cyan, profileName, chalk.ResetColor)
fmt.Printf("Profiles of %s environment:\n", chalk.Bold.TextStyle(activeEnv.GetName()))

for _, profileName := range profileNames {
if servicesFlag {
fmt.Println()

fmt.Println(chalk.Bold.TextStyle(profileName))

profile, err := activeEnv.GetProfile(profileName)

utils.ErrorAndExit(err)

for _, service := range profile.Services {
if utils.Includes(profile.Selected, service) {
fmt.Printf("- %s%s%s\n", chalk.Green, service, chalk.ResetColor)
fmt.Printf("- %s\n", chalk.Cyan.Color(service))
} else {
fmt.Printf("- %s\n", service)
}
}

fmt.Println()
} else {
fmt.Printf("- %s\n", chalk.Cyan.Color(profileName))
}

}
Expand Down
36 changes: 21 additions & 15 deletions internal/commands/profilecmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,48 @@ var renameCmd = &cobra.Command{
Short: "Rename profile",
Long: "Rename profile",
Example: "dockma profile rename",
Args: cobra.NoArgs,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 && !config.GetActiveEnv().HasProfile(args[0]) {
return fmt.Errorf("No such profile '%s'", args[0])
}

if len(args) > 1 {
return errors.New("Command only takes one argument")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()

profileNames := activeEnv.GetProfileNames()

if len(profileNames) == 0 {
fmt.Printf("%sNo profiles created in environment: %s%s\n", chalk.Cyan, activeEnv.GetName(), chalk.ResetColor)
fmt.Printf("No profiles in environment: %s\n", chalk.Cyan.Color(activeEnv.GetName()))

os.Exit(0)
}

renameProfile := survey.Select("Select profile to update", profileNames)

profileName := survey.InputName("Enter name for profile", renameProfile)

// FIXME use regex
if profileName == "" || profileName == "-" {
utils.ErrorAndExit(errors.New("Invalid profile name"))
var renameProfile string
if len(args) == 0 {
renameProfile = survey.Select("Select profile to update", profileNames)
} else {
renameProfile = args[0]
}

profileName := survey.InputName("Enter new name for profile", renameProfile)

if activeEnv.HasProfile(profileName) {
utils.ErrorAndExit(errors.New("Profile name already taken. Use 'update' to reselect services"))
utils.ErrorAndExit(errors.New("Profile name already taken"))
}

profileMap := viper.GetStringMap(fmt.Sprintf("envs.%s.profiles", activeEnv.GetName()))

profile := profileMap[renameProfile]

profileMap[renameProfile] = nil

profileMap[profileName] = profile

viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv.GetName()), profileMap)

config.Save(fmt.Sprintf("Renamed profile from %s to %s%s%s [%s]", renameProfile, chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to rename profile '%s'", renameProfile))
config.Save(fmt.Sprintf("Renamed profile from %s to %s %s", chalk.Cyan.Color(renameProfile), chalk.Cyan.Color(profileName), chalk.Bold.TextStyle(fmt.Sprintf("[%s]", activeEnv.GetName()))), fmt.Errorf("Failed to rename profile '%s'", renameProfile))
},
}

Expand Down
28 changes: 22 additions & 6 deletions internal/commands/profilecmd/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package profilecmd

import (
"errors"
"fmt"
"os"

Expand All @@ -19,36 +20,51 @@ var updateCmd = &cobra.Command{
Short: "Update profile's service selection",
Long: "Update profile's service selection",
Example: "dockma profile update",
Args: cobra.NoArgs,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 && !config.GetActiveEnv().HasProfile(args[0]) {
return fmt.Errorf("No such profile '%s'", args[0])
}

if len(args) > 1 {
return errors.New("Command only takes one argument")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
envHomeDir := activeEnv.GetHomeDir()

profileNames := activeEnv.GetProfileNames()

if len(profileNames) == 0 {
fmt.Printf("%sNo profiles created in environment: %s%s\n", chalk.Cyan, activeEnv.GetName(), chalk.ResetColor)
fmt.Printf("No profiles in environment: %s\n", chalk.Cyan.Color(activeEnv.GetName()))

os.Exit(0)
}

profileName := survey.Select("Select profile to update", profileNames)
var profileName string
if len(args) == 0 {
profileName = survey.Select("Select profile to update", profileNames)
} else {
profileName = args[0]
}

services, err := dockercompose.GetServices(envHomeDir)
utils.ErrorAndExit(err)

profile, err := activeEnv.GetProfile(profileName)
utils.ErrorAndExit(err)

selected := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, profile.Selected)
selected := survey.MultiSelect(fmt.Sprintf("Select services for profile %s", chalk.Cyan.Color(profileName)), services.All, profile.Selected)

if len(selected) == 0 {
fmt.Printf("%sNo services selected%s\n\n", chalk.Yellow, chalk.ResetColor)
utils.Warn("No services selected.")
}

viper.Set(fmt.Sprintf("envs.%s.profiles.%s", activeEnv.GetName(), profileName), selected)

config.Save(fmt.Sprintf("Updated profile: %s [%s]", chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to update profile '%s'", profileName))
config.Save(fmt.Sprintf("Updated profile in %s: %s", chalk.Bold.TextStyle(activeEnv.GetName()), chalk.Cyan.Color(profileName)), fmt.Errorf("Failed to update profile '%s'", profileName))
},
}

Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func (e *env) GetProfileNames() (profiles []string) {
profiles = append(profiles, profile)
}

sort.Strings(profiles)

return
}

Expand Down

0 comments on commit b912b3c

Please sign in to comment.