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

Commit

Permalink
add latest profile
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 7, 2020
1 parent 30213d9 commit a9f98fc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
37 changes: 27 additions & 10 deletions internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,33 @@ var UpCommand = &cobra.Command{
var preselected []string

// default
profileName := "CUSTOM"

profileName := "latest"
if len(profileNames) > 0 {
profileNames = append(profileNames, "CUSTOM")
profileNames = append(profileNames, "latest")

profileName, err := survey.Select("Select profile to run or CUSTOM", profileNames)
profileName, err := survey.Select(fmt.Sprintf("Select profile to run or %slatest%s", chalk.Cyan, chalk.ResetColor), profileNames)

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

profile, err := config.GetProfile(activeEnv, profileName)
if profileName != "latest" {
profile, err := config.GetProfile(activeEnv, profileName)

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

preselected = profile.Selected
} else {
profile, err := config.GetLatest(activeEnv)

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

preselected = profile.Selected
preselected = profile.Selected
}
}

services, err := dockercompose.GetServices(envHomeDir)
Expand All @@ -89,7 +98,7 @@ var UpCommand = &cobra.Command{
utils.Abort()
}

if profileName == "CUSTOM" {
if profileName == "latest" {
saveProfile, err := survey.Confirm("Save as profile", false)

if err != nil {
Expand All @@ -104,6 +113,14 @@ var UpCommand = &cobra.Command{
}

viper.Set(fmt.Sprintf("envs.%s.profiles.%s", activeEnv, profileName), selectedServices)
} else {
viper.Set(fmt.Sprintf("envs.%s.latest", activeEnv), selectedServices)
}

err = config.Save()

if err != nil {
fmt.Printf("%sSome problem ocurred: Could not save profile/latest selection%s\n")
}
}

Expand Down
17 changes: 17 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ type Profile struct {
Selected []string
}

// GetLatest returns special profile with latest chosen services
func GetLatest(env string) (profile Profile, err error) {
profile = Profile{}

services, err := dockercompose.GetServices(GetEnvHomeDir(env))

if err != nil {
return
}

profile.Services = services.All

profile.Selected = viper.GetStringSlice(fmt.Sprintf("envs.%s.latest", env))

return
}

// GetProfile returns services for given profile
func GetProfile(env string, profileName string) (profile Profile, err error) {
profile = Profile{}
Expand Down
18 changes: 18 additions & 0 deletions pkg/dockercompose/dockercompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ type Services struct {
Override []string
}

var servicesChache map[string]Services = make(map[string]Services)

func isEmpty(services Services) bool {
if len(services.All) == 0 || len(services.Base) == 0 || len(services.Override) == 0 {
return true
}

return false
}

func GetDockerCompose(filepath string, override bool) (*viper.Viper, error) {
fileName := "docker-compose"
if override {
Expand Down Expand Up @@ -43,6 +53,12 @@ func GetVersion(filepath string) string {
}

func GetServices(filepath string) (services Services, err error) {
services = servicesChache[filepath]

if !isEmpty(services) {
return
}

dockercompose, err := GetDockerCompose(filepath, false)

services = Services{}
Expand All @@ -63,6 +79,8 @@ func GetServices(filepath string) (services Services, err error) {
services.All = services.Base
}

servicesChache[filepath] = services

return services, nil
}

Expand Down

0 comments on commit a9f98fc

Please sign in to comment.