summaryrefslogtreecommitdiff
path: root/distro.go
blob: a912e2f0a3e03e3859c8c9e7a318273f1a105417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2024 WIT.COM Inc.

package debian

// simple stab at making a human readable distro name
// this is for displaying in a table in the zookeeper app
// it's just so you can easily see what machines in your grid are
// doing what

import (
	"bufio"
	"fmt"
	"os"
	"runtime"
	"strings"
)

func initDistro() string {
	switch runtime.GOOS {
	case "windows":
		return "windows"
	case "macos":
		return "macos"
	case "linux":
		// Detect the Linux distribution
		distro := detectDistro()
		if distro == "" {
			fmt.Println("Unable to detect Linux distribution.")
			distro = "fixme"
		}

		fmt.Printf("Detected distribution: %s\n", distro)
		return distro
	default:
		return runtime.GOOS
	}
}

// detectDistro returns the Linux distribution name (if possible)
func detectDistro() string {
	// Check if we're on Linux

	// Try to read /etc/os-release to determine the distro
	file, err := os.Open("/etc/os-release")
	if err != nil {
		return ""
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "ID=") {
			parts := strings.SplitN(line, "=", 2)
			if len(parts) == 2 {
				return strings.Trim(parts[1], `"`)
			}
		}
	}
	return ""
}