git-age/cmd/config.go

71 lines
1.5 KiB
Go
Raw Normal View History

package cmd
import (
"fmt"
"os"
"filippo.io/age"
"filippo.io/age/agessh"
"github.com/bmatcuk/doublestar/v4"
"gopkg.in/yaml.v3"
)
// Config is the configuration for git-age
type Config map[string]Recipients
func (c Config) Includes(file string) (bool, error) {
for glob := range c {
ok, err := doublestar.Match(glob, file)
if err != nil {
return false, fmt.Errorf("bad glob %q: %w", glob, err)
}
if ok {
return true, nil
}
}
return false, nil
}
type Recipients []string
// Recipients parses age recipients from recipient strings
func (r Recipients) Recipients() ([]age.Recipient, error) {
var recipients []age.Recipient
for _, rstr := range r {
var rec age.Recipient
rec, err := age.ParseX25519Recipient(rstr)
if err != nil {
if debug {
fmt.Fprintf(os.Stderr, "could not parse recipient %q with age, trying ssh next: %v\n", rstr, err)
}
rec, err = agessh.ParseRecipient(rstr)
if err != nil {
return recipients, err
}
}
recipients = append(recipients, rec)
}
return recipients, nil
}
// LoadConfig decodes .git-age.yaml into a Config
func LoadConfig() (Config, error) {
var cfg Config
fi, err := os.Open(".git-age.yaml")
if err != nil {
return cfg, err
}
defer fi.Close()
return cfg, yaml.NewDecoder(fi).Decode(&cfg)
}
// SaveConfig encodes a Config into .git-age.yaml
func SaveConfig(cfg Config) error {
fi, err := os.Create(".git-age.yaml")
if err != nil {
return err
}
defer fi.Close()
return yaml.NewEncoder(fi).Encode(cfg)
}