115 líneas
2.6 KiB
Go
115 líneas
2.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/manalejandro/buque/internal/docker"
|
|
"github.com/manalejandro/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")
|
|
}
|