-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregister.go
More file actions
77 lines (63 loc) · 2.06 KB
/
register.go
File metadata and controls
77 lines (63 loc) · 2.06 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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"fmt"
"time"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/spf13/cobra"
)
type RegisterCmd struct {
cmd *cobra.Command
Opts RegisterOpts
}
type RegisterOpts struct {
GlobalOptions
Owner string
Organization string
Role string
ExpiresAt string
}
func (c *RegisterCmd) RunE(_ *cobra.Command, args []string) error {
p := portal.NewPortalClient()
newKey, err := c.Register(p)
if err != nil {
return err
}
if newKey != nil {
fmt.Printf("API key registered successfully!\nOwner: %s\nOrganisation: %s\nKey: %s\n", newKey.Owner, newKey.Organization, newKey.ApiKey)
}
return nil
}
func (c *RegisterCmd) Register(p portal.Portal) (*portal.ApiKey, error) {
var err error
var expiresAt time.Time
if c.Opts.ExpiresAt != "" {
expiresAt, err = time.Parse(time.RFC3339, c.Opts.ExpiresAt)
if err != nil {
return nil, fmt.Errorf("failed to parse expiration date: %w", err)
}
}
newKey, err := p.RegisterAPIKey(c.Opts.Owner, c.Opts.Organization, c.Opts.Role, expiresAt)
if err != nil {
return nil, fmt.Errorf("failed to register API key: %w", err)
}
return newKey, nil
}
func AddRegisterCmd(list *cobra.Command, opts GlobalOptions) {
c := RegisterCmd{
cmd: &cobra.Command{
Use: "register",
Short: "Register a new API key",
Long: io.Long(`Register a new API key for accessing the OMS portal.`),
},
Opts: RegisterOpts{GlobalOptions: opts},
}
c.cmd.Flags().StringVarP(&c.Opts.Owner, "owner", "o", "", "Owner of the new API key")
c.cmd.Flags().StringVarP(&c.Opts.Organization, "organization", "g", "", "Organization of the new API key")
c.cmd.Flags().StringVarP(&c.Opts.Role, "role", "r", "Ext", "Role of the new API key. Available roles: Admin, Dev, Ext")
c.cmd.Flags().StringVarP(&c.Opts.ExpiresAt, "expires", "e", "", "Expiration date of the new API key. Default is 1 year from now. Format: RFC3339 (e.g., 2024-12-31T23:59:59Z)")
c.cmd.RunE = c.RunE
list.AddCommand(c.cmd)
}