...

Source file src/golang.conradwood.net/go-easyops/appinfo/appinfo.go

Documentation: golang.conradwood.net/go-easyops/appinfo

     1  /*
     2  This package stores data about the build process.
     3  During build variables are set, such as file location, build number and time.
     4  These can be retrieved through this package.
     5  */
     6  package appinfo
     7  
     8  import (
     9  	"fmt"
    10  	"strconv"
    11  )
    12  
    13  type AppVersionInfo struct {
    14  	Number         uint64
    15  	Description    string
    16  	Timestamp      int64
    17  	RepositoryID   uint64
    18  	RepositoryName string
    19  	CommitID       string
    20  	GitURL         string
    21  	ArtefactID     uint64
    22  }
    23  
    24  var (
    25  	appInfo           *AppVersionInfo // set by init() of some modules (overrides)
    26  	OldAppInfo        *AppVersionInfo // set by cmdline
    27  	LD_Number         string          // set by linker flags
    28  	LD_Description    string
    29  	LD_Timestamp      string
    30  	LD_RepositoryID   string
    31  	LD_ArtefactID     string
    32  	LD_RepositoryName string
    33  	LD_CommitID       string
    34  	LD_GitURL         string
    35  )
    36  
    37  func RegisterAppInfo(avi *AppVersionInfo) {
    38  	appInfo = avi
    39  }
    40  func AppInfo() *AppVersionInfo {
    41  	if appInfo != nil {
    42  		return appInfo
    43  	}
    44  	if LD_Number != "" {
    45  		a := &AppVersionInfo{
    46  			Number:         required_number(LD_Number),
    47  			Description:    LD_Description,
    48  			Timestamp:      int64(required_number(LD_Timestamp)),
    49  			RepositoryID:   required_number(LD_RepositoryID),
    50  			ArtefactID:     required_number(LD_ArtefactID),
    51  			RepositoryName: LD_RepositoryName,
    52  			CommitID:       LD_CommitID,
    53  			GitURL:         LD_GitURL,
    54  		}
    55  		appInfo = a
    56  		return a
    57  	}
    58  	if OldAppInfo != nil {
    59  		return OldAppInfo
    60  	}
    61  	a := &AppVersionInfo{RepositoryName: "notset"}
    62  	return a
    63  }
    64  func required_number(num string) uint64 {
    65  	if num == "" {
    66  		return 0
    67  	}
    68  
    69  	n, err := strconv.ParseInt(num, 10, 64)
    70  	if err != nil {
    71  		fmt.Printf("Not a number: \"%s\" (%s)\n", num, err)
    72  		panic("invalid linker flags")
    73  	}
    74  	return uint64(n)
    75  }
    76  

View as plain text