207
internal/cmd/compose.go
Archivo normal
207
internal/cmd/compose.go
Archivo normal
@@ -0,0 +1,207 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yourusername/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")
|
||||
}
|
||||
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")
|
||||
}
|
||||
28
internal/cmd/init.go
Archivo normal
28
internal/cmd/init.go
Archivo normal
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize buque configuration",
|
||||
Long: `Initialize buque with default configuration file.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := configMgr.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize config: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Buque initialized successfully!\n")
|
||||
fmt.Printf("Configuration file: %s\n", cfg.ConfigPath)
|
||||
fmt.Printf("\nNext steps:\n")
|
||||
fmt.Printf(" 1. Add environments: buque env add <name> <path>\n")
|
||||
fmt.Printf(" 2. Deploy nginx-proxy: buque proxy deploy\n")
|
||||
fmt.Printf(" 3. Start environments: buque up <name>\n")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
114
internal/cmd/logs.go
Archivo normal
114
internal/cmd/logs.go
Archivo normal
@@ -0,0 +1,114 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yourusername/buque/internal/docker"
|
||||
"github.com/yourusername/buque/internal/models"
|
||||
)
|
||||
|
||||
var logsCmd = &cobra.Command{
|
||||
Use: "logs <environment>",
|
||||
Short: "Show logs from an environment",
|
||||
Long: `Display logs from containers in an environment.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg := configMgr.GetConfig()
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration not loaded")
|
||||
}
|
||||
|
||||
envName := args[0]
|
||||
var env *models.Environment
|
||||
|
||||
for _, e := range cfg.Environments {
|
||||
if e.Name == envName {
|
||||
env = &e
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if env == nil {
|
||||
return fmt.Errorf("environment '%s' not found", envName)
|
||||
}
|
||||
|
||||
compose, err := docker.NewComposeManager()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
follow, _ := cmd.Flags().GetBool("follow")
|
||||
tail, _ := cmd.Flags().GetString("tail")
|
||||
|
||||
ctx := context.Background()
|
||||
return compose.Logs(ctx, *env, follow, tail)
|
||||
},
|
||||
}
|
||||
|
||||
var psCmd = &cobra.Command{
|
||||
Use: "ps [environment...]",
|
||||
Short: "List containers",
|
||||
Long: `List containers for one or more environments. If no environment is specified, lists all.`,
|
||||
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 {
|
||||
fmt.Printf("\n=== Environment: %s ===\n", env.Name)
|
||||
|
||||
output, err := compose.PS(ctx, env)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
fmt.Println("No containers running")
|
||||
} else {
|
||||
fmt.Println(output)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var pruneCmd = &cobra.Command{
|
||||
Use: "prune",
|
||||
Short: "Remove unused Docker resources",
|
||||
Long: `Remove unused containers, images, networks, and volumes.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
client, err := docker.NewClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
fmt.Println("Pruning unused images...")
|
||||
if err := client.PruneImages(ctx); err != nil {
|
||||
return fmt.Errorf("failed to prune images: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println("Unused resources pruned successfully!")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
logsCmd.Flags().BoolP("follow", "f", false, "Follow log output")
|
||||
logsCmd.Flags().StringP("tail", "t", "100", "Number of lines to show from the end of the logs")
|
||||
}
|
||||
118
internal/cmd/proxy.go
Archivo normal
118
internal/cmd/proxy.go
Archivo normal
@@ -0,0 +1,118 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yourusername/buque/internal/proxy"
|
||||
)
|
||||
|
||||
var proxyCmd = &cobra.Command{
|
||||
Use: "proxy",
|
||||
Short: "Manage nginx-proxy",
|
||||
Long: `Deploy, remove, and manage the nginx-proxy reverse proxy.`,
|
||||
}
|
||||
|
||||
var proxyDeployCmd = &cobra.Command{
|
||||
Use: "deploy",
|
||||
Short: "Deploy nginx-proxy",
|
||||
Long: `Deploy nginx-proxy with SSL support using Let's Encrypt.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg := configMgr.GetConfig()
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration not loaded")
|
||||
}
|
||||
|
||||
manager, err := proxy.NewNginxManager(cfg.NginxProxy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
return manager.Deploy(ctx)
|
||||
},
|
||||
}
|
||||
|
||||
var proxyRemoveCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove nginx-proxy",
|
||||
Long: `Stop and remove the nginx-proxy deployment.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg := configMgr.GetConfig()
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration not loaded")
|
||||
}
|
||||
|
||||
manager, err := proxy.NewNginxManager(cfg.NginxProxy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
return manager.Remove(ctx)
|
||||
},
|
||||
}
|
||||
|
||||
var proxyStatusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Show nginx-proxy status",
|
||||
Long: `Display the status of nginx-proxy containers.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg := configMgr.GetConfig()
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration not loaded")
|
||||
}
|
||||
|
||||
manager, err := proxy.NewNginxManager(cfg.NginxProxy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
status, err := manager.Status(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(status)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var proxyExampleCmd = &cobra.Command{
|
||||
Use: "example <service-name> <virtual-host>",
|
||||
Short: "Generate example docker-compose.yml",
|
||||
Long: `Generate an example docker-compose.yml for a service behind nginx-proxy.`,
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg := configMgr.GetConfig()
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration not loaded")
|
||||
}
|
||||
|
||||
serviceName := args[0]
|
||||
virtualHost := args[1]
|
||||
|
||||
manager, err := proxy.NewNginxManager(cfg.NginxProxy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
|
||||
example := manager.GetExampleServiceCompose(serviceName, virtualHost)
|
||||
fmt.Println(example)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
proxyCmd.AddCommand(proxyDeployCmd)
|
||||
proxyCmd.AddCommand(proxyRemoveCmd)
|
||||
proxyCmd.AddCommand(proxyStatusCmd)
|
||||
proxyCmd.AddCommand(proxyExampleCmd)
|
||||
}
|
||||
56
internal/cmd/root.go
Archivo normal
56
internal/cmd/root.go
Archivo normal
@@ -0,0 +1,56 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yourusername/buque/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
configMgr *config.Manager
|
||||
rootCmd *cobra.Command
|
||||
)
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "buque",
|
||||
Short: "Buque - Docker Compose environment manager",
|
||||
Long: `Buque is a command-line tool for managing multiple Docker Compose
|
||||
environments on a single machine. It provides easy deployment, monitoring,
|
||||
and maintenance of containerized applications with nginx-proxy integration.`,
|
||||
Version: "1.0.0",
|
||||
}
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.buque/config.yaml)")
|
||||
|
||||
// Add all subcommands
|
||||
rootCmd.AddCommand(initCmd)
|
||||
rootCmd.AddCommand(envCmd)
|
||||
rootCmd.AddCommand(upCmd)
|
||||
rootCmd.AddCommand(downCmd)
|
||||
rootCmd.AddCommand(restartCmd)
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
rootCmd.AddCommand(statsCmd)
|
||||
rootCmd.AddCommand(logsCmd)
|
||||
rootCmd.AddCommand(psCmd)
|
||||
rootCmd.AddCommand(proxyCmd)
|
||||
rootCmd.AddCommand(pullCmd)
|
||||
rootCmd.AddCommand(pruneCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
configMgr = config.NewManager(cfgFile)
|
||||
if _, err := configMgr.Load(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to load config: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Execute runs the root command
|
||||
func Execute() error {
|
||||
return rootCmd.Execute()
|
||||
}
|
||||
131
internal/cmd/stats.go
Archivo normal
131
internal/cmd/stats.go
Archivo normal
@@ -0,0 +1,131 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yourusername/buque/internal/models"
|
||||
"github.com/yourusername/buque/internal/stats"
|
||||
)
|
||||
|
||||
var statsCmd = &cobra.Command{
|
||||
Use: "stats [environment]",
|
||||
Short: "Show container statistics",
|
||||
Long: `Display resource usage statistics for containers. If an environment is specified, shows only containers from that environment.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
collector, err := stats.NewCollector()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer collector.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
continuous, _ := cmd.Flags().GetBool("continuous")
|
||||
interval, _ := cmd.Flags().GetInt("interval")
|
||||
sortBy, _ := cmd.Flags().GetString("sort")
|
||||
|
||||
if continuous {
|
||||
// Clear screen and show stats continuously
|
||||
fmt.Print("\033[2J") // Clear screen
|
||||
|
||||
ticker := time.NewTicker(time.Duration(interval) * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
fmt.Print("\033[H") // Move cursor to home position
|
||||
|
||||
if err := displayStats(ctx, collector, args, sortBy); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("\nRefreshing every %d seconds... (Press Ctrl+C to exit)\n", interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return displayStats(ctx, collector, args, sortBy)
|
||||
},
|
||||
}
|
||||
|
||||
func displayStats(ctx context.Context, collector *stats.Collector, args []string, sortBy string) error {
|
||||
var containerStats []models.ContainerStats
|
||||
var err error
|
||||
|
||||
if len(args) > 0 {
|
||||
// Show stats for specific environment
|
||||
containerStats, err = collector.CollectForEnvironment(ctx, args[0])
|
||||
} else {
|
||||
// Show stats for all containers
|
||||
containerStats, err = collector.CollectAll(ctx)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(containerStats) == 0 {
|
||||
fmt.Println("No running containers found.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sort stats
|
||||
containerStats = collector.SortStats(containerStats, sortBy, true)
|
||||
|
||||
// Display in table format
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||
fmt.Fprintln(w, "CONTAINER\tENVIRONMENT\tCPU %\tMEMORY USAGE\tMEMORY %\tNET I/O\tBLOCK I/O")
|
||||
|
||||
for _, stat := range containerStats {
|
||||
netIO := fmt.Sprintf("%s / %s",
|
||||
stats.FormatBytes(stat.NetworkRx),
|
||||
stats.FormatBytes(stat.NetworkTx))
|
||||
blockIO := fmt.Sprintf("%s / %s",
|
||||
stats.FormatBytes(stat.BlockRead),
|
||||
stats.FormatBytes(stat.BlockWrite))
|
||||
memUsage := fmt.Sprintf("%s / %s",
|
||||
stats.FormatBytes(stat.MemoryUsage),
|
||||
stats.FormatBytes(stat.MemoryLimit))
|
||||
|
||||
fmt.Fprintf(w, "%s\t%s\t%.2f%%\t%s\t%.2f%%\t%s\t%s\n",
|
||||
stat.Name,
|
||||
stat.Environment,
|
||||
stat.CPUPercentage,
|
||||
memUsage,
|
||||
stat.MemoryPercent,
|
||||
netIO,
|
||||
blockIO,
|
||||
)
|
||||
}
|
||||
|
||||
w.Flush()
|
||||
|
||||
// Show aggregated stats
|
||||
aggStats, err := collector.GetAggregatedStats(ctx)
|
||||
if err == nil {
|
||||
fmt.Printf("\nTotal Containers: %d\n", aggStats.TotalContainers)
|
||||
fmt.Printf("Total CPU: %.2f%%\n", aggStats.TotalCPUPercent)
|
||||
fmt.Printf("Total Memory: %s / %s (%.2f%%)\n",
|
||||
stats.FormatBytes(aggStats.TotalMemoryUsage),
|
||||
stats.FormatBytes(aggStats.TotalMemoryLimit),
|
||||
aggStats.TotalMemoryPercent)
|
||||
fmt.Printf("Total Network: %s / %s\n",
|
||||
stats.FormatBytes(aggStats.TotalNetworkRx),
|
||||
stats.FormatBytes(aggStats.TotalNetworkTx))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
statsCmd.Flags().BoolP("continuous", "c", false, "Continuous monitoring mode")
|
||||
statsCmd.Flags().IntP("interval", "i", 2, "Refresh interval in seconds (for continuous mode)")
|
||||
statsCmd.Flags().StringP("sort", "s", "cpu", "Sort by: cpu, memory, network, name")
|
||||
}
|
||||
24
internal/cmd/utils.go
Archivo normal
24
internal/cmd/utils.go
Archivo normal
@@ -0,0 +1,24 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/yourusername/buque/internal/models"
|
||||
)
|
||||
|
||||
// getEnvironmentsToProcess returns environments to process based on args
|
||||
func getEnvironmentsToProcess(args []string, allEnvs []models.Environment) []models.Environment {
|
||||
if len(args) == 0 {
|
||||
return allEnvs
|
||||
}
|
||||
|
||||
var result []models.Environment
|
||||
for _, arg := range args {
|
||||
for _, env := range allEnvs {
|
||||
if env.Name == arg {
|
||||
result = append(result, env)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Referencia en una nueva incidencia
Block a user