diff --git a/command/config.go b/command/config.go index 58f95b7..493cc37 100644 --- a/command/config.go +++ b/command/config.go @@ -15,6 +15,7 @@ func configCommand() *cobra.Command { configCmd.AddCommand( configLangCommand(), configRepoCommand(), + configServiceCommand(), ) return configCmd } diff --git a/command/config_repo.go b/command/config_repo.go index 767ddf0..7c893e8 100644 --- a/command/config_repo.go +++ b/command/config_repo.go @@ -13,7 +13,7 @@ func configRepoCommand() *cobra.Command { Use: "repo", Short: "Repository source configuration", Long: `Menu for setting up the images source repository.`, - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { configRepoRun() }, Hidden: false, @@ -26,6 +26,7 @@ func configRepoRun() { options := []string{"ghcr.io", "quay.io"} selectedOption, _ := pterm.DefaultInteractiveSelect. WithOptions(options). + WithFilter(false). WithDefaultOption(currentRepo). Show("Select application repository source") pterm.Printfln("Selected repo: %s", pterm.Green(selectedOption)) diff --git a/command/config_service.go b/command/config_service.go new file mode 100644 index 0000000..1576948 --- /dev/null +++ b/command/config_service.go @@ -0,0 +1,50 @@ +package command + +import ( + "log" + + "atomicgo.dev/keyboard/keys" + "github.com/pterm/pterm" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func configServiceCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "service", + Short: "Additional service containers", + Long: `Menu for managing the launch of additional containers (portainer and mailhog).`, + Run: func(_ *cobra.Command, _ []string) { + configServiceRun() + }, + Hidden: false, + } + return cmd +} + +func configServiceRun() { + hasKeys := viper.IsSet("services") + currentServices := viper.GetStringSlice("services") + if !hasKeys { + currentServices = append(currentServices, "portainer", "mail") + } + options := []string{"portainer", "mail"} + selectedOption, _ := pterm.DefaultInteractiveMultiselect. + WithOptions(options). + WithFilter(false). + WithKeySelect(keys.Space). + WithKeyConfirm(keys.Enter). + WithDefaultOptions(currentServices). + Show("Select the services that should be started with the 'dl service up' command") + pterm.Printfln("Selected services: %s", pterm.Green(selectedOption)) + + saveServiceConfig(selectedOption) +} + +func saveServiceConfig(lang interface{}) { + viper.Set("services", lang) + err := viper.WriteConfig() + if err != nil { + log.Fatal(err) + } +} diff --git a/command/service_up.go b/command/service_up.go index fc1c5cf..001eb78 100644 --- a/command/service_up.go +++ b/command/service_up.go @@ -2,12 +2,14 @@ package command import ( "context" + "slices" "github.com/compose-spec/compose-go/v2/types" "github.com/local-deploy/dl/containers" "github.com/local-deploy/dl/utils" "github.com/local-deploy/dl/utils/docker" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var recreate bool @@ -17,7 +19,7 @@ func upServiceCommand() *cobra.Command { Use: "up", Short: "Start local services", Long: `Start portainer, mailcatcher and traefik containers.`, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { ctx := cmd.Context() err := upServiceRun(ctx) if err != nil { @@ -61,6 +63,9 @@ func upServiceRun(ctx context.Context) error { services := types.Services{} servicesContainers := getServicesContainer() for _, service := range servicesContainers { + if !isEnable(service.Name) { + continue + } services[service.Name] = service } @@ -82,3 +87,21 @@ func upServiceRun(ctx context.Context) error { return nil } + +func isEnable(service string) bool { + if service == "traefik" { + return true + } + + hasKeys := viper.IsSet("services") + services := viper.GetStringSlice("services") + if !hasKeys { + services = append(services, "portainer", "mail") + } + + index := slices.IndexFunc(services, func(v string) bool { + return v == service + }) + + return index != -1 +}