buildinfo.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 common
  20. import (
  21. "encoding/json"
  22. "strings"
  23. )
  24. /*
  25. These values should be filled in at build time using the `-X` option[1] to the
  26. Go linker (probably via `-ldflags` option to `go build` -- like `-ldflags "-X var1=abc -X var2=xyz"`).
  27. [1]: http://golang.org/cmd/ld/
  28. Without those build flags, the build info in the notice will simply be empty strings.
  29. Suggestions for how to fill in the values will be given for each variable.
  30. Note that any passed value must contain no whitespace.
  31. */
  32. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=`date --iso-8601=seconds`
  33. var buildDate string
  34. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=`git config --get remote.origin.url`
  35. var buildRepo string
  36. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=`git rev-parse --short HEAD`
  37. var buildRev string
  38. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=`go version | perl -ne '/go version (.*?) / && print $1'`
  39. var goVersion string
  40. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=`gomobile version | perl -ne '/gomobile version (.*?) / && print $1'`
  41. var gomobileVersion string
  42. // -X github.com/Psiphon-Labs/psiphon-tunnel-core/common.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/,$/}/'`
  43. // Dependencies should be listed as a JSON object like the following (no spaces) {"github.com/Psiphon-Labs/psiphon-tunnel-core":"abcdef","...":"..."}
  44. var dependencies string
  45. // Capture relevant build information here for use in clients or servers
  46. type BuildInfo struct {
  47. BuildDate string `json:"buildDate"`
  48. BuildRepo string `json:"buildRepo"`
  49. BuildRev string `json:"buildRev"`
  50. GoVersion string `json:"goVersion"`
  51. GomobileVersion string `json:"gomobileVersion,omitempty"`
  52. Dependencies json.RawMessage `json:"dependencies"`
  53. }
  54. // Convert 'BuildInfo' struct to 'map[string]interface{}'
  55. func (bi *BuildInfo) ToMap() *map[string]interface{} {
  56. var dependenciesMap map[string]interface{}
  57. json.Unmarshal([]byte(bi.Dependencies), &dependenciesMap)
  58. buildInfoMap := map[string]interface{}{
  59. "buildDate": bi.BuildDate,
  60. "buildRepo": bi.BuildRepo,
  61. "buildRev": bi.BuildRev,
  62. "goVersion": bi.GoVersion,
  63. "dependencies": dependenciesMap,
  64. }
  65. return &buildInfoMap
  66. }
  67. // Return an instance of the BuildInfo struct
  68. func GetBuildInfo() *BuildInfo {
  69. if strings.TrimSpace(dependencies) == "" {
  70. dependencies = "{}"
  71. }
  72. buildInfo := BuildInfo{
  73. BuildDate: strings.TrimSpace(buildDate),
  74. BuildRepo: strings.TrimSpace(buildRepo),
  75. BuildRev: strings.TrimSpace(buildRev),
  76. GoVersion: strings.TrimSpace(goVersion),
  77. GomobileVersion: strings.TrimSpace(gomobileVersion),
  78. Dependencies: json.RawMessage(strings.TrimSpace(dependencies)),
  79. }
  80. return &buildInfo
  81. }