package gitinfo import ( "errors" "runtime/debug" "time" ) // GitInfo is the git info for a build type GitInfo struct { Revision string Time time.Time Modified bool } // MustReadGitInfo returns GitInfo, or dev info when encountering an error func MustReadGitInfo() *GitInfo { info, err := ReadGitInfo() if err != nil { return &GitInfo{ Revision: "develop", Time: time.Now(), Modified: true, } } return info } // ReadGitInfo returns the GitInfo for a build func ReadGitInfo() (*GitInfo, error) { info, ok := readBuildInfo() if !ok { return nil, ErrNoBuildInfo } git := &GitInfo{} var vcs string for _, setting := range info.Settings { switch setting.Key { case "vcs": vcs = setting.Value case "vcs.revision": git.Revision = setting.Value case "vcs.time": t, err := time.Parse(time.RFC3339, setting.Value) if err != nil { return nil, err } git.Time = t case "vcs.modified": git.Modified = setting.Value == "true" } } if vcs != "git" { return nil, ErrNoGit } return git, nil } var ( readBuildInfo = debug.ReadBuildInfo ErrNoBuildInfo = errors.New("could not read build info") ErrNoGit = errors.New("vcs is not git") )