github.com/apptainer/singularity@v3.1.1+incompatible/pkg/util/user-agent/agent.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package useragent
     7  
     8  import (
     9  	"fmt"
    10  	"runtime"
    11  	"strings"
    12  )
    13  
    14  var value string
    15  
    16  // Value contains the Singularity user agent.
    17  //
    18  // For example, "Singularity/3.0.0 (linux amd64) Go/1.10.3".
    19  func Value() string {
    20  	if value == "" {
    21  		panic("useragent.InitValue() must be called before calling useragent.Value()")
    22  	}
    23  
    24  	return value
    25  }
    26  
    27  // InitValue sets value that will be returned when
    28  // user queries singularity version.
    29  func InitValue(name, version string) {
    30  	value = fmt.Sprintf("%v (%v %v) %v",
    31  		singularityVersion(name, version),
    32  		strings.Title(runtime.GOOS),
    33  		runtime.GOARCH,
    34  		goVersion())
    35  }
    36  
    37  func singularityVersion(name, version string) string {
    38  	product := strings.Title(name)
    39  	ver := strings.Split(version, "-")[0]
    40  	return fmt.Sprintf("%v/%v", product, ver)
    41  }
    42  
    43  func goVersion() string {
    44  	version := strings.TrimPrefix(runtime.Version(), "go")
    45  	return fmt.Sprintf("Go/%v", version)
    46  }