Home

git-age @main - refs - log - search -
https://git.jolheiser.com/git-age.git
git-crypt, but with age
cmd/config.go - raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
}