github.com/landoop/schema-registry@v0.0.0-20190327143759-50a5701c1891/schema-registry-cli/cmd/get.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  // get can handle three argument styles: <id>, <subj ver> or <subj>
    11  var getCmd = &cobra.Command{
    12  	Use:   "get <id> | (<subject> [<version>])",
    13  	Short: "retrieves a schema specified by id or subject",
    14  	Long: `The schema can be requested by id or subject.
    15  When a subject is given, optionally one can provide a specific version. If no
    16  version is specified, the latest version is returned.
    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  		id, idParseErr := strconv.Atoi(args[0])
    23  		var err error
    24  		switch {
    25  		case len(args) == 1 && idParseErr == nil:
    26  			err = getByID(id)
    27  		case len(args) == 1 && idParseErr != nil:
    28  			err = getLatestBySubject(args[0])
    29  		case len(args) == 2:
    30  			ver, err := strconv.Atoi(args[1])
    31  			if err != nil {
    32  				return fmt.Errorf("2nd argument must be a version number")
    33  			}
    34  			err = getBySubjectVersion(args[0], ver)
    35  		default:
    36  			return fmt.Errorf("?")
    37  		}
    38  		return err
    39  	},
    40  }
    41  
    42  func init() {
    43  	RootCmd.AddCommand(getCmd)
    44  }