github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/inspector/check/distro.go (about) 1 package check 2 3 import ( 4 "bufio" 5 "errors" 6 "fmt" 7 "io" 8 "os" 9 "runtime" 10 "strings" 11 ) 12 13 const ( 14 Ubuntu Distro = "ubuntu" 15 RHEL Distro = "rhel" 16 CentOS Distro = "centos" 17 Darwin Distro = "darwin" 18 Unsupported Distro = "" 19 ) 20 21 // Distro is a Linux distribution that the inspector supports 22 type Distro string 23 24 // DetectDistro uses the /etc/os-release file to get distro information. 25 func DetectDistro() (Distro, error) { 26 if runtime.GOOS == "darwin" { 27 return Darwin, nil 28 } 29 f, err := os.Open("/etc/os-release") 30 if err != nil { 31 return Unsupported, fmt.Errorf("error reading /etc/os-release file: %v", err) 32 } 33 defer f.Close() 34 return detectDistroFromOSRelease(f) 35 } 36 37 func detectDistroFromOSRelease(r io.Reader) (Distro, error) { 38 idLine := "" 39 s := bufio.NewScanner(r) 40 for s.Scan() { 41 l := s.Text() 42 if strings.HasPrefix(l, "ID=") { 43 idLine = l 44 break 45 } 46 } 47 if idLine == "" { 48 return Unsupported, errors.New("/etc/os-release file does not contain ID= field") 49 } 50 fields := strings.Split(idLine, "=") 51 if len(fields) != 2 { 52 return Unsupported, fmt.Errorf("Unknown format of /etc/os-release file. ID line was: %s", idLine) 53 } 54 55 // Remove double-quotes from field value 56 switch strings.Replace(fields[1], "\"", "", -1) { 57 case "centos": 58 return CentOS, nil 59 case "rhel": 60 return RHEL, nil 61 case "ubuntu": 62 return Ubuntu, nil 63 default: 64 return Unsupported, fmt.Errorf("Unsupported distribution detected: %s", fields[1]) 65 } 66 }