github.com/cilium/cilium@v1.16.2/clustermesh-apiserver/version/version.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package version
     5  
     6  import (
     7  	"fmt"
     8  	"os/exec"
     9  	"regexp"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/cilium/cilium/clustermesh-apiserver/etcdinit"
    14  	"github.com/cilium/cilium/pkg/version"
    15  )
    16  
    17  func NewCmd() *cobra.Command {
    18  	return &cobra.Command{
    19  		Use:   "version",
    20  		Short: "Print version information",
    21  		Run: func(_ *cobra.Command, _ []string) {
    22  			fmt.Printf("Cilium ClusterMesh: %s\n", version.Version)
    23  			fmt.Printf("etcd: %s\n", getEtcdVersion())
    24  		},
    25  	}
    26  }
    27  
    28  func getEtcdVersion() string {
    29  	out, err := exec.Command(etcdinit.EtcdBinaryLocation, "--version").Output()
    30  	if err != nil {
    31  		return fmt.Sprintf("unable to retrieve version: %s", err)
    32  	}
    33  
    34  	const pattern = `etcd Version: (?P<version>.*?)\nGit SHA: (?P<sha>.*?)\nGo Version: (?P<go>.*?)\nGo OS\/Arch: (?P<arch>.*?)\n`
    35  	re := regexp.MustCompile(pattern)
    36  	matches := re.FindAllStringSubmatch(string(out), 1)
    37  	if len(matches) != 1 {
    38  		return "unable to parse version"
    39  	}
    40  
    41  	return fmt.Sprintf("%s %s go version %s %s",
    42  		matches[0][re.SubexpIndex("version")],
    43  		matches[0][re.SubexpIndex("sha")],
    44  		matches[0][re.SubexpIndex("go")],
    45  		matches[0][re.SubexpIndex("arch")],
    46  	)
    47  }