-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.go
More file actions
86 lines (77 loc) · 2.22 KB
/
Copy pathkeys.go
File metadata and controls
86 lines (77 loc) · 2.22 KB
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
// SPDX-FileCopyrightText: Copyright 2025 Carabiner Systems, Inc
// SPDX-License-Identifier: Apache-2.0
package command
import (
"errors"
"fmt"
"os"
"github.com/carabiner-dev/signer/key"
"github.com/spf13/cobra"
)
var _ OptionsSet = &KeyOptions{}
// KeyOptions provides key file configuration for Carabiner applications.
//
// Deprecated: Use github.com/carabiner-dev/command/keys.Options instead.
type KeyOptions struct {
config *OptionsSetConfig
PublicKeyPaths []string
}
// Config returns the flag configuration for key options.
//
// Deprecated: Use github.com/carabiner-dev/command/keys.Options instead.
func (ko *KeyOptions) Config() *OptionsSetConfig {
if ko.config == nil {
ko.config = &OptionsSetConfig{
Flags: map[string]FlagConfig{
"key": {
Short: "k",
Long: "key",
Help: "path to public key files",
},
},
}
}
return ko.config
}
// AddFlags adds the options flags to a command.
//
// Deprecated: Use github.com/carabiner-dev/command/keys.Options instead.
func (ko *KeyOptions) AddFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringSliceVarP(
&ko.PublicKeyPaths,
ko.Config().LongFlag("key"), ko.Config().ShortFlag("key"), []string{}, ko.Config().HelpText("key"),
)
}
// Validate checks the options. Key files are verified to check if they exist.
//
// Deprecated: Use github.com/carabiner-dev/command/keys.Options instead.
func (ko *KeyOptions) Validate() error {
errs := []error{}
for _, p := range ko.PublicKeyPaths {
if _, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
errs = append(errs, fmt.Errorf("checking key %q: %w", p, err))
}
}
}
return errors.Join(errs...)
}
// ParseKeys parses the key files and returns a slice of public key providers.
//
// Deprecated: Use github.com/carabiner-dev/command/keys.Options instead.
func (ko *KeyOptions) ParseKeys() ([]key.PublicKeyProvider, error) {
parser := key.NewParser()
r := []key.PublicKeyProvider{}
for _, path := range ko.PublicKeyPaths {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading key file: %w", err)
}
k, err := parser.ParsePublicKey(data)
if err != nil {
return nil, fmt.Errorf("parsing key %q: %w", path, err)
}
r = append(r, k)
}
return r, nil
}