github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/internal/build/env.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package build 18 19 import ( 20 "flag" 21 "fmt" 22 "os" 23 "strings" 24 ) 25 26 var ( 27 // These flags override values in build env. 28 GitCommitFlag = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`) 29 GitBranchFlag = flag.String("git-branch", "", `Overrides git branch being built`) 30 GitTagFlag = flag.String("git-tag", "", `Overrides git tag being built`) 31 BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`) 32 PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`) 33 ) 34 35 // Environment contains metadata provided by the build environment. 36 type Environment struct { 37 Name string // name of the environment 38 Repo string // name of GitHub repo 39 Commit, Branch, Tag string // Git info 40 Buildnum string 41 IsPullRequest bool 42 } 43 44 func (env Environment) String() string { 45 return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)", 46 env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest) 47 } 48 49 // Env returns metadata about the current CI environment, falling back to LocalEnv 50 // if not running on CI. 51 func Env() Environment { 52 switch { 53 case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true": 54 return Environment{ 55 Name: "travis", 56 Repo: os.Getenv("TRAVIS_REPO_SLUG"), 57 Commit: os.Getenv("TRAVIS_COMMIT"), 58 Branch: os.Getenv("TRAVIS_BRANCH"), 59 Tag: os.Getenv("TRAVIS_TAG"), 60 Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"), 61 IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false", 62 } 63 case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True": 64 return Environment{ 65 Name: "appveyor", 66 Repo: os.Getenv("APPVEYOR_REPO_NAME"), 67 Commit: os.Getenv("APPVEYOR_REPO_COMMIT"), 68 Branch: os.Getenv("APPVEYOR_REPO_BRANCH"), 69 Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"), 70 Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"), 71 IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "", 72 } 73 default: 74 return LocalEnv() 75 } 76 } 77 78 // LocalEnv returns build environment metadata gathered from git. 79 func LocalEnv() Environment { 80 env := applyEnvFlags(Environment{Name: "local", Repo: "atheioschain/go-atheios"}) 81 if _, err := os.Stat(".git"); err != nil { 82 return env 83 } 84 if env.Commit == "" { 85 env.Commit = RunGit("rev-parse", "HEAD") 86 } 87 if env.Branch == "" { 88 if b := RunGit("rev-parse", "--abbrev-ref", "HEAD"); b != "HEAD" { 89 env.Branch = b 90 } 91 } 92 if env.Tag == "" { 93 env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD")) 94 } 95 return env 96 } 97 98 func firstLine(s string) string { 99 return strings.Split(s, "\n")[0] 100 } 101 102 func applyEnvFlags(env Environment) Environment { 103 if !flag.Parsed() { 104 panic("you need to call flag.Parse before Env or LocalEnv") 105 } 106 if *GitCommitFlag != "" { 107 env.Commit = *GitCommitFlag 108 } 109 if *GitBranchFlag != "" { 110 env.Branch = *GitBranchFlag 111 } 112 if *GitTagFlag != "" { 113 env.Tag = *GitTagFlag 114 } 115 if *BuildnumFlag != "" { 116 env.Buildnum = *BuildnumFlag 117 } 118 if *PullRequestFlag { 119 env.IsPullRequest = true 120 } 121 return env 122 }