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

Commit

Permalink
add profile cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 7, 2020
1 parent 2f8a796 commit b4be01d
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var createCmd = &cobra.Command{
Use: "create",
Short: "Create named service selection.",
Long: `-`,
Example: "dockma profile create [name]",
Example: "dockma profile create",
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
envHomeDir := config.GetEnvHomeDir(activeEnv)
Expand Down Expand Up @@ -51,13 +51,17 @@ var createCmd = &cobra.Command{

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

utils.Abort()
}

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

config.Save()
err = config.Save()

if err != nil {
utils.Error(err)
}

utils.Success(fmt.Sprintf("Successfully saved profile: %s [%s]", profileName, activeEnv))
},
}

Expand Down
55 changes: 55 additions & 0 deletions internal/commands/profilecmd/delete.go
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)
}
52 changes: 52 additions & 0 deletions internal/commands/profilecmd/list.go
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")
}
75 changes: 75 additions & 0 deletions internal/commands/profilecmd/rename.go
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)
}
76 changes: 76 additions & 0 deletions internal/commands/profilecmd/update.go
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)
}

0 comments on commit b4be01d

Please sign in to comment.