github.com/btcsuite/btcd@v0.24.0/version.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // semanticAlphabet
    14  const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
    15  
    16  // These constants define the application version and follow the semantic
    17  // versioning 2.0.0 spec (http://semver.org/).
    18  const (
    19  	appMajor uint = 0
    20  	appMinor uint = 24
    21  	appPatch uint = 0
    22  
    23  	// appPreRelease MUST only contain characters from semanticAlphabet
    24  	// per the semantic versioning spec.
    25  	appPreRelease = "beta"
    26  )
    27  
    28  // appBuild is defined as a variable so it can be overridden during the build
    29  // process with '-ldflags "-X main.appBuild foo' if needed.  It MUST only
    30  // contain characters from semanticAlphabet per the semantic versioning spec.
    31  var appBuild string
    32  
    33  // version returns the application version as a properly formed string per the
    34  // semantic versioning 2.0.0 spec (http://semver.org/).
    35  func version() string {
    36  	// Start with the major, minor, and patch versions.
    37  	version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)
    38  
    39  	// Append pre-release version if there is one.  The hyphen called for
    40  	// by the semantic versioning spec is automatically appended and should
    41  	// not be contained in the pre-release string.  The pre-release version
    42  	// is not appended if it contains invalid characters.
    43  	preRelease := normalizeVerString(appPreRelease)
    44  	if preRelease != "" {
    45  		version = fmt.Sprintf("%s-%s", version, preRelease)
    46  	}
    47  
    48  	// Append build metadata if there is any.  The plus called for
    49  	// by the semantic versioning spec is automatically appended and should
    50  	// not be contained in the build metadata string.  The build metadata
    51  	// string is not appended if it contains invalid characters.
    52  	build := normalizeVerString(appBuild)
    53  	if build != "" {
    54  		version = fmt.Sprintf("%s+%s", version, build)
    55  	}
    56  
    57  	return version
    58  }
    59  
    60  // normalizeVerString returns the passed string stripped of all characters which
    61  // are not valid according to the semantic versioning guidelines for pre-release
    62  // version and build metadata strings.  In particular they MUST only contain
    63  // characters in semanticAlphabet.
    64  func normalizeVerString(str string) string {
    65  	var result bytes.Buffer
    66  	for _, r := range str {
    67  		if strings.ContainsRune(semanticAlphabet, r) {
    68  			result.WriteRune(r)
    69  		}
    70  	}
    71  	return result.String()
    72  }