github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/buildinfo/buildinfo.go (about)

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