-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: add state routes and build info
- Loading branch information
Showing
18 changed files
with
388 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package api | ||
|
||
import "time" | ||
|
||
type ( | ||
// BuildState contains static information about the build. | ||
BuildState struct { | ||
Network string `json:"network"` | ||
Version string `json:"version"` | ||
Commit string `json:"commit"` | ||
OS string `json:"OS"` | ||
BuildTime time.Time `json:"buildTime"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//go:build ignore | ||
|
||
// This script generates meta.go which contains version info for the hostd binary. It can be run with `go generate`. | ||
// | ||
//go:generate -command go run gen.go | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
const logFormat = `{%n "commit": "%H",%n "shortCommit": "%h",%n "timestamp": "%cD",%n "tag": "%(describe:tags=true)"%n}` | ||
|
||
type ( | ||
gitTime time.Time | ||
|
||
gitMeta struct { | ||
Commit string `json:"commit"` | ||
ShortCommit string `json:"shortCommit"` | ||
Timestamp gitTime `json:"timestamp"` | ||
Tag string `json:"tag"` | ||
} | ||
) | ||
|
||
var buildTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT. | ||
// This file was generated by go generate at {{ .RunTime }}. | ||
package build | ||
import ( | ||
"time" | ||
) | ||
const ( | ||
commit = "{{ .Commit }}" | ||
version = "{{ .Version }}" | ||
buildTime = {{ .UnixTimestamp }} | ||
) | ||
// Commit returns the commit hash of hostd | ||
func Commit() string { | ||
return commit | ||
} | ||
// Version returns the version of hostd | ||
func Version() string { | ||
return version | ||
} | ||
// BuildTime returns the time at which the binary was built. | ||
func BuildTime() time.Time { | ||
return time.Unix(buildTime, 0) | ||
}`)) | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface. | ||
func (t *gitTime) UnmarshalJSON(buf []byte) error { | ||
timeFormats := []string{ | ||
time.RFC1123Z, | ||
"Mon, 2 Jan 2006 15:04:05 -0700", | ||
"2006-01-02 15:04:05 -0700", | ||
time.UnixDate, | ||
time.ANSIC, | ||
time.RFC3339, | ||
time.RFC1123, | ||
} | ||
|
||
for _, format := range timeFormats { | ||
parsed, err := time.Parse(format, strings.Trim(string(buf), `"`)) | ||
if err == nil { | ||
*t = gitTime(parsed) | ||
return nil | ||
} | ||
} | ||
return errors.New("failed to parse time") | ||
} | ||
|
||
func getGitMeta() (meta gitMeta, _ error) { | ||
cmd := exec.Command("git", "log", "-1", "--pretty=format:"+logFormat+"") | ||
buf, err := cmd.Output() | ||
if err != nil { | ||
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 { | ||
return gitMeta{}, fmt.Errorf("command failed: %w", errors.New(string(err.Stderr))) | ||
} | ||
return gitMeta{}, fmt.Errorf("failed to execute command: %w", err) | ||
} else if err := json.Unmarshal(buf, &meta); err != nil { | ||
return gitMeta{}, fmt.Errorf("failed to unmarshal json: %w", err) | ||
} | ||
return | ||
} | ||
|
||
func main() { | ||
meta, err := getGitMeta() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
commit := meta.ShortCommit | ||
version := meta.Tag | ||
if len(version) == 0 { | ||
// no version, use commit and current time for development | ||
version = commit | ||
meta.Timestamp = gitTime(time.Now()) | ||
} | ||
|
||
f, err := os.Create("meta.go") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer f.Close() | ||
|
||
err = buildTemplate.Execute(f, struct { | ||
Commit string | ||
Version string | ||
UnixTimestamp int64 | ||
|
||
RunTime string | ||
}{ | ||
Commit: commit, | ||
Version: version, | ||
UnixTimestamp: time.Time(meta.Timestamp).Unix(), | ||
|
||
RunTime: time.Now().Format(time.RFC3339), | ||
}) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.