mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-13 16:47:36 +08:00
131 lines
3.4 KiB
Go
131 lines
3.4 KiB
Go
// Copyright © 2023 OpenIM. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package secret
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/marmotedu/marmotedu-sdk-go/marmotedu/service/iam"
|
|
"github.com/olekukonko/tablewriter"
|
|
metav1 "github.com/openim-sigs/component-base/pkg/meta/v1"
|
|
"github.com/spf13/cobra"
|
|
|
|
cmdutil "github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/cmd/util"
|
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/internal/imctl/util/templates"
|
|
"github.com/OpenIMSDK/Open-IM-Server/tools/imctl/pkg/cli/genericclioptions"
|
|
)
|
|
|
|
const (
|
|
getUsageStr = "get SECRET_NAME"
|
|
)
|
|
|
|
// GetOptions is an options struct to support get subcommands.
|
|
type GetOptions struct {
|
|
Name string
|
|
|
|
iamclient iam.IamInterface
|
|
|
|
genericclioptions.IOStreams
|
|
}
|
|
|
|
var (
|
|
getExample = templates.Examples(`
|
|
# Get a specified secret information
|
|
iamctl secret get foo`)
|
|
|
|
getUsageErrStr = fmt.Sprintf("expected '%s'.\nSECRET_NAME is required arguments for the get command", getUsageStr)
|
|
)
|
|
|
|
// NewGetOptions returns an initialized GetOptions instance.
|
|
func NewGetOptions(ioStreams genericclioptions.IOStreams) *GetOptions {
|
|
return &GetOptions{
|
|
IOStreams: ioStreams,
|
|
}
|
|
}
|
|
|
|
// NewCmdGet returns new initialized instance of get sub command.
|
|
func NewCmdGet(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
|
o := NewGetOptions(ioStreams)
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "get SECRET_NAME",
|
|
DisableFlagsInUseLine: true,
|
|
Aliases: []string{},
|
|
Short: "Display a secret resource",
|
|
TraverseChildren: true,
|
|
Long: "Display a secret resource.",
|
|
Example: getExample,
|
|
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{},
|
|
}
|
|
|
|
return cmd
|
|
}
|
|
|
|
// Complete completes all the required options.
|
|
func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
|
var err error
|
|
if len(args) == 0 {
|
|
return cmdutil.UsageErrorf(cmd, getUsageErrStr)
|
|
}
|
|
|
|
o.Name = args[0]
|
|
|
|
o.iamclient, err = f.IAMClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Validate makes sure there is no discrepency in command options.
|
|
func (o *GetOptions) Validate(cmd *cobra.Command, args []string) error {
|
|
return nil
|
|
}
|
|
|
|
// Run executes a get subcommand using the specified options.
|
|
func (o *GetOptions) Run(args []string) error {
|
|
secret, err := o.iamclient.APIV1().Secrets().Get(context.TODO(), o.Name, metav1.GetOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
table := tablewriter.NewWriter(o.Out)
|
|
|
|
data := [][]string{
|
|
{
|
|
secret.Name,
|
|
secret.SecretID,
|
|
secret.SecretKey,
|
|
time.Unix(secret.Expires, 0).Format("2006-01-02 15:04:05"),
|
|
secret.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
},
|
|
}
|
|
|
|
table = setHeader(table)
|
|
table = cmdutil.TableWriterDefaultConfig(table)
|
|
table.AppendBulk(data)
|
|
table.Render()
|
|
|
|
return nil
|
|
}
|