208 líneas
5.3 KiB
Go
208 líneas
5.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/manalejandro/buque/internal/docker"
|
|
)
|
|
|
|
var upCmd = &cobra.Command{
|
|
Use: "up [environment...]",
|
|
Short: "Start environments",
|
|
Long: `Start one or more environments. If no environment is specified, starts all enabled environments.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg := configMgr.GetConfig()
|
|
if cfg == nil {
|
|
return fmt.Errorf("configuration not loaded")
|
|
}
|
|
|
|
compose, err := docker.NewComposeManager()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
detach, _ := cmd.Flags().GetBool("detach")
|
|
build, _ := cmd.Flags().GetBool("build")
|
|
|
|
ctx := context.Background()
|
|
environments := getEnvironmentsToProcess(args, cfg.Environments)
|
|
|
|
for _, env := range environments {
|
|
if !env.Enabled {
|
|
fmt.Printf("Skipping disabled environment: %s\n", env.Name)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Starting environment: %s\n", env.Name)
|
|
|
|
if build {
|
|
if err := compose.Build(ctx, env); err != nil {
|
|
fmt.Printf("Warning: failed to build %s: %v\n", env.Name, err)
|
|
}
|
|
}
|
|
|
|
if err := compose.Up(ctx, env, detach); err != nil {
|
|
return fmt.Errorf("failed to start %s: %w", env.Name, err)
|
|
}
|
|
|
|
fmt.Printf("Environment '%s' started successfully!\n", env.Name)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var downCmd = &cobra.Command{
|
|
Use: "down [environment...]",
|
|
Short: "Stop environments",
|
|
Long: `Stop one or more environments. If no environment is specified, stops all environments.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg := configMgr.GetConfig()
|
|
if cfg == nil {
|
|
return fmt.Errorf("configuration not loaded")
|
|
}
|
|
|
|
compose, err := docker.NewComposeManager()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
removeVolumes, _ := cmd.Flags().GetBool("volumes")
|
|
ctx := context.Background()
|
|
environments := getEnvironmentsToProcess(args, cfg.Environments)
|
|
|
|
for _, env := range environments {
|
|
fmt.Printf("Stopping environment: %s\n", env.Name)
|
|
|
|
if err := compose.Down(ctx, env, removeVolumes); err != nil {
|
|
return fmt.Errorf("failed to stop %s: %w", env.Name, err)
|
|
}
|
|
|
|
fmt.Printf("Environment '%s' stopped successfully!\n", env.Name)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var restartCmd = &cobra.Command{
|
|
Use: "restart [environment...]",
|
|
Short: "Restart environments",
|
|
Long: `Restart one or more environments. If no environment is specified, restarts all enabled environments.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg := configMgr.GetConfig()
|
|
if cfg == nil {
|
|
return fmt.Errorf("configuration not loaded")
|
|
}
|
|
|
|
compose, err := docker.NewComposeManager()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx := context.Background()
|
|
environments := getEnvironmentsToProcess(args, cfg.Environments)
|
|
|
|
for _, env := range environments {
|
|
if !env.Enabled {
|
|
fmt.Printf("Skipping disabled environment: %s\n", env.Name)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Restarting environment: %s\n", env.Name)
|
|
|
|
if err := compose.Restart(ctx, env); err != nil {
|
|
return fmt.Errorf("failed to restart %s: %w", env.Name, err)
|
|
}
|
|
|
|
fmt.Printf("Environment '%s' restarted successfully!\n", env.Name)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var pullCmd = &cobra.Command{
|
|
Use: "pull [environment...]",
|
|
Short: "Pull images for environments",
|
|
Long: `Pull latest images for one or more environments. If no environment is specified, pulls all enabled environments.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg := configMgr.GetConfig()
|
|
if cfg == nil {
|
|
return fmt.Errorf("configuration not loaded")
|
|
}
|
|
|
|
compose, err := docker.NewComposeManager()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx := context.Background()
|
|
environments := getEnvironmentsToProcess(args, cfg.Environments)
|
|
|
|
for _, env := range environments {
|
|
if !env.Enabled {
|
|
fmt.Printf("Skipping disabled environment: %s\n", env.Name)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Pulling images for environment: %s\n", env.Name)
|
|
|
|
if err := compose.Pull(ctx, env); err != nil {
|
|
fmt.Printf("Warning: failed to pull images for %s: %v\n", env.Name, err)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Images for '%s' pulled successfully!\n", env.Name)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var updateCmd = &cobra.Command{
|
|
Use: "update [environment...]",
|
|
Short: "Update environments",
|
|
Long: `Update one or more environments by pulling latest images and recreating containers.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg := configMgr.GetConfig()
|
|
if cfg == nil {
|
|
return fmt.Errorf("configuration not loaded")
|
|
}
|
|
|
|
compose, err := docker.NewComposeManager()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx := context.Background()
|
|
environments := getEnvironmentsToProcess(args, cfg.Environments)
|
|
|
|
for _, env := range environments {
|
|
if !env.Enabled {
|
|
fmt.Printf("Skipping disabled environment: %s\n", env.Name)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Updating environment: %s\n", env.Name)
|
|
|
|
if err := compose.Update(ctx, env); err != nil {
|
|
return fmt.Errorf("failed to update %s: %w", env.Name, err)
|
|
}
|
|
|
|
fmt.Printf("Environment '%s' updated successfully!\n", env.Name)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
upCmd.Flags().BoolP("detach", "d", true, "Detached mode: Run containers in the background")
|
|
upCmd.Flags().BoolP("build", "b", false, "Build images before starting")
|
|
|
|
downCmd.Flags().BoolP("volumes", "v", false, "Remove volumes")
|
|
}
|