2023-08-03 19:44:07 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"filippo.io/age"
|
|
|
|
"filippo.io/age/agessh"
|
2023-11-13 22:17:21 +00:00
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
2023-08-03 19:44:07 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration for git-age
|
|
|
|
type Config map[string]Recipients
|
|
|
|
|
2023-11-13 22:17:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:44:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-13 22:17:21 +00:00
|
|
|
// LoadConfig decodes .git-age.yaml into a Config
|
2023-08-03 19:44:07 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-11-13 22:17:21 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|