-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (86 loc) · 2.22 KB
/
main.go
File metadata and controls
98 lines (86 loc) · 2.22 KB
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/brettbuddin/git-credential-op/internal/subcmd"
"github.com/brettbuddin/git-credential-op/internal/subcmd/op"
)
func main() {
err := run(os.Args[1:])
if err == nil {
return
}
if errors.Is(err, flag.ErrHelp) {
os.Exit(2)
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func run(args []string) error {
var (
titleFormat string
locatorTag string
account string
vault string
additionalTags string
)
fs := flag.NewFlagSet("git-credential-op", flag.ExitOnError)
fs.StringVar(&titleFormat, "title", subcmd.DefaultTitleFormat, "item title format")
fs.StringVar(&locatorTag, "locator-tag", op.DefaultLocatorTag, "locator tag value")
fs.StringVar(&account, "account", "", "account URL (e.g. mycompany.1password.com")
fs.StringVar(&vault, "vault", "", "vault name (e.g. Private)")
fs.StringVar(&additionalTags, "additional-tags", "", "comma separated list of additional tags to include in managed items")
fs.Usage = usageFn(fs)
if err := fs.Parse(args); err != nil {
return err
}
args = fs.Args()
if len(args) != 1 {
fs.Usage()
return flag.ErrHelp
}
if !op.InPath() {
return fmt.Errorf(`op (1Password) not found in PATH`)
}
runner := &op.Runner{
LocatorTag: locatorTag,
Account: account,
Vault: vault,
Executor: op.DefaultExecutor(),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
switch args[0] {
case "get":
return subcmd.Get(runner)
case "store":
return subcmd.Store(runner, titleFormat, strings.Split(additionalTags, ","))
case "erase":
return subcmd.Erase(runner)
default:
// To fully conform with the git-credential contract we must ignore any subcommands we don't
// recognize. See https://git-scm.com/docs/gitcredentials#_custom_helpers
return nil
}
}
var usage = `git-credential-op is a git-credential helper for storing credentials in 1Password.
Usage:
git-credential-op (get|store|erase)
Setup with Git:
# .gitconfig OR .git/config
[credential]
helper = op
`
func usageFn(fs *flag.FlagSet) func() {
return func() {
out := fs.Output()
fmt.Fprintln(out, usage)
fmt.Fprint(out, "Flags:\n\n")
fs.PrintDefaults()
fmt.Fprintln(out)
}
}