buildinfo.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package buildinfo
  20. import (
  21. "encoding/json"
  22. "strings"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/values"
  24. )
  25. /*
  26. These values should be filled in at build time using the `-X` option[1] to the
  27. Go linker (probably via `-ldflags` option to `go build` -- like `-ldflags "-X var1=abc -X var2=xyz"`).
  28. [1]: http://golang.org/cmd/ld/
  29. Without those build flags, the build info in the notice will simply be empty strings.
  30. Suggestions for how to fill in the values will be given for each variable.
  31. Note that any passed value must contain no whitespace.
  32. */
  33. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=`date --iso-8601=seconds`
  34. var buildDate string
  35. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=`git config --get remote.origin.url`
  36. var buildRepo string
  37. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=`git rev-parse --short HEAD`
  38. var buildRev string
  39. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=`go version | perl -ne '/go version (.*?) / && print $1'`
  40. var goVersion string
  41. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=`gomobile version | perl -ne '/gomobile version (.*?) / && print $1'`
  42. var gomobileVersion string
  43. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/common/buildinfo.dependencies=`echo -n "{" && go list -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | xargs -I pkg bash -c 'cd $GOPATH/src/pkg && echo -n "\"pkg\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/'`
  44. // Dependencies should be listed as a JSON object like the following (no spaces) {"github.com/Psiphon-Labs/psiphon-tunnel-core":"abcdef","...":"..."}
  45. var dependencies string
  46. // BuildInfo captures relevant build information here for use in clients or servers
  47. type BuildInfo struct {
  48. BuildDate string `json:"buildDate"`
  49. BuildRepo string `json:"buildRepo"`
  50. BuildRev string `json:"buildRev"`
  51. GoVersion string `json:"goVersion"`
  52. GomobileVersion string `json:"gomobileVersion,omitempty"`
  53. Dependencies json.RawMessage `json:"dependencies"`
  54. ValuesRev string `json:"valuesRev"`
  55. }
  56. // ToMap converts 'BuildInfo' struct to 'map[string]interface{}'
  57. func (bi *BuildInfo) ToMap() map[string]interface{} {
  58. var dependenciesMap map[string]interface{}
  59. json.Unmarshal([]byte(bi.Dependencies), &dependenciesMap)
  60. buildInfoMap := map[string]interface{}{
  61. "buildDate": bi.BuildDate,
  62. "buildRepo": bi.BuildRepo,
  63. "buildRev": bi.BuildRev,
  64. "goVersion": bi.GoVersion,
  65. "dependencies": dependenciesMap,
  66. "valuesRev": bi.ValuesRev,
  67. }
  68. return buildInfoMap
  69. }
  70. // GetBuildInfo returns an instance of the BuildInfo struct
  71. func GetBuildInfo() *BuildInfo {
  72. if strings.TrimSpace(dependencies) == "" {
  73. dependencies = "{}"
  74. }
  75. buildInfo := BuildInfo{
  76. BuildDate: strings.TrimSpace(buildDate),
  77. BuildRepo: strings.TrimSpace(buildRepo),
  78. BuildRev: strings.TrimSpace(buildRev),
  79. GoVersion: strings.TrimSpace(goVersion),
  80. GomobileVersion: strings.TrimSpace(gomobileVersion),
  81. Dependencies: json.RawMessage(strings.TrimSpace(dependencies)),
  82. ValuesRev: values.GetRevision(),
  83. }
  84. return &buildInfo
  85. }