|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/MayR-Labs/envdoc-go/internal/parser" |
| 8 | + "github.com/MayR-Labs/envdoc-go/internal/utils" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +// NewClearValuesCmd returns the clear-values command |
| 13 | +func NewClearValuesCmd() *cobra.Command { |
| 14 | + return &cobra.Command{ |
| 15 | + Use: "clear-values [file]", |
| 16 | + Short: "Clear all values from an environment file", |
| 17 | + Long: `Clears all values from the specified environment file, leaving only the keys. |
| 18 | +This is a dangerous operation and requires PIN confirmation.`, |
| 19 | + Args: cobra.MaximumNArgs(1), |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + var inputFile string |
| 22 | + var err error |
| 23 | + |
| 24 | + // Get input file |
| 25 | + if len(args) > 0 { |
| 26 | + inputFile = args[0] |
| 27 | + } else { |
| 28 | + inputFile, err = utils.PromptForEnvFile("Select the .env file to clear values:") |
| 29 | + if err != nil { |
| 30 | + fmt.Printf("Error: %v\n", err) |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Check if input file exists |
| 36 | + if !utils.FileExists(inputFile) { |
| 37 | + fmt.Printf("Error: File '%s' does not exist\n", inputFile) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + |
| 41 | + // First warning |
| 42 | + fmt.Printf("\n⚠️ WARNING: This operation will CLEAR ALL VALUES from '%s'\n", inputFile) |
| 43 | + fmt.Println("⚠️ This action is IRREVERSIBLE and will remove all sensitive data!") |
| 44 | + fmt.Println("⚠️ Make sure you have a backup before proceeding.") |
| 45 | + |
| 46 | + // First PIN confirmation |
| 47 | + confirmed, err := utils.ConfirmWithPin("To proceed with clearing all values, please confirm with PIN") |
| 48 | + if err != nil { |
| 49 | + fmt.Printf("Error: %v\n", err) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + if !confirmed { |
| 53 | + fmt.Println("Operation cancelled.") |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + // Second warning - final confirmation |
| 58 | + fmt.Printf("\n⚠️ FINAL WARNING: You are about to PERMANENTLY CLEAR all values in '%s'\n", inputFile) |
| 59 | + fmt.Println("⚠️ This is your LAST CHANCE to cancel this operation!") |
| 60 | + |
| 61 | + // Second confirmation (yes/no) |
| 62 | + finalConfirm, err := utils.PromptForConfirmation("Are you ABSOLUTELY SURE you want to continue?") |
| 63 | + if err != nil { |
| 64 | + fmt.Printf("Error: %v\n", err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + if !finalConfirm { |
| 68 | + fmt.Println("Operation cancelled.") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // Parse input file |
| 73 | + envVars, err := parser.ParseEnvFile(inputFile) |
| 74 | + if err != nil { |
| 75 | + fmt.Printf("Error parsing file: %v\n", err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + // Clear all values |
| 80 | + clearedVars := make([]parser.EnvVar, len(envVars)) |
| 81 | + for i, envVar := range envVars { |
| 82 | + clearedVars[i] = parser.EnvVar{ |
| 83 | + Key: envVar.Key, |
| 84 | + Value: "", |
| 85 | + Comment: envVar.Comment, |
| 86 | + BlankAfter: envVar.BlankAfter, |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Write back to file |
| 91 | + if err := parser.WriteEnvFile(inputFile, clearedVars); err != nil { |
| 92 | + fmt.Printf("Error writing file: %v\n", err) |
| 93 | + os.Exit(1) |
| 94 | + } |
| 95 | + |
| 96 | + fmt.Printf("✓ All values cleared from: %s\n", inputFile) |
| 97 | + fmt.Printf("✓ %d keys retained with empty values\n", len(clearedVars)) |
| 98 | + }, |
| 99 | + } |
| 100 | +} |
0 commit comments