sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/version/doc.go (about)

     1  /*
     2  Copyright 2020 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  
    17  // version holds variables that identify a Prow binary's name and version
    18  package version
    19  
    20  import (
    21  	"fmt"
    22  	"regexp"
    23  	"time"
    24  )
    25  
    26  var (
    27  	// Name is the colloquial identifier for the compiled component
    28  	Name = "unset"
    29  	// Version is a concatenation of the commit SHA and date for the build
    30  	Version = "0"
    31  	// reVersion is a regex expression for extracting build time.
    32  	// Version derived from "v${build_date}-${git_commit}" as in /hack/print-workspace-status.sh
    33  	reVersion = regexp.MustCompile(`v(\d+)-.*`)
    34  )
    35  
    36  // UserAgent exposes the component's name and version for user-agent header
    37  func UserAgent() string {
    38  	return Name + "/" + Version
    39  }
    40  
    41  // UserAgentFor exposes the component's name and version for user-agent header
    42  // while embedding the additional identifier
    43  func UserAgentWithIdentifier(identifier string) string {
    44  	return Name + "." + identifier + "/" + Version
    45  }
    46  
    47  // VersionTimestamp returns the timestamp of date derived from version
    48  func VersionTimestamp() (int64, error) {
    49  	var ver int64
    50  	m := reVersion.FindStringSubmatch(Version)
    51  	if len(m) < 2 {
    52  		return ver, fmt.Errorf("version expected to be in form 'v${build_date}-${git_commit}': %q", Version)
    53  	}
    54  	t, err := time.Parse("20060102", m[1])
    55  	if err != nil {
    56  		return ver, err
    57  	}
    58  	return t.Unix(), nil
    59  }