1 Commits
Author SHA1 Message Date
sneak 9a74d2e18c Hold the encrypt and decrypt result in memory until it is written (closes #143)
check / check (push) Canceled after 0s
With --output, secret encrypt and secret decrypt emptied that file before
reading any input, so a failed command left it empty, and encrypting a file
onto itself gave the encryption of nothing.

With --output they now hold the result in memory and open --output, with the
same call as before, only once encryption or decryption has succeeded. A
failed command leaves --output untouched, and --input can be the same file.
Without --output the result still goes to stdout as it is produced.

Model: opus-5-5
2026-10-15 00:01:14 +00:00
+4 -6
View File
@@ -220,7 +220,7 @@ func (cli *Instance) Encrypt(secretName, inputFile, outputFile string) error {
return nil
}
return cli.writeOutputFile(outputFile, &result)
return cli.writeOutputFile(outputFile, result.Bytes())
}
// Decrypt decrypts data using an age secret key stored in a secret
@@ -307,21 +307,19 @@ func (cli *Instance) Decrypt(secretName, inputFile, outputFile string) error {
return nil
}
return cli.writeOutputFile(outputFile, &result)
return cli.writeOutputFile(outputFile, result.Bytes())
}
// writeOutputFile creates outputFile, or empties it if it exists, and
// writes result into it.
func (cli *Instance) writeOutputFile(
outputFile string, result *bytes.Buffer,
) error {
func (cli *Instance) writeOutputFile(outputFile string, result []byte) error {
file, err := cli.fs.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer func() { _ = file.Close() }()
_, err = result.WriteTo(file)
_, err = file.Write(result)
if err != nil {
return fmt.Errorf("failed to write output file: %w", err)
}