2020-09-11 02:55:09 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package distro reports which distro we're running on.
|
|
|
|
package distro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Distro string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Debian = Distro("debian")
|
|
|
|
Arch = Distro("arch")
|
|
|
|
Synology = Distro("synology")
|
2020-09-22 17:28:40 +00:00
|
|
|
OpenWrt = Distro("openwrt")
|
2020-12-22 04:57:35 +00:00
|
|
|
NixOS = Distro("nixos")
|
2020-09-11 02:55:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Get returns the current distro, or the empty string if unknown.
|
|
|
|
func Get() Distro {
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
return linuxDistro()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-12-22 04:57:35 +00:00
|
|
|
func have(file string) bool {
|
|
|
|
_, err := os.Stat(file)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func haveDir(file string) bool {
|
|
|
|
fi, err := os.Stat(file)
|
|
|
|
return err == nil && fi.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-09-11 02:55:09 +00:00
|
|
|
func linuxDistro() Distro {
|
2020-12-22 04:57:35 +00:00
|
|
|
switch {
|
2021-04-01 23:24:53 +00:00
|
|
|
case haveDir("/usr/syno"):
|
2020-09-11 02:55:09 +00:00
|
|
|
return Synology
|
2020-12-22 04:57:35 +00:00
|
|
|
case have("/etc/debian_version"):
|
2020-09-11 02:55:09 +00:00
|
|
|
return Debian
|
2020-12-22 04:57:35 +00:00
|
|
|
case have("/etc/arch-release"):
|
2020-09-11 02:55:09 +00:00
|
|
|
return Arch
|
2020-12-22 04:57:35 +00:00
|
|
|
case have("/etc/openwrt_version"):
|
2020-09-22 17:28:40 +00:00
|
|
|
return OpenWrt
|
2020-12-22 04:57:35 +00:00
|
|
|
case have("/run/current-system/sw/bin/nixos-version"):
|
|
|
|
return NixOS
|
2020-09-22 17:28:40 +00:00
|
|
|
}
|
2020-09-11 02:55:09 +00:00
|
|
|
return ""
|
|
|
|
}
|