github.com/aquanetwork/aquachain@v1.7.8/internal/build/env.go (about) 1 // Copyright 2016 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain 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 CronJobFlag = flag.Bool("cron-job", false, `Overrides cron job status of the build`) 34 StaticFlag = flag.Bool("static", false, `Use static linking`) 35 MuslFlag = flag.Bool("musl", false, `Use musl c library`) 36 RaceFlag = flag.Bool("race", false, `Use race detector (slow runtime!)`) 37 UseUSBFlag = flag.Bool("usb", false, `Use usb (trezor/ledger)`) 38 ) 39 40 // Environment contains metadata provided by the build environment. 41 type Environment struct { 42 Name string // name of the environment 43 Repo string // name of GitHub repo 44 Commit, Branch, Tag string // Git info 45 Buildnum string 46 IsPullRequest bool 47 IsCronJob bool 48 Config map[string]bool 49 } 50 51 func (env Environment) String() string { 52 return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t config:%v)", 53 env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest, env.Config) 54 } 55 56 // Env returns metadata about the current CI environment, falling back to LocalEnv 57 // if not running on CI. 58 func Env() Environment { 59 switch { 60 case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true": 61 return Environment{ 62 Name: "travis", 63 Repo: os.Getenv("TRAVIS_REPO_SLUG"), 64 Commit: os.Getenv("TRAVIS_COMMIT"), 65 Branch: os.Getenv("TRAVIS_BRANCH"), 66 Tag: os.Getenv("TRAVIS_TAG"), 67 Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"), 68 IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false", 69 IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron", 70 Config: map[string]bool{}, 71 } 72 default: 73 return LocalEnv() 74 } 75 } 76 77 // LocalEnv returns build environment metadata gathered from git. 78 func LocalEnv() Environment { 79 env := applyEnvFlags(Environment{Name: "local", Repo: "aquachain/aquachain"}) 80 81 head := readGitFile("HEAD") 82 if splits := strings.Split(head, " "); len(splits) == 2 { 83 head = splits[1] 84 } else { 85 return env 86 } 87 if env.Commit == "" { 88 env.Commit = readGitFile(head) 89 } 90 if env.Branch == "" { 91 if head != "HEAD" { 92 env.Branch = strings.TrimPrefix(head, "refs/heads/") 93 } 94 } 95 if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" { 96 env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD")) 97 } 98 return env 99 } 100 101 func firstLine(s string) string { 102 return strings.Split(s, "\n")[0] 103 } 104 105 func applyEnvFlags(env Environment) Environment { 106 if !flag.Parsed() { 107 panic("you need to call flag.Parse before Env or LocalEnv") 108 } 109 if *GitCommitFlag != "" { 110 env.Commit = *GitCommitFlag 111 } 112 if *GitBranchFlag != "" { 113 env.Branch = *GitBranchFlag 114 } 115 if *GitTagFlag != "" { 116 env.Tag = *GitTagFlag 117 } 118 if *BuildnumFlag != "" { 119 env.Buildnum = *BuildnumFlag 120 } 121 if *PullRequestFlag { 122 env.IsPullRequest = true 123 } 124 if *CronJobFlag { 125 env.IsCronJob = true 126 } 127 128 if env.Config == nil { 129 env.Config = map[string]bool{} 130 } 131 132 if *StaticFlag { 133 env.Config["static"] = *StaticFlag 134 } 135 136 if *MuslFlag { 137 env.Config["musl"] = *MuslFlag 138 } 139 140 if *RaceFlag { 141 env.Config["race"] = *RaceFlag // uh-oh 142 } 143 144 if *UseUSBFlag { 145 env.Config["usb"] = *UseUSBFlag 146 } 147 148 return env 149 }