github.com/bitrise-io/go-xamarin@v0.0.0-20211005113058-bf60a8bae851/cli/version.go (about)

     1  package cli
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	yaml "gopkg.in/yaml.v2"
     8  
     9  	"github.com/bitrise-io/go-utils/log"
    10  	"github.com/bitrise-io/go-xamarin/version"
    11  	"github.com/urfave/cli"
    12  )
    13  
    14  // VersionOutputModel ...
    15  type VersionOutputModel struct {
    16  	Version     string `json:"version"`
    17  	BuildNumber string `json:"build_number"`
    18  	Commit      string `json:"commit"`
    19  }
    20  
    21  const (
    22  	// FormatRaw ...
    23  	FormatRaw = "raw"
    24  	// FormatJSON ...
    25  	FormatJSON = "json"
    26  	// FormatYML ...
    27  	FormatYML = "yml"
    28  )
    29  
    30  // Print ...
    31  func print(versionOutput VersionOutputModel, format string) {
    32  	switch format {
    33  	case FormatJSON:
    34  		serBytes, err := json.Marshal(versionOutput)
    35  		if err != nil {
    36  			log.Errorf("failed to print output, error: %s", err)
    37  			return
    38  		}
    39  		fmt.Printf("%s\n", serBytes)
    40  	case FormatYML:
    41  		serBytes, err := yaml.Marshal(versionOutput)
    42  		if err != nil {
    43  			log.Errorf("failed to print output, error: %s", err)
    44  			return
    45  		}
    46  		fmt.Printf("%s\n", serBytes)
    47  	default:
    48  		fmt.Printf("version: %s\nbuild number: %s\ncommit: %s\n", versionOutput.Version, versionOutput.BuildNumber, versionOutput.Commit)
    49  	}
    50  }
    51  
    52  func versionCmd(c *cli.Context) error {
    53  	format := c.String("format")
    54  
    55  	versionOutput := VersionOutputModel{
    56  		Version:     version.VERSION,
    57  		BuildNumber: version.BuildNumber,
    58  		Commit:      version.Commit,
    59  	}
    60  
    61  	print(versionOutput, format)
    62  
    63  	return nil
    64  }