Skip to content

Commit

Permalink
feat: get & list chaincode(#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayuy committed Jun 5, 2023
1 parent a2fc395 commit 4562da0
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/bc-cli/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/bestchains/bc-cli/pkg/account"
"github.com/bestchains/bc-cli/pkg/chaincode"
"github.com/bestchains/bc-cli/pkg/chaincodebuild"
"github.com/bestchains/bc-cli/pkg/common"
"github.com/bestchains/bc-cli/pkg/connProfile"
Expand All @@ -43,7 +44,7 @@ func NewGetCmd() *cobra.Command {
cmd.AddCommand(org.NewOrgGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
cmd.AddCommand(connProfile.NewGetConnProfileCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
cmd.AddCommand(federation.NewFedGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
//cmd.AddCommand(chaincode.NewCCGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
cmd.AddCommand(chaincode.NewCCGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
cmd.AddCommand(chaincodebuild.NewCCBGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
//cmd.AddCommand(channel.NewChanGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
//cmd.AddCommand(vote.NewVoteGetCmd(common.Options{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}}))
Expand Down
105 changes: 101 additions & 4 deletions pkg/chaincode/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,114 @@ limitations under the License.
package chaincode

import (
"github.com/bestchains/bc-cli/pkg/common"
"context"
"encoding/json"
"fmt"
"strings"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubectl/pkg/cmd/get"

"github.com/bestchains/bc-cli/pkg/common"
)

func NewCCGetCmd(option common.Options) *cobra.Command {
var (
channel string
id string
version string
)
defaultPrintFlag := get.NewGetPrintFlags()
cmd := &cobra.Command{
Use: "",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
Use: "chaincode [NAME]",
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
err := cmd.MarkFlagRequired("channel")
if err != nil {
fmt.Fprintln(option.ErrOut, err)
return
}
}
},
Run: func(cmd *cobra.Command, args []string) {
cli, err := common.GetDynamicClient()
if err != nil {
fmt.Fprintln(option.ErrOut, err)
return
}

list := corev1.List{
TypeMeta: v1.TypeMeta{
Kind: "List",
APIVersion: "v1",
},
ListMeta: v1.ListMeta{},
}
if len(args) == 0 {
var labels []string
for k, v := range map[string]string{
"channel": channel,
"id": id,
"version": version,
} {
if v != "" {
labels = append(labels, fmt.Sprintf("bestchains.chaincode.%s=%s", k, v))
}
}
chaincodes, err := cli.Resource(schema.GroupVersionResource{Group: common.IBPGroup, Version: common.IBPVersion, Resource: common.Chaincode}).List(context.TODO(), v1.ListOptions{
LabelSelector: strings.Join(labels, ","),
})
if err != nil {
fmt.Fprintln(option.ErrOut, err)
return
}
for i := 0; i < len(chaincodes.Items); i++ {
list.Items = append(list.Items, runtime.RawExtension{Object: &chaincodes.Items[i]})
}
} else {
for _, arg := range args {
chaincode, err := cli.Resource(schema.GroupVersionResource{Group: common.IBPGroup, Version: common.IBPVersion, Resource: common.Chaincode}).Get(context.TODO(), arg, v1.GetOptions{})
if err != nil {
fmt.Fprintln(option.ErrOut, err)
continue
}
list.Items = append(list.Items, runtime.RawExtension{Object: chaincode})
}
}
var obj runtime.Object
if len(list.Items) != 1 {
listData, err := json.Marshal(list)
if err != nil {
fmt.Fprintln(option.ErrOut, err)
return
}
converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, listData)
if err != nil {
fmt.Fprintln(option.ErrOut, err)
return
}
obj = converted
} else {
obj = list.Items[0].Object
}

p, err := defaultPrintFlag.ToPrinter()
if err != nil {
fmt.Fprintln(option.ErrOut, err)
return
}
_ = p.PrintObj(obj, option.Out)
},
}

defaultPrintFlag.AddFlags(cmd)
cmd.Flags().StringVar(&channel, "channel", "", "channel name")
cmd.Flags().StringVar(&id, "id", "", "chaincode id")
cmd.Flags().StringVar(&version, "version", "", "chaincode version")
return cmd
}
1 change: 1 addition & 0 deletions pkg/common/ibp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
Network = "networks"
EndorsePolicy = "endorsepolicies"
ChaincodeBuild = "chaincodebuilds"
Chaincode = "chaincodes"
)

func InKubeGetter() (*clientcmdapi.Config, error) {
Expand Down

0 comments on commit 4562da0

Please sign in to comment.