1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+ "runtime"
7
+ "strings"
8
+ )
9
+
10
+ func main () {
11
+ osType := runtime .GOOS
12
+ fmt .Println ("Detected OS Type:" ,osType )
13
+
14
+ switch (osType ){
15
+ case "darwin" :
16
+ fmt .Println ("MacOS is not supported" )
17
+ case "windows" :
18
+ fmt .Println ("Windows is not supported" )
19
+ case "linux" :
20
+ distro , base_distro := fetchLinusDistro ()
21
+ if (distro == "unknown" && base_distro == "unknown" ){fmt .Println ("Failed to fetch the details of your distro" )}
22
+ fmt .Println ("Distribution:" , distro )
23
+ fmt .Println ("Built On:" , base_distro )
24
+ default :
25
+ fmt .Println ("OS not supported" )
26
+
27
+ }
28
+ }
29
+
30
+ func fetchLinusDistro () (string , string ) {
31
+ data , err := os .ReadFile ("/etc/os-release" )
32
+
33
+ if (err != nil ) {return "unknown" , "unknown" }
34
+ var distro , base_distro string
35
+
36
+ lines := strings .Split (string (data ),"\n " )
37
+ for _ , line := range lines {
38
+ if strings .HasPrefix (line , "ID=" ){
39
+ distro = strings .Trim (strings .SplitN (line ,"=" ,2 )[1 ],`"` )
40
+ }
41
+ if (strings .HasPrefix (line , "ID_LIKE=" )){
42
+ base_distro = strings .Trim (strings .SplitN (line , "=" ,2 )[1 ],`"` )
43
+ }
44
+ }
45
+
46
+ return distro , base_distro
47
+ }
0 commit comments