github.com/crossplane-contrib/function-cue@v0.2.2-0.20240508161918-5100fcb5a058/cmd/fn-cue-tools/version.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package main
    19  
    20  import (
    21  	"bytes"
    22  	"fmt"
    23  	"runtime"
    24  	"strings"
    25  	"text/tabwriter"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  // Build information. Populated at build-time.
    31  var (
    32  	Version   = "dev"
    33  	Commit    = "unknown"
    34  	BuildDate = "unknown"
    35  )
    36  
    37  // info contains detailed information about the binary.
    38  type buildInfo struct {
    39  	version, commit, buildDate string
    40  }
    41  
    42  func (v buildInfo) String() string {
    43  	var buf bytes.Buffer
    44  	w := tabwriter.NewWriter(&buf, 2, 2, 3, ' ', 0)
    45  	write := func(prompt, value string) {
    46  		_, _ = fmt.Fprintln(w, prompt+"\t", value)
    47  	}
    48  	write("Version", v.version)
    49  	write("Go Version", runtime.Version())
    50  	write("Commit", strings.ReplaceAll(v.commit, "_", " "))
    51  	write("Build Date", v.buildDate)
    52  	write("OS/Arch", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
    53  	_ = w.Flush()
    54  	return buf.String()
    55  }
    56  
    57  func versionCommand() *cobra.Command {
    58  	return &cobra.Command{
    59  		Use:   "version",
    60  		Short: "print program version",
    61  		Run: func(cmd *cobra.Command, args []string) {
    62  			info := buildInfo{Version, Commit, BuildDate}
    63  			fmt.Println(info)
    64  		},
    65  	}
    66  }