github.com/landoop/schema-registry@v0.0.0-20190327143759-50a5701c1891/schema-registry-cli/cmd/compatible.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/spf13/cobra" 8 ) 9 10 // compatible can handle two argument styles: <subj ver> or <subj> 11 var compatibleCmd = &cobra.Command{ 12 Use: "compatible <subject> [version]", 13 Short: "tests compatibility between a schema from stdin and a given subject", 14 Long: `The compatibility level of the subject is used for this check. 15 If it has never been changed, the global compatibility level applies. 16 If no schema version is specified, the latest version is tested. 17 `, 18 RunE: func(cmd *cobra.Command, args []string) error { 19 if len(args) < 1 || len(args) > 2 { 20 return fmt.Errorf("expected 1 to 2 arguments") 21 } 22 var iscompat bool 23 var err error 24 switch len(args) { 25 case 1: 26 iscompat, err = assertClient().IsLatestSchemaCompatible(args[0], stdinToString()) 27 case 2: 28 ver, err := strconv.Atoi(args[1]) 29 if err != nil { 30 return fmt.Errorf("2nd argument must be a version number") 31 } 32 iscompat, err = assertClient().IsSchemaCompatible(args[0], stdinToString(), ver) 33 } 34 if err != nil { 35 return err 36 } 37 if iscompat { 38 fmt.Println("the provided schema is compatible") 39 } else { 40 err = fmt.Errorf("the provided schema is not compatible") 41 } 42 return err 43 }, 44 } 45 46 func init() { 47 RootCmd.AddCommand(compatibleCmd) 48 }