github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_unsafe_show_metadata.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package cli 15 16 import ( 17 "github.com/pingcap/errors" 18 apiv2client "github.com/pingcap/tiflow/pkg/api/v2" 19 "github.com/pingcap/tiflow/pkg/cmd/context" 20 "github.com/pingcap/tiflow/pkg/cmd/factory" 21 "github.com/pingcap/tiflow/pkg/cmd/util" 22 "github.com/spf13/cobra" 23 ) 24 25 // unsafeShowMetadataOptions defines flags for the `cli unsafe show-metadata` command. 26 type unsafeShowMetadataOptions struct { 27 apiClient apiv2client.APIV2Interface 28 } 29 30 // newUnsafeShowMetadataOptions creates new unsafeShowMetadataOptions 31 // for the `cli unsafe show-metadata` command. 32 func newUnsafeShowMetadataOptions() *unsafeShowMetadataOptions { 33 return &unsafeShowMetadataOptions{} 34 } 35 36 // complete adapts from the command line args to the data and client required. 37 func (o *unsafeShowMetadataOptions) complete(f factory.Factory) error { 38 apiClient, err := f.APIV2Client() 39 if err != nil { 40 return err 41 } 42 o.apiClient = apiClient 43 return nil 44 } 45 46 // run runs the `cli unsafe show-metadata` command. 47 func (o *unsafeShowMetadataOptions) run(cmd *cobra.Command) error { 48 ctx := context.GetDefaultContext() 49 50 kvs, err := o.apiClient.Unsafe().Metadata(ctx) 51 if err != nil { 52 return errors.Trace(err) 53 } 54 55 for _, kv := range *kvs { 56 cmd.Printf("Key: %s, Value: %s\n", kv.Key, kv.Value) 57 } 58 cmd.Printf("Show %d KVs\n", len(*kvs)) 59 60 return nil 61 } 62 63 // newCmdShowMetadata creates the `cli unsafe show-metadata` command. 64 func newCmdShowMetadata(f factory.Factory) *cobra.Command { 65 o := newUnsafeShowMetadataOptions() 66 67 command := &cobra.Command{ 68 Use: "show-metadata", 69 Short: "Show metadata stored in PD", 70 Args: cobra.NoArgs, 71 Run: func(cmd *cobra.Command, args []string) { 72 util.CheckErr(o.complete(f)) 73 util.CheckErr(o.run(cmd)) 74 }, 75 } 76 77 return command 78 }