go-hep.org/x/hep@v0.38.1/version.go (about)

     1  // Copyright ©2019 The go-hep 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  //go:build go1.12
     6  
     7  package hep
     8  
     9  import (
    10  	"fmt"
    11  	"runtime/debug"
    12  )
    13  
    14  // Version returns the version of Go-HEP and its checksum.
    15  // The returned values are only valid in binaries built with module support.
    16  func Version() (version, sum string) {
    17  	b, ok := debug.ReadBuildInfo()
    18  	if !ok {
    19  		return "", ""
    20  	}
    21  	return versionOf(b)
    22  }
    23  
    24  func versionOf(b *debug.BuildInfo) (version, sum string) {
    25  	if b == nil {
    26  		return "", ""
    27  	}
    28  
    29  	const root = "go-hep.org/x/hep"
    30  	for _, m := range b.Deps {
    31  		if m.Path != root {
    32  			continue
    33  		}
    34  		if m.Replace != nil {
    35  			switch {
    36  			case m.Replace.Version != "" && m.Replace.Path != "":
    37  				return fmt.Sprintf("%s %s", m.Replace.Path, m.Replace.Version), m.Replace.Sum
    38  			case m.Replace.Version != "":
    39  				return m.Replace.Version, m.Replace.Sum
    40  			case m.Replace.Path != "":
    41  				return m.Replace.Path, m.Replace.Sum
    42  			default:
    43  				return m.Version + "*", ""
    44  			}
    45  		}
    46  		return m.Version, m.Sum
    47  	}
    48  	return "", ""
    49  }