-
Notifications
You must be signed in to change notification settings - Fork 8
/
flags.go
77 lines (63 loc) · 2.15 KB
/
flags.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
package database
import (
"encoding/hex"
"fmt"
"github.com/kwilteam/kwil-db/cmd/kwil-cli/config"
"github.com/kwilteam/kwil-db/core/crypto/auth"
"github.com/kwilteam/kwil-db/core/utils"
"github.com/spf13/cobra"
)
const (
dbidFlag = "dbid"
nameFlag = "name"
ownerFlag = "owner"
actionNameFlag = "action"
)
// getSelectedOwner is used to get the owner flag. Since the owner flag is usually optional,
// it will check to see if it was passed. If it was not passed, it will attempt to
// get the user's public key from the configuration file.
func getSelectedOwner(cmd *cobra.Command, conf *config.KwilCliConfig) ([]byte, error) {
var ident []byte
if cmd.Flags().Changed(ownerFlag) {
hexIdent, err := cmd.Flags().GetString(ownerFlag) // hex owner
if err != nil {
return nil, fmt.Errorf("failed to get public key from flag: %w", err)
}
// if it begins with 0x, remove it
if len(hexIdent) > 2 && hexIdent[:2] == "0x" {
hexIdent = hexIdent[2:]
}
ident, err = hex.DecodeString(hexIdent)
if err != nil {
return nil, fmt.Errorf("failed to decode public key: %w", err)
}
} else {
if conf.PrivateKey == nil {
return nil, nil // nil is a valid owner, as it will return all
}
signer := auth.EthPersonalSigner{Key: *conf.PrivateKey}
ident = signer.Identity()
}
return ident, nil
}
// getSelectedDbid returns the Dbid selected by the user.
// Since the user can pass either a name and owner, or a dbid, we need to
// check which one they passed and return the appropriate dbid.
// If only a name flag is passed, it will get the owner from the configuration file.
func getSelectedDbid(cmd *cobra.Command, conf *config.KwilCliConfig) (string, error) {
if cmd.Flags().Changed(dbidFlag) {
return cmd.Flags().GetString(dbidFlag)
}
if !cmd.Flags().Changed(nameFlag) {
return "", fmt.Errorf("neither dbid nor name was provided")
}
name, err := cmd.Flags().GetString(nameFlag)
if err != nil {
return "", fmt.Errorf("failed to get name from flag: %w", err)
}
owner, err := getSelectedOwner(cmd, conf)
if err != nil {
return "", fmt.Errorf("failed to get owner flag: %w", err)
}
return utils.GenerateDBID(name, owner), nil
}