git-age/cmd/smudge.go

64 lines
1.1 KiB
Go
Raw Normal View History

package cmd
import (
"io"
"os"
"path/filepath"
"filippo.io/age"
"github.com/urfave/cli/v2"
"lukechampine.com/blake3"
)
var Smudge = &cli.Command{
Name: "smudge",
Aliases: []string{"s"},
Description: "Git smudge filter",
Hidden: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Usage: "File being worked on",
Aliases: []string{"f"},
},
},
Action: actionSmudge,
}
func actionSmudge(ctx *cli.Context) error {
identities, err := ageIdentities()
if err != nil {
return err
}
dir, err := gitConfigDir(ctx.String("file"))
if err != nil {
return err
}
hashFile, err := os.Create(filepath.Join(dir, "blake3"))
if err != nil {
return err
}
defer hashFile.Close()
ageFile, err := os.Create(filepath.Join(dir, "age"))
if err != nil {
return err
}
defer ageFile.Close()
hasher := blake3.New(64, nil)
defer hashFile.Write(hasher.Sum(nil))
ageTee := io.TeeReader(os.Stdin, ageFile)
decrypted, err := age.Decrypt(ageTee, identities...)
if err != nil {
return err
}
hashTee := io.TeeReader(decrypted, hasher)
if _, err := io.Copy(os.Stdout, hashTee); err != nil {
return err
}
return nil
}