github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/buildscripts/gen-ldflags.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Copyright (c) 2015-2021 MinIO, Inc.
     5  //
     6  // This file is part of MinIO Object Storage stack
     7  //
     8  // This program is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU Affero General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // This program is distributed in the hope that it will be useful
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU Affero General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU Affero General Public License
    19  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  
    21  package main
    22  
    23  import (
    24  	"fmt"
    25  	"os"
    26  	"os/exec"
    27  	"strings"
    28  	"time"
    29  )
    30  
    31  func genLDFlags(version string) string {
    32  	releaseTag, date := releaseTag(version)
    33  	copyrightYear := fmt.Sprintf("%d", date.Year())
    34  
    35  	var ldflagsStr string
    36  	ldflagsStr = "-s -w -X github.com/minio/mc/cmd.Version=" + version + " "
    37  	ldflagsStr = ldflagsStr + "-X github.com/minio/mc/cmd.CopyrightYear=" + copyrightYear + " "
    38  	ldflagsStr = ldflagsStr + "-X github.com/minio/mc/cmd.ReleaseTag=" + releaseTag + " "
    39  	ldflagsStr = ldflagsStr + "-X github.com/minio/mc/cmd.CommitID=" + commitID() + " "
    40  	ldflagsStr = ldflagsStr + "-X github.com/minio/mc/cmd.ShortCommitID=" + commitID()[:12]
    41  	return ldflagsStr
    42  }
    43  
    44  // releaseTag prints release tag to the console for easy git tagging.
    45  func releaseTag(version string) (string, time.Time) {
    46  	relPrefix := "DEVELOPMENT"
    47  	if prefix := os.Getenv("MC_RELEASE"); prefix != "" {
    48  		relPrefix = prefix
    49  	}
    50  
    51  	relSuffix := ""
    52  	if hotfix := os.Getenv("MC_HOTFIX"); hotfix != "" {
    53  		relSuffix = hotfix
    54  	}
    55  
    56  	relTag := strings.ReplaceAll(version, " ", "-")
    57  	relTag = strings.ReplaceAll(relTag, ":", "-")
    58  	t, err := time.Parse("2006-01-02T15-04-05Z", relTag)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  
    63  	relTag = strings.ReplaceAll(relTag, ",", "")
    64  	relTag = relPrefix + "." + relTag
    65  	if relSuffix != "" {
    66  		relTag += "." + relSuffix
    67  	}
    68  
    69  	return relTag, t
    70  }
    71  
    72  // commitID returns the abbreviated commit-id hash of the last commit.
    73  func commitID() string {
    74  	// git log --format="%h" -n1
    75  	var (
    76  		commit []byte
    77  		e      error
    78  	)
    79  	cmdName := "git"
    80  	cmdArgs := []string{"log", "--format=%H", "-n1"}
    81  	if commit, e = exec.Command(cmdName, cmdArgs...).Output(); e != nil {
    82  		fmt.Fprintln(os.Stderr, "Error generating git commit-id: ", e)
    83  		os.Exit(1)
    84  	}
    85  
    86  	return strings.TrimSpace(string(commit))
    87  }
    88  
    89  func commitTime() time.Time {
    90  	// git log --format=%cD -n1
    91  	var (
    92  		commitUnix []byte
    93  		err        error
    94  	)
    95  	cmdName := "git"
    96  	cmdArgs := []string{"log", "--format=%cI", "-n1"}
    97  	if commitUnix, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
    98  		fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err)
    99  		os.Exit(1)
   100  	}
   101  
   102  	t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(commitUnix)))
   103  	if err != nil {
   104  		fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err)
   105  		os.Exit(1)
   106  	}
   107  
   108  	return t.UTC()
   109  }
   110  
   111  func main() {
   112  	var version string
   113  	if len(os.Args) > 1 {
   114  		version = os.Args[1]
   115  	} else {
   116  		version = commitTime().Format(time.RFC3339)
   117  	}
   118  
   119  	fmt.Println(genLDFlags(version))
   120  }