github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/version.go (about)

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