-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (94 loc) · 3.1 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"embed"
"fmt"
"log"
"github.com/ryboe/q"
"github.com/spewerspew/spew"
"github.com/VonC/git-cred/internal/credhelper"
"github.com/VonC/git-cred/version"
"github.com/alecthomas/kong"
)
// CLI stores arguments and subcommands
type CLI struct {
Servername string `help:"repository hosting server name (hostname). If not set, use the one from current repository folder, if present in pwd" short:"s" type:"string"`
Debug bool `help:"if true, print Debug information." type:"bool" short:"d" env:"DEBUG"`
Username string `help:"Get: username. If not set, use the one from from current repository remote URL, if present in pwd" short:"u"`
Get GetCmd `cmd:"" help:"get password for a given servername and username: can read those from current folder repository" name:"get" default:""`
Set SetCmd `cmd:"" help:"[password] set user password for a given host: -u/--username and -s/--servername mandatory" name:"set" aliases:"store"`
Erase EraseCmd `cmd:"" help:"erase password for a given host and username: -u/--username and -s/--servername mandatory" name:"erase" aliases:"rm,del,delete,remove"`
ch CredHelper
Version VersionFlag `name:"version" help:"Print version information and quit" short:"v" type:"counter"`
VersionC VersionCmd `cmd:"" help:"Show the version information" name:"version" aliases:"ver"`
versionFs embed.FS
}
type VersionFlag int
type SetCmd struct {
Password string `arg:"" help:"user password or token" short:"p"`
}
type EraseCmd struct{}
type GetCmd struct{}
func fatal(msg string, err error) {
if err != nil {
log.Fatalf("%s: error '%+v'", msg, err)
}
}
type VersionCmd struct{}
type CredHelper interface {
Get(username string, servername string) (string, error)
Set(username, password, servername string) error
Erase(username, servername string) error
}
type Context struct {
*CLI
}
// https://github.com/golang/go/issues/41191
// https://stackoverflow.com/a/67357103/6309
//
//go:embed version/*
var versionFs embed.FS
// git-cred main entry
func main() {
var err error
var cli CLI
ctx := kong.Parse(&cli)
if cli.Debug {
spew.Dump(cli)
q.Q(cli)
fmt.Printf("ctx command '%s'\n", ctx.Command())
}
cli.versionFs = versionFs
if ctx.Command() != "version" && cli.Version > 0 {
fmt.Println(version.String(int(cli.Version), cli.versionFs))
ctx.Exit(0)
}
if ctx.Command() != "version" {
var ch CredHelper
ch, err = credhelper.NewCredHelper(cli.Servername, cli.Username)
fatal("Unable to get Credential Helper", err)
cli.ch = ch
}
err = ctx.Run(&Context{CLI: &cli})
fatal("gitcred Unable to run:", err)
}
func (s *GetCmd) Run(c *Context) error {
get, err := c.ch.Get(c.Username, c.Servername)
if err != nil {
return err
}
fmt.Println(get)
return nil
}
func (s *SetCmd) Run(c *Context) error {
err := c.ch.Set(c.Username, s.Password, c.Servername)
return err
}
func (e *EraseCmd) Run(c *Context) error {
err := c.ch.Erase(c.Username, c.Servername)
return err
}
func (v *VersionCmd) Run(c *Context) error {
//spew.Dump(c)
fmt.Println(version.String(int(c.Version)+1, c.versionFs))
return nil
}