github.com/comcast/canticle@v0.0.0-20161108184242-c53cface56e8/canticles/buildinfo.go (about)

     1  package canticles
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/user"
     8  	"path"
     9  	"text/template"
    10  	"time"
    11  )
    12  
    13  type BuildInfo struct {
    14  	BuildTime    string
    15  	BuildUser    string
    16  	BuildHost    string
    17  	Revision     string
    18  	CanticleDeps *json.RawMessage
    19  }
    20  
    21  func (b *BuildInfo) DepString() string {
    22  	s, _ := json.Marshal(b.CanticleDeps)
    23  	return string(s)
    24  }
    25  
    26  func (b *BuildInfo) WriteFiles(dir string) error {
    27  	pkgdir := path.Join(dir, "buildinfo")
    28  	if err := os.MkdirAll(pkgdir, 0755); err != nil {
    29  		return err
    30  	}
    31  	if err := ioutil.WriteFile(path.Join(pkgdir, "buildinfo.go"), []byte(BuildInfoGoFile), 0644); err != nil {
    32  		return err
    33  	}
    34  	f, err := os.Create(path.Join(pkgdir, "info.go"))
    35  	if err != nil {
    36  		return err
    37  	}
    38  	defer f.Close()
    39  	return BuildInfoTemplate.Execute(f, b)
    40  }
    41  
    42  func NewBuildInfo(rev string, stable bool, deps []*CanticleDependency) (*BuildInfo, error) {
    43  	var bi BuildInfo
    44  	bi.Revision = rev
    45  
    46  	// Encode our deps to place be placed
    47  	msg, err := json.MarshalIndent(deps, "", "    ")
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// Decode our canticle file as a raw json message so we can embed it
    53  	var j json.RawMessage
    54  	if err := json.Unmarshal(msg, &j); err != nil {
    55  		return nil, err
    56  	}
    57  	bi.CanticleDeps = &j
    58  
    59  	// Add the other fields if a stable build info set is requiered
    60  	if stable {
    61  		return &bi, nil
    62  	}
    63  
    64  	u, err := user.Current()
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	bi.BuildUser = u.Username
    69  
    70  	if bi.BuildHost, err = os.Hostname(); err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	bi.BuildTime = time.Now().Format(time.RFC3339)
    75  
    76  	return &bi, nil
    77  }
    78  
    79  var BuildInfoGoFile = `
    80  // Package buildinfo is GENERATED CODE from the Canticle build tool,
    81  // you may check this file in so build can happen without
    82  // genversion. DO NOT CHECK IN info.go in this package.
    83  package buildinfo
    84  
    85  import "encoding/json"
    86  
    87  // BuildInfo contains the deps of this as well as information about
    88  // when genversion was called.
    89  type BuildInfo struct {
    90  	BuildTime    string
    91  	BuildUser    string
    92  	BuildHost    string
    93  	Revision     string
    94  	CanticleDeps *json.RawMessage
    95  }
    96  
    97  var buildInfo = &BuildInfo{}
    98  
    99  // GetBuildInfo returns the information saved by cant genversion.
   100  func GetBuildInfo() *BuildInfo {
   101          return buildInfo
   102  }
   103  `
   104  
   105  var BuildInfoTemplate = template.Must(template.New("version").Parse(`
   106  package buildinfo
   107  
   108  import "encoding/json"
   109  
   110  // This is GENERATED CODE, DO NOT CHECK THIS IN
   111  func init() {
   112  	CanticleDeps := json.RawMessage(` + "`{{.DepString}}`" + `)
   113  	buildInfo = &BuildInfo{
   114  		"{{.BuildTime}}",
   115  		"{{.BuildUser}}",
   116  		"{{.BuildHost}}",
   117  		"{{.Revision}}",
   118  		&CanticleDeps,
   119          }
   120  }
   121  `))