This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
policy_list.go
117 lines (95 loc) · 3.24 KB
/
policy_list.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2020 Talhuang<talhuang1231@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package policy
import (
"bytes"
"context"
"fmt"
"github.com/fatih/color"
"github.com/marmotedu/component-base/pkg/json"
metav1 "github.com/marmotedu/component-base/pkg/meta/v1"
"github.com/marmotedu/marmotedu-sdk-go/marmotedu/service/iam"
"github.com/spf13/cobra"
cmdutil "github.com/skeleton1231/go-iam-ecommerce-microservice/internal/iamctl/cmd/util"
"github.com/skeleton1231/go-iam-ecommerce-microservice/internal/iamctl/util/templates"
"github.com/skeleton1231/go-iam-ecommerce-microservice/pkg/cli/genericclioptions"
)
const (
defaultLimit = 1000
)
// ListOptions is an options struct to support list subcommands.
type ListOptions struct {
Offset int64
Limit int64
iamclient iam.IamInterface
genericclioptions.IOStreams
}
var listExample = templates.Examples(`
# Display all policy resources
iamctl poicy list
# Display all policy resources with offset and limit
iamctl policy list --offset=0 --limit=10`)
// NewListOptions returns an initialized ListOptions instance.
func NewListOptions(ioStreams genericclioptions.IOStreams) *ListOptions {
return &ListOptions{
Offset: 0,
Limit: defaultLimit,
IOStreams: ioStreams,
}
}
// NewCmdList returns new initialized instance of list sub command.
func NewCmdList(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := NewListOptions(ioStreams)
cmd := &cobra.Command{
Use: "list",
DisableFlagsInUseLine: true,
Aliases: []string{},
Short: "Display all authorization policy resources",
TraverseChildren: true,
Long: "Display all authorization policy resources.",
Example: listExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(f, cmd, args))
cmdutil.CheckErr(o.Validate(cmd, args))
cmdutil.CheckErr(o.Run(args))
},
SuggestFor: []string{},
}
cmd.Flags().Int64VarP(&o.Offset, "offset", "o", o.Offset, "Specify the offset of the first row to be returned.")
cmd.Flags().Int64VarP(&o.Limit, "limit", "l", o.Limit, "Specify the amount records to be returned.")
return cmd
}
// Complete completes all the required options.
func (o *ListOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
var err error
o.iamclient, err = f.IAMClient()
if err != nil {
return err
}
return nil
}
// Validate makes sure there is no discrepency in command options.
func (o *ListOptions) Validate(cmd *cobra.Command, args []string) error {
return nil
}
// Run executes a list subcommand using the specified options.
func (o *ListOptions) Run(args []string) error {
policies, err := o.iamclient.APIV1().Policies().List(context.TODO(), metav1.ListOptions{
Offset: &o.Offset,
Limit: &o.Limit,
})
if err != nil {
return err
}
for _, pol := range policies.Items {
bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
if err := jsonEncoder.Encode(pol.Policy); err != nil {
return err
}
fmt.Fprintf(o.Out, "%12s %s\n", color.RedString(pol.Name+":"), bf.String())
}
return nil
}