github.com/searKing/golang/go@v1.2.117/os/distribution/distribution.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package distribution 6 7 import ( 8 "os/exec" 9 "runtime" 10 "strings" 11 ) 12 13 type Distribution int 14 15 const ( 16 Windows Distribution = iota 17 Ubuntu 18 Redhat 19 Centos 20 Butt 21 ) 22 23 func (d Distribution) String() string { 24 switch d { 25 case Windows: 26 return "Windows" 27 case Ubuntu: 28 return "Ubuntu" 29 case Redhat: 30 return "Redhat" 31 case Centos: 32 return "Centos" 33 } 34 return "Butt" 35 } 36 37 func GetOSVersion() Distribution { 38 if runtime.GOOS == "Windows" { 39 return Windows 40 } 41 42 params := []string{ 43 "cat", 44 "/proc/version", 45 } 46 data, err := exec.Command(params[0], params[1:]...).CombinedOutput() 47 if err != nil { 48 return Butt 49 } 50 51 versionInfo := strings.ToLower(string(data)) 52 53 switch { 54 case strings.Contains(versionInfo, "red hat"): 55 return Redhat 56 case strings.Contains(versionInfo, "centos"): 57 return Centos 58 case strings.Contains(versionInfo, "ubuntu"): 59 return Ubuntu 60 } 61 return Butt 62 }