fixed flags
This commit is contained in:
parent
3d54199faa
commit
398ff981d1
3 changed files with 41 additions and 23 deletions
|
|
@ -45,10 +45,10 @@ func usage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) == 0 {
|
if len(os.Args) < 2 {
|
||||||
usage()
|
usage()
|
||||||
}
|
}
|
||||||
arg := os.Args[0]
|
arg := os.Args[1]
|
||||||
switch arg {
|
switch arg {
|
||||||
case "new", "get", "ls", "req", "approve", "reject", "del":
|
case "new", "get", "ls", "req", "approve", "reject", "del":
|
||||||
//flag.StringVar(&config, "config", "/etc/keyctl/url.conf", "path to client config file")
|
//flag.StringVar(&config, "config", "/etc/keyctl/url.conf", "path to client config file")
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ type NewKey struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
ErrEmptyKeyName = errors.New("empty key name")
|
||||||
ErrInvalidKeyName = errors.New("invalid key name")
|
ErrInvalidKeyName = errors.New("invalid key name")
|
||||||
ErrInvalidKeySize = errors.New("invalid key size")
|
ErrInvalidKeySize = errors.New("invalid key size")
|
||||||
)
|
)
|
||||||
|
|
@ -35,6 +36,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (nk NewKey) Validate() error {
|
func (nk NewKey) Validate() error {
|
||||||
|
if nk.Name == "" {
|
||||||
|
return ErrEmptyKeyName
|
||||||
|
}
|
||||||
if core.NameRe.MatchString(nk.Name) {
|
if core.NameRe.MatchString(nk.Name) {
|
||||||
return ErrInvalidKeyName
|
return ErrInvalidKeyName
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,26 +10,20 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
|
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
|
||||||
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/client"
|
clientv1 "git.giftfish.de/ston1th/keyctl/pkg/api/v1/client"
|
||||||
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
|
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
|
||||||
"git.giftfish.de/ston1th/keyctl/pkg/core"
|
"git.giftfish.de/ston1th/keyctl/pkg/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var fs = flag.NewFlagSet("", flag.ExitOnError)
|
||||||
|
|
||||||
func exit(err error) {
|
func exit(err error) {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(arg string) {
|
func Run(arg string) {
|
||||||
opts, err := apiclient.ClientOptionsFromEnv()
|
var err error
|
||||||
if err != nil {
|
|
||||||
exit(err)
|
|
||||||
}
|
|
||||||
apic := apiclient.NewClient(opts...)
|
|
||||||
c, err := client.NewClient(apic)
|
|
||||||
if err != nil {
|
|
||||||
exit(err)
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
switch arg {
|
switch arg {
|
||||||
case "new":
|
case "new":
|
||||||
|
|
@ -39,18 +33,23 @@ func Run(arg string) {
|
||||||
Size: size,
|
Size: size,
|
||||||
Encoding: core.EncodingFromString(encoding),
|
Encoding: core.EncodingFromString(encoding),
|
||||||
}
|
}
|
||||||
|
c := client()
|
||||||
err = c.CreateKey(ctx, newKey)
|
err = c.CreateKey(ctx, newKey)
|
||||||
case "del":
|
case "del":
|
||||||
id := idFlags("key id")
|
id := idFlags("key id")
|
||||||
|
c := client()
|
||||||
err = c.DeleteKey(ctx, id)
|
err = c.DeleteKey(ctx, id)
|
||||||
case "approve":
|
case "approve":
|
||||||
id := idFlags("approval id")
|
id := idFlags("approval id")
|
||||||
|
c := client()
|
||||||
err = c.AcceptApproval(ctx, id)
|
err = c.AcceptApproval(ctx, id)
|
||||||
case "reject":
|
case "reject":
|
||||||
id := idFlags("approval id")
|
id := idFlags("approval id")
|
||||||
|
c := client()
|
||||||
err = c.RejectApproval(ctx, id)
|
err = c.RejectApproval(ctx, id)
|
||||||
case "get":
|
case "get":
|
||||||
id, json := getFlags()
|
id, json := getFlags()
|
||||||
|
c := client()
|
||||||
key, err := c.GetKey(ctx, id)
|
key, err := c.GetKey(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
|
|
@ -61,6 +60,7 @@ func Run(arg string) {
|
||||||
fmt.Println(key.Key)
|
fmt.Println(key.Key)
|
||||||
case "ls":
|
case "ls":
|
||||||
json := outputFlags()
|
json := outputFlags()
|
||||||
|
c := client()
|
||||||
keys, err := c.GetKeys(ctx)
|
keys, err := c.GetKeys(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
|
|
@ -79,6 +79,7 @@ func Run(arg string) {
|
||||||
}
|
}
|
||||||
case "req":
|
case "req":
|
||||||
json := outputFlags()
|
json := outputFlags()
|
||||||
|
c := client()
|
||||||
approvals, err := c.GetApprovals(ctx)
|
approvals, err := c.GetApprovals(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
|
|
@ -102,6 +103,19 @@ func Run(arg string) {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func client() (c *clientv1.Client) {
|
||||||
|
opts, err := apiclient.ClientOptionsFromEnv()
|
||||||
|
if err != nil {
|
||||||
|
exit(err)
|
||||||
|
}
|
||||||
|
apic := apiclient.NewClient(opts...)
|
||||||
|
c, err = clientv1.NewClient(apic)
|
||||||
|
if err != nil {
|
||||||
|
exit(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func jsonOutput(v any) {
|
func jsonOutput(v any) {
|
||||||
b, err := json.Marshal(v)
|
b, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -112,28 +126,28 @@ func jsonOutput(v any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func idFlags(desc string) (id string) {
|
func idFlags(desc string) (id string) {
|
||||||
flag.StringVar(&id, "id", "", desc)
|
fs.StringVar(&id, "id", "", desc)
|
||||||
flag.Parse()
|
fs.Parse(os.Args[2:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFlags() (id string, json bool) {
|
func getFlags() (id string, json bool) {
|
||||||
flag.StringVar(&id, "id", "", "key id")
|
fs.StringVar(&id, "id", "", "key id")
|
||||||
flag.BoolVar(&json, "json", false, "json output")
|
fs.BoolVar(&json, "json", false, "json output")
|
||||||
flag.Parse()
|
fs.Parse(os.Args[2:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputFlags() (json bool) {
|
func outputFlags() (json bool) {
|
||||||
flag.BoolVar(&json, "json", false, "json output")
|
fs.BoolVar(&json, "json", false, "json output")
|
||||||
flag.Parse()
|
fs.Parse(os.Args[2:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func newKeyFlags() (name, encoding string, size int) {
|
func newKeyFlags() (name, encoding string, size int) {
|
||||||
flag.StringVar(&name, "name", "", "key name")
|
fs.StringVar(&name, "name", "", "key name")
|
||||||
flag.StringVar(&encoding, "enc", "hex", "key encoding scheme [hex|base32|base64|base64url]")
|
fs.StringVar(&encoding, "enc", "hex", "key encoding scheme [hex|base32|base64|base64url]")
|
||||||
flag.IntVar(&size, "size", 16, "key size in bytes")
|
fs.IntVar(&size, "size", 16, "key size in bytes")
|
||||||
flag.Parse()
|
fs.Parse(os.Args[2:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue