github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/version/static-data/make_static_data.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  )
     8  
     9  const bytesPerLine = 16
    10  
    11  func usage() {
    12  	fmt.Println("go run make_static_data.go -?")
    13  	fmt.Println("   Prints this help text")
    14  	fmt.Println("go run make_static_data.go <packageName> <dstFile.go>")
    15  	fmt.Println("   <packageName>   is the name of the ultimate package for <dstFile.go>")
    16  	fmt.Println("   <dstFile.go>    is the name of the generated .go source file")
    17  }
    18  
    19  var bs = []byte{}
    20  
    21  func main() {
    22  	var (
    23  		gitDescribeCmd       *exec.Cmd
    24  		gitDescribeOutput    []byte
    25  		dstFile              *os.File
    26  		dstFileName          string
    27  		err                  error
    28  		packageName          string
    29  		proxyfsVersionString string
    30  	)
    31  
    32  	if (2 == len(os.Args)) && ("-?" == os.Args[1]) {
    33  		usage()
    34  		os.Exit(0)
    35  	}
    36  
    37  	if 3 != len(os.Args) {
    38  		usage()
    39  		os.Exit(1)
    40  	}
    41  
    42  	packageName = os.Args[1]
    43  	dstFileName = os.Args[2]
    44  
    45  	dstFile, err = os.Create(dstFileName)
    46  	if nil != err {
    47  		panic(err.Error())
    48  	}
    49  
    50  	_, err = dstFile.Write([]byte(fmt.Sprintf("// Code generated by \"go run make_static_data.go %v %v\" - DO NOT EDIT\n\n", packageName, dstFileName)))
    51  	if nil != err {
    52  		panic(err.Error())
    53  	}
    54  	_, err = dstFile.Write([]byte(fmt.Sprintf("package %v\n\n", packageName)))
    55  	if nil != err {
    56  		panic(err.Error())
    57  	}
    58  
    59  	proxyfsVersionString = os.Getenv("PROXYFS_VERSION")
    60  
    61  	if "" == proxyfsVersionString {
    62  		gitDescribeCmd = exec.Command("git", "describe", "--tags")
    63  
    64  		gitDescribeOutput, err = gitDescribeCmd.Output()
    65  		if nil != err {
    66  			panic(err.Error())
    67  		}
    68  
    69  		proxyfsVersionString = string(gitDescribeOutput[:len(gitDescribeOutput)-1])
    70  	}
    71  
    72  	_, err = dstFile.Write([]byte(fmt.Sprintf("const ProxyFSVersion = `%v`\n", proxyfsVersionString)))
    73  	if nil != err {
    74  		panic(err.Error())
    75  	}
    76  
    77  	err = dstFile.Close()
    78  	if nil != err {
    79  		panic(err.Error())
    80  	}
    81  
    82  	os.Exit(0)
    83  }