github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/version/osversion.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package version
     5  
     6  import (
     7  	"io/ioutil"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/juju/loggo"
    12  )
    13  
    14  var logger = loggo.GetLogger("juju.version")
    15  
    16  func readSeries(releaseFile string) string {
    17  	data, err := ioutil.ReadFile(releaseFile)
    18  	if err != nil {
    19  		return "unknown"
    20  	}
    21  	for _, line := range strings.Split(string(data), "\n") {
    22  		const prefix = "DISTRIB_CODENAME="
    23  		if strings.HasPrefix(line, prefix) {
    24  			return strings.Trim(line[len(prefix):], "\t '\"")
    25  		}
    26  	}
    27  	return "unknown"
    28  }
    29  
    30  type kernelVersionFunc func() (string, error)
    31  
    32  // kernelToMajor takes a dotted version and returns just the Major portion
    33  func kernelToMajor(getKernelVersion kernelVersionFunc) (int, error) {
    34  	fullVersion, err := getKernelVersion()
    35  	if err != nil {
    36  		return 0, err
    37  	}
    38  	parts := strings.SplitN(fullVersion, ".", 2)
    39  	majorVersion, err := strconv.ParseInt(parts[0], 10, 32)
    40  	if err != nil {
    41  		return 0, err
    42  	}
    43  	return int(majorVersion), nil
    44  }
    45  
    46  func macOSXSeriesFromKernelVersion(getKernelVersion kernelVersionFunc) string {
    47  	majorVersion, err := kernelToMajor(getKernelVersion)
    48  	if err != nil {
    49  		logger.Infof("unable to determine OS version: %v", err)
    50  		return "unknown"
    51  	}
    52  	return macOSXSeriesFromMajorVersion(majorVersion)
    53  }
    54  
    55  // TODO(jam): 2014-05-06 https://launchpad.net/bugs/1316593
    56  // we should have a system file that we can read so this can be updated without
    57  // recompiling Juju. For now, this is a lot easier, and also solves the fact
    58  // that we want to populate version.Current.Series during init() time, before
    59  // we've potentially read that information from anywhere else
    60  // macOSXSeries maps from the Darwin Kernel Major Version to the Mac OSX
    61  // series.
    62  var macOSXSeries = map[int]string{
    63  	13: "mavericks",
    64  	12: "mountainlion",
    65  	11: "lion",
    66  	10: "snowleopard",
    67  	9:  "leopard",
    68  	8:  "tiger",
    69  	7:  "panther",
    70  	6:  "jaguar",
    71  	5:  "puma",
    72  }
    73  
    74  func macOSXSeriesFromMajorVersion(majorVersion int) string {
    75  	if series, ok := macOSXSeries[majorVersion]; ok {
    76  		return series
    77  	}
    78  	return "unknown"
    79  }