github.com/elfadel/cilium@v1.6.12/pkg/version/version.go (about)

     1  // Copyright 2017 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package version
    16  
    17  import (
    18  	"encoding/base64"
    19  	"encoding/json"
    20  	"strings"
    21  )
    22  
    23  // CiliumVersion provides a minimal structure to the version string
    24  type CiliumVersion struct {
    25  	// Version is the semantic version of Cilium
    26  	Version string
    27  	// Revision is the short SHA from the last commit
    28  	Revision string
    29  	// GoRuntimeVersion is the Go version used to run Cilium
    30  	GoRuntimeVersion string
    31  	// Arch is the architecture where Cilium was compiled
    32  	Arch string
    33  	// AuthorDate is the git author time reference stored as string ISO 8601 formatted
    34  	AuthorDate string
    35  }
    36  
    37  // Version is set during build
    38  var Version string
    39  
    40  // FromString converts a version string into struct
    41  func FromString(versionString string) CiliumVersion {
    42  	// string to parse: "0.13.90 a722bdb 2018-01-09T22:32:37+01:00 go version go1.9 linux/amd64"
    43  	fields := strings.Split(versionString, " ")
    44  	if len(fields) != 7 {
    45  		return CiliumVersion{}
    46  	}
    47  
    48  	cver := CiliumVersion{
    49  		Version:          fields[0],
    50  		Revision:         fields[1],
    51  		AuthorDate:       fields[2],
    52  		GoRuntimeVersion: fields[5],
    53  		Arch:             fields[6],
    54  	}
    55  	return cver
    56  }
    57  
    58  // GetCiliumVersion returns a initialized CiliumVersion structure
    59  func GetCiliumVersion() CiliumVersion {
    60  	return FromString(Version)
    61  }
    62  
    63  // Base64 returns the version in a base64 format.
    64  func Base64() (string, error) {
    65  	jsonBytes, err := json.Marshal(Version)
    66  	if err != nil {
    67  		return "", err
    68  	}
    69  	return base64.StdEncoding.EncodeToString(jsonBytes), nil
    70  }