This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f8a796
commit b4be01d
Showing
5 changed files
with
266 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package profilecmd | ||
|
||
import ( | ||
"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" | ||
) | ||
|
||
var deleteCmd = &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete a profile of active environment.", | ||
Long: `-`, | ||
Aliases: []string{"del"}, | ||
Example: "dockma profile delete", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
activeEnv := config.GetActiveEnv() | ||
profileNames := config.GetProfilesNames(activeEnv) | ||
|
||
if len(profileNames) == 0 { | ||
fmt.Printf("%sNo profiles created in environment: %s%s\n", chalk.Cyan, activeEnv, chalk.ResetColor) | ||
|
||
os.Exit(0) | ||
} | ||
|
||
profileName, err := survey.Select("Select profile to be deleted", profileNames) | ||
|
||
if err != nil { | ||
utils.Error(err) | ||
} | ||
|
||
profileMap := viper.GetStringMap(fmt.Sprintf("envs.%s.profiles", activeEnv)) | ||
|
||
profileMap[profileName] = nil | ||
|
||
viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv), profileMap) | ||
|
||
err = config.Save() | ||
|
||
if err != nil { | ||
utils.Error(err) | ||
} | ||
|
||
utils.Success(fmt.Sprintf("Successfully deleted profile: %s [%s]", profileName, activeEnv)) | ||
}, | ||
} | ||
|
||
func init() { | ||
ProfileCommand.AddCommand(deleteCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package profilecmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/martinnirtl/dockma/internal/config" | ||
"github.com/martinnirtl/dockma/internal/utils" | ||
"github.com/spf13/cobra" | ||
"github.com/ttacon/chalk" | ||
) | ||
|
||
var servicesFlag bool | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List profiles of active environment.", | ||
Long: `-`, | ||
Example: "dockma profile list", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
activeEnv := config.GetActiveEnv() | ||
profileNames := config.GetProfilesNames(activeEnv) | ||
|
||
for _, profileName := range profileNames { | ||
fmt.Printf("%s%s%s\n", chalk.Cyan, profileName, chalk.ResetColor) | ||
|
||
if servicesFlag { | ||
profile, err := config.GetProfile(activeEnv, profileName) | ||
|
||
if err != nil { | ||
utils.Error(err) | ||
} | ||
|
||
for _, service := range profile.Services { | ||
if utils.Includes(profile.Selected, service) { | ||
fmt.Printf("- %s%s%s\n", chalk.Green, service, chalk.ResetColor) | ||
} else { | ||
fmt.Printf("- %s\n", service) | ||
} | ||
} | ||
|
||
fmt.Println() | ||
} | ||
|
||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
ProfileCommand.AddCommand(listCmd) | ||
|
||
listCmd.Flags().BoolVarP(&servicesFlag, "services", "S", false, "Print services") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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" | ||
) | ||
|
||
var renameCmd = &cobra.Command{ | ||
Use: "rename", | ||
Short: "Rename profile.", | ||
Long: `-`, | ||
Example: "dockma profile rename", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
activeEnv := config.GetActiveEnv() | ||
|
||
profileNames := config.GetProfilesNames(activeEnv) | ||
|
||
if len(profileNames) == 0 { | ||
fmt.Printf("%sNo profiles created in environment: %s%s\n", chalk.Cyan, activeEnv, chalk.ResetColor) | ||
|
||
os.Exit(0) | ||
} | ||
|
||
renameProfile, err := survey.Select("Select profile to update", profileNames) | ||
|
||
if err != nil { | ||
utils.Abort() | ||
} | ||
|
||
profileName, err := survey.Input("Enter name for profile", renameProfile) | ||
|
||
if err != nil { | ||
utils.Abort() | ||
} | ||
|
||
// FIXME use regex | ||
if profileName == "" || profileName == "-" { | ||
utils.Error(errors.New("Invalid profile name")) | ||
} | ||
|
||
if config.HasProfileName(activeEnv, profileName) { | ||
utils.Error(errors.New("Profile name already taken. Use 'update' to reselect services")) | ||
} | ||
|
||
profileMap := viper.GetStringMap(fmt.Sprintf("envs.%s.profiles", activeEnv)) | ||
|
||
profile := profileMap[renameProfile] | ||
|
||
profileMap[renameProfile] = nil | ||
|
||
profileMap[profileName] = profile | ||
|
||
viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv), profileMap) | ||
|
||
err = config.Save() | ||
|
||
if err != nil { | ||
utils.Error(err) | ||
} | ||
|
||
utils.Success(fmt.Sprintf("Successfully renamed profile from %s to %s [%s]", renameProfile, profileName, activeEnv)) | ||
}, | ||
} | ||
|
||
func init() { | ||
ProfileCommand.AddCommand(renameCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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/martinnirtl/dockma/pkg/dockercompose" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/ttacon/chalk" | ||
) | ||
|
||
var updateCmd = &cobra.Command{ | ||
Use: "update", | ||
Short: "Update profile's service selection.", | ||
Long: `-`, | ||
Example: "dockma profile update", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
activeEnv := config.GetActiveEnv() | ||
envHomeDir := config.GetEnvHomeDir(activeEnv) | ||
|
||
profileNames := config.GetProfilesNames(activeEnv) | ||
|
||
if len(profileNames) == 0 { | ||
fmt.Printf("%sNo profiles created in environment: %s%s\n", chalk.Cyan, activeEnv, chalk.ResetColor) | ||
|
||
os.Exit(0) | ||
} | ||
|
||
profileName, err := survey.Select("Select profile to update", profileNames) | ||
|
||
if err != nil { | ||
utils.Abort() | ||
} | ||
|
||
services, err := dockercompose.GetServices(envHomeDir) | ||
|
||
if err != nil { | ||
utils.Error(errors.New("Could not read services")) | ||
} | ||
|
||
profile, err := config.GetProfile(activeEnv, profileName) | ||
|
||
if err != nil { | ||
utils.Error(err) | ||
} | ||
|
||
selected, err := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, profile.Selected) | ||
|
||
if err != nil { | ||
utils.Abort() | ||
} | ||
|
||
if len(selected) == 0 { | ||
fmt.Printf("%sNo services selected%s\n\n", chalk.Yellow, chalk.ResetColor) | ||
} | ||
|
||
viper.Set(fmt.Sprintf("envs.%s.profiles.%s", activeEnv, profileName), selected) | ||
|
||
err = config.Save() | ||
|
||
if err != nil { | ||
utils.Error(err) | ||
} | ||
|
||
utils.Success(fmt.Sprintf("Successfully updated profile: %s [%s]", profileName, activeEnv)) | ||
}, | ||
} | ||
|
||
func init() { | ||
ProfileCommand.AddCommand(updateCmd) | ||
} |