-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
80 lines (69 loc) · 2.11 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/debug"
"github.com/ethrgeist/git-credential-1password/internal"
)
// versioning is not yet implemented
var version = "main"
// getVersion returns the version of the binary
func getVersion() string {
info, ok := debug.ReadBuildInfo()
if ok && version == "main" {
version = info.Main.Version
}
return version
}
// PrintVersion prints the version of the binary
func PrintVersion() {
fmt.Fprintf(os.Stderr, "git-credential-1password %s\n", getVersion())
}
func main() {
flag.StringVar(&internal.Account, "account", "my", "1Password account")
flag.StringVar(&internal.Vault, "vault", "Private", "1Password vault")
flag.StringVar(&internal.Prefix, "prefix", "", "1Password item name prefix")
versionFlag := flag.Bool("version", false, "Print version")
flag.Usage = func() {
PrintVersion()
fmt.Fprintln(os.Stderr, "usage: git credential-1password [<options>] <action>")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Actions:")
fmt.Fprintln(os.Stderr, " get Generate credential [called by Git]")
fmt.Fprintln(os.Stderr, " store Store credential [called by Git]")
fmt.Fprintln(os.Stderr, " erase Erase credential [called by Git]")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "See also https://github.com/ethrgeist/git-credential-1password")
}
flag.Parse()
if *versionFlag {
PrintVersion()
os.Exit(0)
}
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(2)
}
// set op cli parameters based on flags
internal.OpItemFlags = append(internal.OpItemFlags, "--account", internal.Account)
internal.OpItemFlags = append(internal.OpItemFlags, "--vault", internal.Vault)
// git provides argument via stdin
// ref: https://git-scm.com/docs/gitcredentials
switch args[0] {
case "get":
internal.GetCommand()
case "store":
internal.StoreCommand()
case "erase":
internal.EraseCommand()
default:
// unknown argument
log.Fatalf("It doesn't look like anything to me. (Unknown argument: %s)\n", args[0])
}
}