aboutsummaryrefslogtreecommitdiff
path: root/cmd/eno/cobra.go
blob: ced2bc2c2972ca353c124b39c693d3c3ec9d4399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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 <file>",
	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)
		}
		crypt.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)
		}
		crypt.TryMlock(password)
		defer clear(password)

		encrypted, err := crypt.EncryptBytesPass(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 <file>",
	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)
		}
		crypt.TryMlock(password)
		defer clear(password)

		plaintext, err := crypt.DecryptBytesPass(fileBytes, password)
		if err != nil {
			cmd.PrintErrln("Error:", err)
			os.Exit(1)
		}
		crypt.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 editCmd = &cobra.Command{
	Use: "edit <note>",
	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"

		authMode, err := utils.CheckFileAuthMode(target)
		if err != nil {
			cmd.PrintErrln("Error:", err)
			os.Exit(1)
		}

		switch authMode {
		case crypt.SSH:
			err = crud.EditNoteSSH(target, Conf.DefaultSSHPrivPath, Conf.EditorString)
		case crypt.Pass:
			err = crud.EditNotePass(target, Conf.EditorString)
		default:
			cmd.PrintErrln("Error: unknown auth mode:", authMode)
			os.Exit(1)
		}
		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")
	newCmd.Flags().Bool("ssh", false, "use your ssh for auth")

	sshCmd.AddCommand(sshInitCmd)

	cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd, sshCmd)
}