package main import ( "fmt" "os" "path/filepath" "strings" "github.com/spf13/cobra" "gitlab.com/whiteboardguy/eno/internal/crud" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/utils" ) var cobraCmd = &cobra.Command{ Use: "eno", Short: "file encryption tool", } var encryptCmd = &cobra.Command{ Use: "encrypt ", Short: "encrypt a file", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { output, err := cmd.Flags().GetString("output") if err != nil { return fmt.Errorf("failed to get output flag: %v", err) } if output == "" { output = args[0] + ".eno" } if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { return err } if !ok { return fmt.Errorf("write aborted") } } fileBytes, err := os.ReadFile(args[0]) if err != nil { return fmt.Errorf("failed to read input file: %v", err) } utils.TryMlock(fileBytes) defer clear(fileBytes) fmt.Printf("Enter a passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } utils.TryMlock(password) defer clear(password) encrypted, err := crypt.EncryptRawBytes(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if err := os.WriteFile(output, encrypted, 0644); err != nil { return fmt.Errorf("failed to write output file: %v", err) } return nil }, } var decryptCmd = &cobra.Command{ Use: "decrypt ", Short: "decrypt a file", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { output, err := cmd.Flags().GetString("output") if err != nil { return fmt.Errorf("failed to get output flag: %v", err) } if output == "" { output = strings.TrimSuffix(args[0], ".eno") } if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { return err } if !ok { return fmt.Errorf("write aborted") } } fileBytes, err := os.ReadFile(args[0]) if err != nil { return fmt.Errorf("failed to read input file: %v", err) } fmt.Printf("Enter the passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } utils.TryMlock(password) defer clear(password) plaintext, err := crypt.DecryptRawBytes(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } utils.TryMlock(plaintext) defer clear(plaintext) if err := os.WriteFile(output, plaintext, 0644); err != nil { return fmt.Errorf("failed to write output file: %v", err) } return nil }, } var initCmd = &cobra.Command{ Use: "init", Short: "intialize Eno config", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { err := handlerInit() if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } cmd.Println("Config initialized at ", Conf.ConfPath) }, } var newCmd = &cobra.Command{ Use: "new ", Short: "create a new encrypted note", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { output, err := cmd.Flags().GetString("output") if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } editFlag, err := cmd.Flags().GetBool("edit") if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } noteName := strings.Join(args, " ") if output == "" { if strings.ContainsRune(noteName, filepath.Separator) { output = noteName + ".eno" } else { output = filepath.Join(Conf.DataDir, noteName+".eno") } } if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if !ok { cmd.PrintErrln("Error: write aborted") os.Exit(1) } } err = crud.NewNote(noteName, output) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if editFlag { target := filepath.Clean(output) target = strings.TrimSuffix(target, ".eno") target += ".eno" err = crud.EditNote(target, Conf.EditorString) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } } }, } var editCmd = &cobra.Command{ Use: "edit ", Short: "edit an existing encrypted note", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { target := filepath.Clean(args[0]) ok, err := utils.AskUserYesNo("Edit "+target+" ?", true) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if !ok { cmd.PrintErrln("Error: write aborted") os.Exit(1) } target = strings.TrimSuffix(target, ".eno") target += ".eno" err = crud.EditNote(target, Conf.EditorString) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } }, } func init() { encryptCmd.Flags().StringP("output", "o", "", "output file (required)") // encryptCmd.MarkFlagRequired("output") decryptCmd.Flags().StringP("output", "o", "", "output file (required)") // decryptCmd.MarkFlagRequired("output") newCmd.Flags().StringP("output", "o", "", "output file") newCmd.Flags().BoolP("edit", "e", false, "edit after creation") cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd) }