bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/cwversion/version.go (about)

     1  package cwversion
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"strings"
     9  
    10  	version "github.com/hashicorp/go-version"
    11  )
    12  
    13  /*
    14  
    15  Given a version number MAJOR.MINOR.PATCH, increment the:
    16  
    17  	MAJOR version when you make incompatible API changes,
    18  	MINOR version when you add functionality in a backwards compatible manner, and
    19  	PATCH version when you make backwards compatible bug fixes.
    20  
    21  Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
    22  
    23  */
    24  
    25  var (
    26  	Version             string // = "v0.0.0"
    27  	Codename            string // = "SoumSoum"
    28  	BuildDate           string // = "I don't remember exactly"
    29  	Tag                 string // = "dev"
    30  	GoVersion           string // = "1.13"
    31  	Constraint_parser   = ">= 1.0, <= 2.0"
    32  	Constraint_scenario = ">= 1.0, < 3.0"
    33  	Constraint_api      = "v1"
    34  	Constraint_acquis   = ">= 1.0, < 2.0"
    35  )
    36  
    37  func ShowStr() string {
    38  	ret := ""
    39  	ret += fmt.Sprintf("version: %s-%s\n", Version, Tag)
    40  	ret += fmt.Sprintf("Codename: %s\n", Codename)
    41  	ret += fmt.Sprintf("BuildDate: %s\n", BuildDate)
    42  	ret += fmt.Sprintf("GoVersion: %s\n", GoVersion)
    43  	return ret
    44  }
    45  
    46  func Show() {
    47  	log.Printf("version: %s-%s", Version, Tag)
    48  	log.Printf("Codename: %s", Codename)
    49  	log.Printf("BuildDate: %s", BuildDate)
    50  	log.Printf("GoVersion: %s", GoVersion)
    51  	log.Printf("Constraint_parser: %s", Constraint_parser)
    52  	log.Printf("Constraint_scenario: %s", Constraint_scenario)
    53  	log.Printf("Constraint_api: %s", Constraint_api)
    54  	log.Printf("Constraint_acquis: %s", Constraint_acquis)
    55  }
    56  
    57  func VersionStr() string {
    58  	return fmt.Sprintf("%s-%s", Version, Tag)
    59  }
    60  
    61  func VersionStrip() string {
    62  	version := strings.Split(Version, "-")
    63  	return version[0]
    64  }
    65  
    66  func Statisfies(strvers string, constraint string) (bool, error) {
    67  	vers, err := version.NewVersion(strvers)
    68  	if err != nil {
    69  		return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err)
    70  	}
    71  	constraints, err := version.NewConstraint(constraint)
    72  	if err != nil {
    73  		return false, fmt.Errorf("failed to parse constraint '%s'", constraint)
    74  	}
    75  	if !constraints.Check(vers) {
    76  		return false, nil
    77  	}
    78  	return true, nil
    79  }
    80  
    81  // Latest return latest synsec version based on github
    82  func Latest() (string, error) {
    83  	latest := make(map[string]interface{})
    84  
    85  	resp, err := http.Get("https://api.github.com/repos/breakteam/synsec/releases/latest")
    86  	if err != nil {
    87  		return "", err
    88  	}
    89  	defer resp.Body.Close()
    90  
    91  	err = json.NewDecoder(resp.Body).Decode(&latest)
    92  	if err != nil {
    93  		return "", err
    94  	}
    95  	if _, ok := latest["name"]; !ok {
    96  		return "", fmt.Errorf("unable to find latest release name from github api: %+v", latest)
    97  	}
    98  
    99  	return latest["name"].(string), nil
   100  }