mirror of https://git.jolheiser.com/git-age
94 lines
1.7 KiB
Go
94 lines
1.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var Add = &cli.Command{
|
|
Name: "add",
|
|
Aliases: []string{"a"},
|
|
Description: "Add a new file for encryption",
|
|
Flags: []cli.Flag{
|
|
&cli.StringSliceFlag{
|
|
Name: "recipient",
|
|
Aliases: []string{"r"},
|
|
},
|
|
},
|
|
Action: actionAdd,
|
|
}
|
|
|
|
func actionAdd(ctx *cli.Context) error {
|
|
if ctx.NArg() != 1 {
|
|
return errors.New("add requires 1 argument")
|
|
}
|
|
file := ctx.Args().First()
|
|
|
|
ok, err := hasAttr(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
base, err := gitBaseDir()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
attrFile := filepath.Join(base, ".gitattributes")
|
|
fi, err := os.OpenFile(attrFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fi.Close()
|
|
fmt.Fprintf(fi, "\n%s filter=git-age diff=git-age", file)
|
|
}
|
|
|
|
cfg, err := LoadConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ok, err = cfg.Includes(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if ok {
|
|
return nil
|
|
}
|
|
|
|
recipients := ctx.StringSlice("recipient")
|
|
if len(recipients) == 0 {
|
|
|
|
var recipientOpts []string
|
|
for _, recs := range cfg {
|
|
for _, rec := range recs {
|
|
if !slices.Contains(recipientOpts, rec) {
|
|
recipientOpts = append(recipientOpts, rec)
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(recipientOpts) == 0 {
|
|
return errors.New("no identities configured; git age ident --help")
|
|
}
|
|
|
|
if err := survey.AskOne(&survey.MultiSelect{
|
|
Message: "Identities",
|
|
Options: recipientOpts,
|
|
}, &recipients); err != nil {
|
|
return err
|
|
}
|
|
if len(recipients) == 0 {
|
|
return errors.New("no identity provided, please add manually to .git-age.yaml")
|
|
}
|
|
}
|
|
|
|
cfg[file] = recipients
|
|
return SaveConfig(cfg)
|
|
}
|