179
internal/cmd/env.go
Archivo normal
179
internal/cmd/env.go
Archivo normal
@@ -0,0 +1,179 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yourusername/buque/internal/models"
|
||||
)
|
||||
|
||||
var envCmd = &cobra.Command{
|
||||
Use: "env",
|
||||
Short: "Manage environments",
|
||||
Long: `Add, remove, list, and manage Docker Compose environments.`,
|
||||
}
|
||||
|
||||
var envListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all environments",
|
||||
Aliases: []string{"ls"},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg := configMgr.GetConfig()
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration not loaded")
|
||||
}
|
||||
|
||||
if len(cfg.Environments) == 0 {
|
||||
fmt.Println("No environments configured.")
|
||||
fmt.Println("Add an environment with: buque env add <name> <path>")
|
||||
return nil
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||
fmt.Fprintln(w, "NAME\tPATH\tCOMPOSE FILE\tENABLED\tCREATED")
|
||||
|
||||
for _, env := range cfg.Environments {
|
||||
enabled := "yes"
|
||||
if !env.Enabled {
|
||||
enabled = "no"
|
||||
}
|
||||
created := env.CreatedAt.Format("2006-01-02")
|
||||
if env.CreatedAt.IsZero() {
|
||||
created = "N/A"
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
|
||||
env.Name, env.Path, env.ComposeFile, enabled, created)
|
||||
}
|
||||
|
||||
w.Flush()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var envAddCmd = &cobra.Command{
|
||||
Use: "add <name> <path>",
|
||||
Short: "Add a new environment",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
path := args[1]
|
||||
|
||||
// Convert to absolute path
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid path: %w", err)
|
||||
}
|
||||
|
||||
// Check if path exists
|
||||
if _, err := os.Stat(absPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("path does not exist: %s", absPath)
|
||||
}
|
||||
|
||||
composeFile, _ := cmd.Flags().GetString("compose-file")
|
||||
|
||||
// Check if compose file exists
|
||||
composePath := filepath.Join(absPath, composeFile)
|
||||
if _, err := os.Stat(composePath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("compose file not found: %s", composePath)
|
||||
}
|
||||
|
||||
env := models.Environment{
|
||||
Name: name,
|
||||
Path: absPath,
|
||||
ComposeFile: composeFile,
|
||||
Enabled: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Labels: make(map[string]string),
|
||||
}
|
||||
|
||||
if err := configMgr.AddEnvironment(env); err != nil {
|
||||
return fmt.Errorf("failed to add environment: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Environment '%s' added successfully!\n", name)
|
||||
fmt.Printf("Path: %s\n", absPath)
|
||||
fmt.Printf("Compose file: %s\n", composeFile)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var envRemoveCmd = &cobra.Command{
|
||||
Use: "remove <name>",
|
||||
Short: "Remove an environment",
|
||||
Aliases: []string{"rm"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
|
||||
if err := configMgr.RemoveEnvironment(name); err != nil {
|
||||
return fmt.Errorf("failed to remove environment: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Environment '%s' removed successfully!\n", name)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var envEnableCmd = &cobra.Command{
|
||||
Use: "enable <name>",
|
||||
Short: "Enable an environment",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
cfg := configMgr.GetConfig()
|
||||
|
||||
for i, env := range cfg.Environments {
|
||||
if env.Name == name {
|
||||
cfg.Environments[i].Enabled = true
|
||||
cfg.Environments[i].UpdatedAt = time.Now()
|
||||
if err := configMgr.Save(); err != nil {
|
||||
return fmt.Errorf("failed to save config: %w", err)
|
||||
}
|
||||
fmt.Printf("Environment '%s' enabled!\n", name)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("environment '%s' not found", name)
|
||||
},
|
||||
}
|
||||
|
||||
var envDisableCmd = &cobra.Command{
|
||||
Use: "disable <name>",
|
||||
Short: "Disable an environment",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
cfg := configMgr.GetConfig()
|
||||
|
||||
for i, env := range cfg.Environments {
|
||||
if env.Name == name {
|
||||
cfg.Environments[i].Enabled = false
|
||||
cfg.Environments[i].UpdatedAt = time.Now()
|
||||
if err := configMgr.Save(); err != nil {
|
||||
return fmt.Errorf("failed to save config: %w", err)
|
||||
}
|
||||
fmt.Printf("Environment '%s' disabled!\n", name)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("environment '%s' not found", name)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
envCmd.AddCommand(envListCmd)
|
||||
envCmd.AddCommand(envAddCmd)
|
||||
envCmd.AddCommand(envRemoveCmd)
|
||||
envCmd.AddCommand(envEnableCmd)
|
||||
envCmd.AddCommand(envDisableCmd)
|
||||
|
||||
envAddCmd.Flags().StringP("compose-file", "f", "docker-compose.yml", "Docker Compose file name")
|
||||
}
|
||||
Referencia en una nueva incidencia
Block a user