Skip to content

Commit

Permalink
Merge pull request #133 from local-deploy/CU-86950pntz
Browse files Browse the repository at this point in the history
feat(config): command to disable running service containers
  • Loading branch information
varrcan authored Jul 5, 2024
2 parents fda59c3 + cb24712 commit 31dc281
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func configCommand() *cobra.Command {
configCmd.AddCommand(
configLangCommand(),
configRepoCommand(),
configServiceCommand(),
)
return configCmd
}
3 changes: 2 additions & 1 deletion command/config_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down
50 changes: 50 additions & 0 deletions command/config_service.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
25 changes: 24 additions & 1 deletion command/service_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

0 comments on commit 31dc281

Please sign in to comment.