Home

git-age @main - refs - log -
-
https://git.jolheiser.com/git-age.git
git-crypt, but with age
git-age / cmd / identity.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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
package cmd

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/urfave/cli/v2"
)

var Identity = &cli.Command{
	Name:        "identity",
	Aliases:     []string{"ident"},
	Description: "Manage identity files",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "remove",
			Aliases: []string{"r"},
			Usage:   "Remove an identity file",
		},
		&cli.BoolFlag{
			Name:    "list",
			Aliases: []string{"l"},
			Usage:   "List identity files",
		},
	},

	Action: actionIdentity,
}

func actionIdentity(ctx *cli.Context) error {
	keyFiles, err := listIdentities()
	if err != nil && !errors.Is(err, ErrNoIdentities) {
		return err
	}

	if ctx.Bool("list") {
		return actionListIdentities(ctx, keyFiles)
	}

	if ctx.NArg() != 1 {
		return errors.New("identity requires 1 argument")
	}

	file := ctx.Args().First()
	if strings.HasPrefix(file, "~") {
		home, err := os.UserHomeDir()
		if err != nil {
			return err
		}
		file = filepath.Join(home, strings.TrimPrefix(file, "~"))
	}
	file = os.ExpandEnv(file)
	file, err = filepath.Abs(file)
	if err != nil {
		return err
	}

	exists := -1
	for idx, keyFile := range keyFiles {
		if file == keyFile {
			exists = idx
			break
		}
	}

	if exists >= 0 && !ctx.Bool("remove") {
		return fmt.Errorf("identity file %q already exists", file)
	} else if exists == -1 && ctx.Bool("remove") {
		return fmt.Errorf("identity file %q doesn't exist", file)
	}

	if ctx.Bool("remove") {
		keyFiles = append(keyFiles[:exists], keyFiles[exists+1:]...)
	} else {
		if _, err := parseIdentityFile(file); err != nil {
			return fmt.Errorf("could not add invalid identity file: %w", err)
		}
		keyFiles = append(keyFiles, file)
	}

	cmd("git", "config", "--unset-all", "git-age.identity")
	for _, keyFile := range keyFiles {
		if _, err := cmd("git", "config", "--add", "git-age.identity", keyFile); err != nil {
			return err
		}
	}

	return nil
}

func actionListIdentities(ctx *cli.Context, files []string) error {
	var sb strings.Builder
	for _, file := range files {
		_, err := parseIdentityFile(file)
		valid := "✓"
		if err != nil {
			valid = "x"
		}
		sb.WriteString(fmt.Sprintf("%s %q\n", valid, file))
	}
	fmt.Print(sb.String())
	return nil
}