github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/x/init.go (about)

     1  /*
     2   * Copyright 2016-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package x
    18  
    19  import (
    20  	"crypto/sha256"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"runtime"
    25  	"strings"
    26  
    27  	"github.com/golang/glog"
    28  )
    29  
    30  var (
    31  	initFunc []func()
    32  	isTest   bool
    33  
    34  	// These variables are set using -ldflags
    35  	dgraphVersion  string
    36  	gitBranch      string
    37  	lastCommitSHA  string
    38  	lastCommitTime string
    39  )
    40  
    41  // SetTestRun sets a variable to indicate that the current execution is a test.
    42  func SetTestRun() {
    43  	isTest = true
    44  }
    45  
    46  // IsTestRun indicates whether a test is being executed. Useful to handle special
    47  // conditions during tests that differ from normal execution.
    48  func IsTestRun() bool {
    49  	return isTest
    50  }
    51  
    52  // AddInit adds a function to be run in x.Init, which should be called at the
    53  // beginning of all mains.
    54  func AddInit(f func()) {
    55  	initFunc = append(initFunc, f)
    56  }
    57  
    58  // Init initializes flags and run all functions in initFunc.
    59  func Init() {
    60  	// Default value, would be overwritten by flag.
    61  	Config.QueryEdgeLimit = 1e6
    62  
    63  	// Next, run all the init functions that have been added.
    64  	for _, f := range initFunc {
    65  		f()
    66  	}
    67  }
    68  
    69  // BuildDetails returns a string containing details about the Dgraph binary.
    70  func BuildDetails() string {
    71  	licenseInfo := `Licensed under the Apache Public License 2.0`
    72  	if !strings.HasSuffix(dgraphVersion, "-oss") {
    73  		licenseInfo = "Licensed variously under the Apache Public License 2.0 and Dgraph " +
    74  			"Community License"
    75  	}
    76  	return fmt.Sprintf(`
    77  Dgraph version   : %v
    78  Dgraph SHA-256   : %x
    79  Commit SHA-1     : %v
    80  Commit timestamp : %v
    81  Branch           : %v
    82  Go version       : %v
    83  
    84  For Dgraph official documentation, visit https://docs.dgraph.io.
    85  For discussions about Dgraph     , visit https://discuss.dgraph.io.
    86  To say hi to the community       , visit https://dgraph.slack.com.
    87  
    88  %s.
    89  Copyright 2015-2018 Dgraph Labs, Inc.
    90  
    91  `,
    92  		dgraphVersion, ExecutableChecksum(), lastCommitSHA, lastCommitTime, gitBranch,
    93  		runtime.Version(), licenseInfo)
    94  }
    95  
    96  // PrintVersion prints version and other helpful information if --version.
    97  func PrintVersion() {
    98  	glog.Infof("\n%s\n", BuildDetails())
    99  }
   100  
   101  // Version returns a string containing the dgraphVersion.
   102  func Version() string {
   103  	return dgraphVersion
   104  }
   105  
   106  // ExecutableChecksum returns a byte slice containing the SHA256 checksum of the executable.
   107  // It returns a nil slice if there's an error trying to calculate the checksum.
   108  func ExecutableChecksum() []byte {
   109  	execPath, err := os.Executable()
   110  	if err != nil {
   111  		return nil
   112  	}
   113  	execFile, err := os.Open(execPath)
   114  	if err != nil {
   115  		return nil
   116  	}
   117  	defer execFile.Close()
   118  
   119  	h := sha256.New()
   120  	if _, err := io.Copy(h, execFile); err != nil {
   121  		return nil
   122  	}
   123  
   124  	return h.Sum(nil)
   125  }