github.com/vmware/govmomi@v0.37.2/govc/version/command.go (about)

     1  /*
     2  Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
     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 version
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  )
    28  
    29  type version struct {
    30  	*flags.EmptyFlag
    31  
    32  	require string
    33  	long    bool // detailed govc version output
    34  }
    35  
    36  func init() {
    37  	cli.Register("version", &version{})
    38  }
    39  
    40  func (cmd *version) Register(ctx context.Context, f *flag.FlagSet) {
    41  	f.StringVar(&cmd.require, "require", "", "Require govc version >= this value")
    42  	f.BoolVar(&cmd.long, "l", false, "Print detailed govc version information")
    43  }
    44  
    45  func (cmd *version) Run(ctx context.Context, f *flag.FlagSet) error {
    46  	ver := strings.TrimPrefix(flags.BuildVersion, "v")
    47  	if cmd.require != "" {
    48  		v, err := flags.ParseVersion(ver)
    49  		if err != nil {
    50  			panic(err)
    51  		}
    52  
    53  		rv, err := flags.ParseVersion(cmd.require)
    54  		if err != nil {
    55  			return fmt.Errorf("failed to parse required version '%s': %s", cmd.require, err)
    56  		}
    57  
    58  		if !rv.Lte(v) {
    59  			return fmt.Errorf("version %s or higher is required, this is version %s", cmd.require, ver)
    60  		}
    61  	}
    62  
    63  	if cmd.long {
    64  		fmt.Printf("Build Version: %s\n", flags.BuildVersion)
    65  		fmt.Printf("Build Commit: %s\n", flags.BuildCommit)
    66  		fmt.Printf("Build Date: %s\n", flags.BuildDate)
    67  	} else {
    68  		fmt.Printf("govc %s\n", ver)
    69  	}
    70  
    71  	return nil
    72  }