github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/version/version.go (about)

     1  /*
     2  Copyright 2019 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  package version
    17  
    18  import (
    19  	"fmt"
    20  	"runtime/debug"
    21  )
    22  
    23  // Version returns the version of the main module
    24  func Version() string {
    25  	info, ok := debug.ReadBuildInfo()
    26  	if !ok || info == nil || info.Main.Version == "" {
    27  		// binary has not been built with module support or doesn't contain a version.
    28  		return "(unknown)"
    29  	}
    30  	return info.Main.Version
    31  }
    32  
    33  // Print prints the main module version on stdout.
    34  //
    35  // Print will display either:
    36  //
    37  // - "Version: v0.2.1" when the program has been compiled with:
    38  //
    39  //	$ go get github.com/controller-tools/cmd/controller-gen@v0.2.1
    40  //
    41  //	Note: go modules requires the usage of semver compatible tags starting with
    42  //	     'v' to have nice human-readable versions.
    43  //
    44  // - "Version: (devel)" when the program is compiled from a local git checkout.
    45  //
    46  // - "Version: (unknown)" when not using go modules.
    47  func Print() {
    48  	fmt.Printf("Version: %s\n", Version())
    49  }