go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/versioncli/versioncli.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package versioncli implements a subcommand for obtaining version with the CLI.
    16  package versioncli
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/maruel/subcommands"
    23  
    24  	"go.chromium.org/luci/cipd/version"
    25  	"go.chromium.org/luci/hardcoded/chromeinfra"
    26  )
    27  
    28  // CmdVersion returns a "version" subcommand that prints to stdout the given
    29  // version as well as CIPD package name and the package instance ID if the
    30  // executable was installed via CIPD.
    31  //
    32  // If the executable didn't come from CIPD, the package information is silently
    33  // omitted.
    34  //
    35  // 'version' will be printed exact as given. The recommended format is
    36  // "<appname> vMAJOR.MINOR.PATCH".
    37  func CmdVersion(version string) *subcommands.Command {
    38  	return &subcommands.Command{
    39  		UsageLine: "version",
    40  		ShortDesc: "prints the executable version",
    41  		LongDesc:  "Prints the executable version and the CIPD package the executable was installed from (if it was installed via CIPD).",
    42  		CommandRun: func() subcommands.CommandRun {
    43  			return &versionRun{version: version}
    44  		},
    45  	}
    46  }
    47  
    48  type versionRun struct {
    49  	subcommands.CommandRunBase
    50  	version string
    51  }
    52  
    53  func (c *versionRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
    54  	if len(args) != 0 {
    55  		fmt.Fprintf(a.GetErr(), "%s: position arguments not expected\n", a.GetName())
    56  		return 1
    57  	}
    58  	fmt.Println(c.version)
    59  
    60  	switch ver, err := version.GetStartupVersion(); {
    61  	case err != nil:
    62  		// Note: this is some sort of catastrophic error. If the binary is not
    63  		// installed via CIPD, err == nil && ver.InstanceID == "".
    64  		fmt.Fprintf(os.Stderr, "cannot determine CIPD package version: %s\n", err)
    65  		return 1
    66  	case ver.InstanceID != "":
    67  		fmt.Println()
    68  		fmt.Printf("CIPD package name: %s\n", ver.PackageName)
    69  		fmt.Printf("CIPD instance ID:  %s\n", ver.InstanceID)
    70  		fmt.Printf("CIPD URL: %s/p/%s/+/%s\n", chromeinfra.CIPDServiceURL, ver.PackageName, ver.InstanceID)
    71  	}
    72  
    73  	return 0
    74  }