github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/cmd/bacalhau/version.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package bacalhau
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/filecoin-project/bacalhau/pkg/model"
    26  	"github.com/filecoin-project/bacalhau/pkg/version"
    27  	"github.com/rs/zerolog/log"
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  // Versions is a struct for version information
    32  type Versions struct {
    33  	ClientVersion *model.BuildVersionInfo `json:"clientVersion,omitempty"`
    34  	ServerVersion *model.BuildVersionInfo `json:"serverVersion,omitempty"`
    35  }
    36  
    37  // VersionOptions is a struct to support version command
    38  type VersionOptions struct {
    39  	ClientOnly bool
    40  	Output     string
    41  
    42  	args []string
    43  }
    44  
    45  // NewVersionOptions returns initialized Options
    46  func NewVersionOptions() *VersionOptions {
    47  	return &VersionOptions{}
    48  }
    49  
    50  func newVersionCmd() *cobra.Command {
    51  	oV := NewVersionOptions()
    52  
    53  	versionCmd := &cobra.Command{
    54  		Use:   "version",
    55  		Short: "Get the client and server version.",
    56  		RunE: func(cmd *cobra.Command, _ []string) error {
    57  			return runVersion(cmd, oV)
    58  		},
    59  	}
    60  	versionCmd.Flags().BoolVar(&oV.ClientOnly, "client", oV.ClientOnly, "If true, shows client version only (no server required).")
    61  	versionCmd.Flags().StringVarP(&oV.Output, "output", "o", oV.Output, "One of 'yaml' or 'json'.")
    62  
    63  	return versionCmd
    64  }
    65  
    66  func runVersion(cmd *cobra.Command, oV *VersionOptions) error {
    67  	ctx := cmd.Context()
    68  
    69  	oV.Output = strings.TrimSpace(strings.ToLower(oV.Output))
    70  
    71  	err := oV.Validate(cmd)
    72  	if err != nil {
    73  		Fatal(cmd, fmt.Sprintf("Error validating version: %s\n", err), 1)
    74  	}
    75  
    76  	err = oV.Run(ctx, cmd)
    77  	if err != nil {
    78  		Fatal(cmd, fmt.Sprintf("Error running version: %s\n", err), 1)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // Validate validates the provided options
    85  func (oV *VersionOptions) Validate(*cobra.Command) error {
    86  	if len(oV.args) != 0 {
    87  		return fmt.Errorf("extra arguments: %v", oV.args)
    88  	}
    89  
    90  	if oV.Output != "" && oV.Output != YAMLFormat && oV.Output != JSONFormat {
    91  		return errors.New(`--output must be 'yaml' or 'json'`)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  // Run executes version command
    98  func (oV *VersionOptions) Run(ctx context.Context, cmd *cobra.Command) error {
    99  	var (
   100  		versions Versions
   101  	)
   102  
   103  	versions.ClientVersion = version.Get()
   104  
   105  	if !oV.ClientOnly {
   106  		serverVersion, err := GetAPIClient().Version(ctx)
   107  		if err != nil {
   108  			log.Ctx(ctx).Error().Err(err).Msgf("could not get server version")
   109  			return err
   110  		}
   111  
   112  		versions.ServerVersion = serverVersion
   113  	}
   114  
   115  	switch oV.Output {
   116  	case "":
   117  		cmd.Printf("Client Version: %s\n", versions.ClientVersion.GitVersion)
   118  		if versions.ServerVersion != nil {
   119  			cmd.Printf("Server Version: %s\n", versions.ServerVersion.GitVersion)
   120  		}
   121  	case YAMLFormat:
   122  		marshaled, err := model.YAMLMarshalWithMax(versions)
   123  		if err != nil {
   124  			return err
   125  		}
   126  		cmd.Println(string(marshaled))
   127  	case JSONFormat:
   128  		marshaled, err := model.JSONMarshalWithMax(versions)
   129  		if err != nil {
   130  			return err
   131  		}
   132  		cmd.Println(string(marshaled))
   133  	default:
   134  		// There is a bug in the program if we hit this case.
   135  		// However, we follow a policy of never panicking.
   136  		return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", oV.Output)
   137  	}
   138  
   139  	return nil
   140  }