...

Source file src/sigs.k8s.io/release-utils/mage/version.go

Documentation: sigs.k8s.io/release-utils/mage

     1  /*
     2  Copyright 2022 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  package mage
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"time"
    23  
    24  	"github.com/uwu-tools/magex/shx"
    25  )
    26  
    27  // getVersion gets a description of the commit, e.g. v0.30.1 (latest) or v0.30.1-32-gfe72ff73 (canary)
    28  func getVersion() (string, error) {
    29  	version, err := shx.Output("git", "describe", "--tags", "--always")
    30  	if err != nil {
    31  		return "", err
    32  	}
    33  	if version != "" {
    34  		return version, nil
    35  	}
    36  
    37  	// repo without any tags in it
    38  	return "v0.0.0", nil
    39  }
    40  
    41  // getCommit gets the hash of the current commit
    42  func getCommit() (string, error) {
    43  	return shx.Output("git", "rev-parse", "--short", "HEAD")
    44  }
    45  
    46  // getGitState gets the state of the git repository
    47  func getGitState() string {
    48  	_, err := shx.Output("git", "diff", "--quiet")
    49  	if err != nil {
    50  		return "dirty"
    51  	}
    52  
    53  	return "clean"
    54  }
    55  
    56  // getBuildDateTime gets the build date and time
    57  func getBuildDateTime() (string, error) {
    58  	result, err := shx.Output("git", "log", "-1", "--pretty=%ct")
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  	if result != "" {
    63  		parsedInt, err := strconv.ParseInt(result, 10, 64)
    64  		if err != nil {
    65  			return "", fmt.Errorf("parse source date epoch to int: %w", err)
    66  		}
    67  		return time.Unix(parsedInt, 0).UTC().Format(time.RFC3339), nil
    68  	}
    69  
    70  	return shx.Output("date", "+%Y-%m-%dT%H:%M:%SZ")
    71  }
    72  
    73  // GenerateLDFlags returns the string to use in the `-ldflags` flag.
    74  func GenerateLDFlags() (string, error) {
    75  	pkg := "sigs.k8s.io/release-utils/version"
    76  	version, err := getVersion()
    77  	if err != nil {
    78  		return "", err
    79  	}
    80  	commit, err := getCommit()
    81  	if err != nil {
    82  		return "", err
    83  	}
    84  	buildTime, err := getBuildDateTime()
    85  	if err != nil {
    86  		return "", err
    87  	}
    88  
    89  	return fmt.Sprintf("-X %[1]s.gitVersion=%[2]s -X %[1]s.gitCommit=%[3]s -X %[1]s.gitTreeState=%[4]s -X %[1]s.buildDate=%[5]s",
    90  		pkg, version, commit, getGitState(), buildTime), nil
    91  }
    92  

View as plain text