diff --git a/internal/commands/upcmd/root.go b/internal/commands/upcmd/root.go index 048d3d9..efd2eeb 100644 --- a/internal/commands/upcmd/root.go +++ b/internal/commands/upcmd/root.go @@ -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) @@ -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 { @@ -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") } } diff --git a/internal/config/config.go b/internal/config/config.go index be9d424..d52756e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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{} diff --git a/pkg/dockercompose/dockercompose.go b/pkg/dockercompose/dockercompose.go index 4bc511b..0c88795 100644 --- a/pkg/dockercompose/dockercompose.go +++ b/pkg/dockercompose/dockercompose.go @@ -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 { @@ -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{} @@ -63,6 +79,8 @@ func GetServices(filepath string) (services Services, err error) { services.All = services.Base } + servicesChache[filepath] = services + return services, nil }