/
token.go
80 lines (70 loc) · 2.96 KB
/
token.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
78
79
80
package commands
import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
cloudapiv6 "github.com/ionos-cloud/ionosctl/v6/services/cloudapi-v6"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
defaultTokenCols = []string{"Token"}
)
func ServerTokenCmd() *core.Command {
ctx := context.TODO()
tokenCmd := &core.Command{
Command: &cobra.Command{
Use: "token",
Aliases: []string{"t"},
Short: "Server Token Operations",
Long: "The sub-command of `ionosctl server token` allows you to get Token for specific Server.",
TraverseChildren: true,
},
}
/*
Get Command
*/
get := core.NewCommand(ctx, tokenCmd, core.CommandBuilder{
Namespace: "server",
Resource: "token",
Verb: "get",
Aliases: []string{"g"},
ShortDesc: "Get a Token from a Server",
LongDesc: "Use this command to get the Server's jwToken.\n\nRequired values to run command:\n\n* Data Center Id\n* Server Id",
Example: getTokenServerExample,
PreCmdRun: PreRunDcServerIds,
CmdRun: RunServerTokenGet,
InitClient: true,
})
get.AddUUIDFlag(cloudapiv6.ArgDataCenterId, "", "", cloudapiv6.DatacenterId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgDataCenterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DataCentersIds(), cobra.ShellCompDirectiveNoFileComp
})
get.AddUUIDFlag(cloudapiv6.ArgServerId, cloudapiv6.ArgIdShort, "", cloudapiv6.ServerId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgServerId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ServersIds(viper.GetString(core.GetFlagName(get.NS, cloudapiv6.ArgDataCenterId))), cobra.ShellCompDirectiveNoFileComp
})
get.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultGetDepth, cloudapiv6.ArgDepthDescription)
return tokenCmd
}
func RunServerTokenGet(c *core.CommandConfig) error {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
"ServerToken with id: %v is getting...", viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgServerId))))
t, _, err := c.CloudApiV6Services.Servers().GetToken(
viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgDataCenterId)),
viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgServerId)),
)
if err != nil {
return err
}
out, err := jsontabwriter.GenerateOutput("", jsonpaths.Token, t.Token, tabheaders.GetHeadersAllDefault(defaultTokenCols, nil))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}