|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "stackroost/internal" |
| 10 | + "stackroost/internal/logger" |
| 11 | +) |
| 12 | + |
| 13 | +var purgeDomainLogsCmd = &cobra.Command{ |
| 14 | + Use: "purge-domain-logs", |
| 15 | + Short: "Delete access and error logs for a specific domain", |
| 16 | + Run: func(cmd *cobra.Command, args []string) { |
| 17 | + domain, _ := cmd.Flags().GetString("domain") |
| 18 | + if internal.IsNilOrEmpty(domain) { |
| 19 | + logger.Error("Please provide a domain using --domain") |
| 20 | + os.Exit(1) |
| 21 | + } |
| 22 | + |
| 23 | + server := internal.DetectServerType(domain) |
| 24 | + if server == "" { |
| 25 | + logger.Error("Could not detect server type for domain: " + domain) |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + |
| 29 | + var accessLog, errorLog string |
| 30 | + |
| 31 | + switch server { |
| 32 | + case "apache": |
| 33 | + accessLog = filepath.Join("/var/log/apache2", domain+"-access.log") |
| 34 | + errorLog = filepath.Join("/var/log/apache2", domain+"-error.log") |
| 35 | + case "nginx": |
| 36 | + accessLog = filepath.Join("/var/log/nginx", domain+"-access.log") |
| 37 | + errorLog = filepath.Join("/var/log/nginx", domain+"-error.log") |
| 38 | + case "caddy": |
| 39 | + logger.Warn("Caddy does not maintain traditional per-domain logs") |
| 40 | + return |
| 41 | + default: |
| 42 | + logger.Error("Unsupported server type: " + server) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + for _, logFile := range []string{accessLog, errorLog} { |
| 47 | + if _, err := os.Stat(logFile); err == nil { |
| 48 | + logger.Info("Deleting log: " + logFile) |
| 49 | + if err := internal.RunCommand("sudo", "rm", "-f", logFile); err != nil { |
| 50 | + logger.Error("Failed to delete log file: " + err.Error()) |
| 51 | + } else { |
| 52 | + logger.Success("Deleted: " + logFile) |
| 53 | + } |
| 54 | + } else { |
| 55 | + logger.Warn("Log not found: " + logFile) |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +func init() { |
| 62 | + rootCmd.AddCommand(purgeDomainLogsCmd) |
| 63 | + purgeDomainLogsCmd.Flags().String("domain", "", "Domain name to purge logs for") |
| 64 | + purgeDomainLogsCmd.MarkFlagRequired("domain") |
| 65 | +} |
0 commit comments