first compiling version
This commit is contained in:
parent
60fb40d61a
commit
3d54199faa
27 changed files with 908 additions and 243 deletions
136
pkg/cli/cli.go
136
pkg/cli/cli.go
|
|
@ -1,3 +1,139 @@
|
|||
// Copyright (C) 2023 Marius Schellenberger
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/client"
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/core"
|
||||
)
|
||||
|
||||
func exit(err error) {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Run(arg string) {
|
||||
opts, err := apiclient.ClientOptionsFromEnv()
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
apic := apiclient.NewClient(opts...)
|
||||
c, err := client.NewClient(apic)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
switch arg {
|
||||
case "new":
|
||||
name, encoding, size := newKeyFlags()
|
||||
newKey := schema.NewKey{
|
||||
Name: name,
|
||||
Size: size,
|
||||
Encoding: core.EncodingFromString(encoding),
|
||||
}
|
||||
err = c.CreateKey(ctx, newKey)
|
||||
case "del":
|
||||
id := idFlags("key id")
|
||||
err = c.DeleteKey(ctx, id)
|
||||
case "approve":
|
||||
id := idFlags("approval id")
|
||||
err = c.AcceptApproval(ctx, id)
|
||||
case "reject":
|
||||
id := idFlags("approval id")
|
||||
err = c.RejectApproval(ctx, id)
|
||||
case "get":
|
||||
id, json := getFlags()
|
||||
key, err := c.GetKey(ctx, id)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
if json {
|
||||
jsonOutput(key) // does not return
|
||||
}
|
||||
fmt.Println(key.Key)
|
||||
case "ls":
|
||||
json := outputFlags()
|
||||
keys, err := c.GetKeys(ctx)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
if json {
|
||||
jsonOutput(keys) // does not return
|
||||
}
|
||||
for _, k := range keys {
|
||||
fmt.Printf("%s %s %d %d %s\n",
|
||||
k.ID,
|
||||
k.Name,
|
||||
k.Size,
|
||||
k.Encoding,
|
||||
k.Created,
|
||||
)
|
||||
}
|
||||
case "req":
|
||||
json := outputFlags()
|
||||
approvals, err := c.GetApprovals(ctx)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
if json {
|
||||
jsonOutput(approvals) // does not return
|
||||
}
|
||||
for _, a := range approvals {
|
||||
fmt.Printf("%s %s %s %d %s\n",
|
||||
a.ID,
|
||||
a.AID,
|
||||
a.Name,
|
||||
a.Status,
|
||||
a.Created,
|
||||
)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func jsonOutput(v any) {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func idFlags(desc string) (id string) {
|
||||
flag.StringVar(&id, "id", "", desc)
|
||||
flag.Parse()
|
||||
return
|
||||
}
|
||||
|
||||
func getFlags() (id string, json bool) {
|
||||
flag.StringVar(&id, "id", "", "key id")
|
||||
flag.BoolVar(&json, "json", false, "json output")
|
||||
flag.Parse()
|
||||
return
|
||||
}
|
||||
|
||||
func outputFlags() (json bool) {
|
||||
flag.BoolVar(&json, "json", false, "json output")
|
||||
flag.Parse()
|
||||
return
|
||||
}
|
||||
|
||||
func newKeyFlags() (name, encoding string, size int) {
|
||||
flag.StringVar(&name, "name", "", "key name")
|
||||
flag.StringVar(&encoding, "enc", "hex", "key encoding scheme [hex|base32|base64|base64url]")
|
||||
flag.IntVar(&size, "size", 16, "key size in bytes")
|
||||
flag.Parse()
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue