github.com/gopherd/gonum@v0.0.4/version.go (about)

     1  // Copyright ©2019 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gonum
     6  
     7  import (
     8  	"fmt"
     9  	"runtime/debug"
    10  )
    11  
    12  const root = "github.com/gopherd/gonum"
    13  
    14  // Version returns the version of Gonum and its checksum. The returned
    15  // values are only valid in binaries built with module support.
    16  //
    17  // If a replace directive exists in the Gonum go.mod, the replace will
    18  // be reported in the version in the following format:
    19  //  "version=>[replace-path] [replace-version]"
    20  // and the replace sum will be returned in place of the original sum.
    21  //
    22  // The exact version format returned by Version may change in future.
    23  func Version() (version, sum string) {
    24  	b, ok := debug.ReadBuildInfo()
    25  	if !ok {
    26  		return "", ""
    27  	}
    28  	for _, m := range b.Deps {
    29  		if m.Path == root {
    30  			if m.Replace != nil {
    31  				switch {
    32  				case m.Replace.Version != "" && m.Replace.Path != "":
    33  					return fmt.Sprintf("%s=>%s %s", m.Version, m.Replace.Path, m.Replace.Version), m.Replace.Sum
    34  				case m.Replace.Version != "":
    35  					return fmt.Sprintf("%s=>%s", m.Version, m.Replace.Version), m.Replace.Sum
    36  				case m.Replace.Path != "":
    37  					return fmt.Sprintf("%s=>%s", m.Version, m.Replace.Path), m.Replace.Sum
    38  				default:
    39  					return m.Version + "*", m.Sum + "*"
    40  				}
    41  			}
    42  			return m.Version, m.Sum
    43  		}
    44  	}
    45  	return "", ""
    46  }