diff --git a/internal/commands/profilecmd/create.go b/internal/commands/profilecmd/create.go index 94c8837..bba7a18 100644 --- a/internal/commands/profilecmd/create.go +++ b/internal/commands/profilecmd/create.go @@ -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)) }, } diff --git a/internal/commands/profilecmd/delete.go b/internal/commands/profilecmd/delete.go index 164f1d5..37d33f6 100644 --- a/internal/commands/profilecmd/delete.go +++ b/internal/commands/profilecmd/delete.go @@ -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" @@ -17,18 +19,38 @@ 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())) @@ -36,7 +58,7 @@ var deleteCmd = &cobra.Command{ 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)) }, } diff --git a/internal/commands/profilecmd/list.go b/internal/commands/profilecmd/list.go index afe33c9..9c30c80 100644 --- a/internal/commands/profilecmd/list.go +++ b/internal/commands/profilecmd/list.go @@ -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)) } } diff --git a/internal/commands/profilecmd/rename.go b/internal/commands/profilecmd/rename.go index 6656fd5..04e20b8 100644 --- a/internal/commands/profilecmd/rename.go +++ b/internal/commands/profilecmd/rename.go @@ -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)) }, } diff --git a/internal/commands/profilecmd/update.go b/internal/commands/profilecmd/update.go index f84739d..b58cd63 100644 --- a/internal/commands/profilecmd/update.go +++ b/internal/commands/profilecmd/update.go @@ -1,6 +1,7 @@ package profilecmd import ( + "errors" "fmt" "os" @@ -19,7 +20,17 @@ 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() @@ -27,12 +38,17 @@ var updateCmd = &cobra.Command{ 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) @@ -40,15 +56,15 @@ var updateCmd = &cobra.Command{ 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)) }, } diff --git a/internal/config/config.go b/internal/config/config.go index cbee5a8..4d11bfb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -205,6 +205,8 @@ func (e *env) GetProfileNames() (profiles []string) { profiles = append(profiles, profile) } + sort.Strings(profiles) + return }