From 6a8f6357e2c695607d1b8d9590a558bde1b90232 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Thu, 8 Apr 2021 20:42:04 +0300 Subject: [PATCH] Pull request: all: mv IsOpenWrt Updates #2829. Squashed commit of the following: commit a284a26ba5df101c78e6f866d1bdfa540d205666 Author: Ainar Garipov Date: Thu Apr 8 20:34:20 2021 +0300 aghos: fix darwin commit 9dbd42d75ebb048c83dbd06a1f09d950b3d12181 Author: Ainar Garipov Date: Thu Apr 8 20:11:56 2021 +0300 all: mv IsOpenWrt --- Makefile | 8 +++ internal/aghos/os.go | 5 ++ internal/aghos/{os_unix.go => os_darwin.go} | 6 ++- internal/aghos/os_freebsd.go | 4 ++ internal/aghos/os_linux.go | 42 ++++++++++++++++ internal/aghos/os_windows.go | 4 ++ internal/home/service.go | 7 ++- internal/util/autohosts.go | 7 ++- internal/util/helpers.go | 55 --------------------- scripts/hooks/pre-commit | 2 +- 10 files changed, 78 insertions(+), 62 deletions(-) rename internal/aghos/{os_unix.go => os_darwin.go} (89%) delete mode 100644 internal/util/helpers.go diff --git a/Makefile b/Makefile index 2d8d5e2c..d4441c47 100644 --- a/Makefile +++ b/Makefile @@ -90,5 +90,13 @@ go-test: ; $(ENV) RACE='1' "$(SHELL)" ./scripts/make/go-test.sh go-check: go-tools go-lint go-test +# A quick check to make sure that all supported operating systems can be +# typechecked and built successfully. +go-os-check: + env GOOS='darwin' go vet ./internal/... + env GOOS='freebsd' go vet ./internal/... + env GOOS='linux' go vet ./internal/... + env GOOS='windows' go vet ./internal/... + openapi-lint: ; cd ./openapi/ && $(YARN) test openapi-show: ; cd ./openapi/ && $(YARN) start diff --git a/internal/aghos/os.go b/internal/aghos/os.go index 4d535100..827591fa 100644 --- a/internal/aghos/os.go +++ b/internal/aghos/os.go @@ -45,3 +45,8 @@ func RunCommand(command string, arguments ...string) (int, string, error) { return cmd.ProcessState.ExitCode(), string(out), nil } + +// IsOpenWrt returns true if host OS is OpenWrt. +func IsOpenWrt() (ok bool) { + return isOpenWrt() +} diff --git a/internal/aghos/os_unix.go b/internal/aghos/os_darwin.go similarity index 89% rename from internal/aghos/os_unix.go rename to internal/aghos/os_darwin.go index 5e0ffef6..1fd6ac56 100644 --- a/internal/aghos/os_unix.go +++ b/internal/aghos/os_darwin.go @@ -1,4 +1,4 @@ -// +build aix darwin dragonfly netbsd openbsd solaris +// +build darwin package aghos @@ -30,3 +30,7 @@ func haveAdminRights() (bool, error) { func sendProcessSignal(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) } + +func isOpenWrt() (ok bool) { + return false +} diff --git a/internal/aghos/os_freebsd.go b/internal/aghos/os_freebsd.go index 40965e3a..e9004697 100644 --- a/internal/aghos/os_freebsd.go +++ b/internal/aghos/os_freebsd.go @@ -30,3 +30,7 @@ func haveAdminRights() (bool, error) { func sendProcessSignal(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) } + +func isOpenWrt() (ok bool) { + return false +} diff --git a/internal/aghos/os_linux.go b/internal/aghos/os_linux.go index 7c74bbd0..c68ceb37 100644 --- a/internal/aghos/os_linux.go +++ b/internal/aghos/os_linux.go @@ -3,7 +3,11 @@ package aghos import ( + "bytes" + "io/ioutil" "os" + "path/filepath" + "strings" "syscall" "github.com/AdguardTeam/golibs/log" @@ -37,3 +41,41 @@ func haveAdminRights() (bool, error) { func sendProcessSignal(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) } + +func isOpenWrt() (ok bool) { + const etcDir = "/etc" + + // TODO(e.burkov): Take care of dealing with fs package after updating + // Go version to 1.16. + fileInfos, err := ioutil.ReadDir(etcDir) + if err != nil { + return false + } + + // fNameSubstr is a part of a name of the desired file. + const fNameSubstr = "release" + osNameData := []byte("OpenWrt") + + for _, fileInfo := range fileInfos { + if fileInfo.IsDir() { + continue + } + + fn := fileInfo.Name() + if !strings.Contains(fn, fNameSubstr) { + continue + } + + var body []byte + body, err = ioutil.ReadFile(filepath.Join(etcDir, fn)) + if err != nil { + continue + } + + if bytes.Contains(body, osNameData) { + return true + } + } + + return false +} diff --git a/internal/aghos/os_windows.go b/internal/aghos/os_windows.go index 738889b4..510d8535 100644 --- a/internal/aghos/os_windows.go +++ b/internal/aghos/os_windows.go @@ -40,3 +40,7 @@ func haveAdminRights() (bool, error) { func sendProcessSignal(pid int, sig syscall.Signal) error { return fmt.Errorf("not supported on Windows") } + +func isOpenWrt() (ok bool) { + return false +} diff --git a/internal/home/service.go b/internal/home/service.go index 4935212a..69375cef 100644 --- a/internal/home/service.go +++ b/internal/home/service.go @@ -10,7 +10,6 @@ import ( "syscall" "github.com/AdguardTeam/AdGuardHome/internal/aghos" - "github.com/AdguardTeam/AdGuardHome/internal/util" "github.com/AdguardTeam/golibs/log" "github.com/kardianos/service" ) @@ -215,7 +214,7 @@ func handleServiceInstallCommand(s service.Service) { log.Fatal(err) } - if util.IsOpenWrt() { + if aghos.IsOpenWrt() { // On OpenWrt it is important to run enable after the service installation // Otherwise, the service won't start on the system startup _, err = runInitdCommand("enable") @@ -242,7 +241,7 @@ Click on the link below and follow the Installation Wizard steps to finish setup // handleServiceStatusCommand handles service "uninstall" command func handleServiceUninstallCommand(s service.Service) { - if util.IsOpenWrt() { + if aghos.IsOpenWrt() { // On OpenWrt it is important to run disable command first // as it will remove the symlink _, err := runInitdCommand("disable") @@ -290,7 +289,7 @@ func configureService(c *service.Config) { c.Option["SysvScript"] = sysvScript // On OpenWrt we're using a different type of sysvScript. - if util.IsOpenWrt() { + if aghos.IsOpenWrt() { c.Option["SysvScript"] = openWrtScript } else if runtime.GOOS == "freebsd" { c.Option["SysvScript"] = freeBSDScript diff --git a/internal/util/autohosts.go b/internal/util/autohosts.go index 22602ac4..a9b63c77 100644 --- a/internal/util/autohosts.go +++ b/internal/util/autohosts.go @@ -1,3 +1,7 @@ +// Package util contains various utilities. +// +// TODO(a.garipov): Such packages are widely considered an antipattern. Remove +// this when we refactor our project structure. package util import ( @@ -13,6 +17,7 @@ import ( "sync" "github.com/AdguardTeam/AdGuardHome/internal/aghnet" + "github.com/AdguardTeam/AdGuardHome/internal/aghos" "github.com/AdguardTeam/golibs/log" "github.com/fsnotify/fsnotify" "github.com/miekg/dns" @@ -68,7 +73,7 @@ func (a *AutoHosts) Init(hostsFn string) { a.hostsFn = hostsFn } - if IsOpenWrt() { + if aghos.IsOpenWrt() { // OpenWrt: "/tmp/hosts/dhcp.cfg01411c". a.hostsDirs = append(a.hostsDirs, "/tmp/hosts") } diff --git a/internal/util/helpers.go b/internal/util/helpers.go deleted file mode 100644 index f97635b7..00000000 --- a/internal/util/helpers.go +++ /dev/null @@ -1,55 +0,0 @@ -// Package util contains various utilities. -// -// TODO(a.garipov): Such packages are widely considered an antipattern. Remove -// this when we refactor our project structure. -package util - -import ( - "bytes" - "io/ioutil" - "path/filepath" - "runtime" - "strings" -) - -// IsOpenWrt returns true if host OS is OpenWrt. -func IsOpenWrt() bool { - if runtime.GOOS != "linux" { - return false - } - - const etcDir = "/etc" - - // TODO(e.burkov): Take care of dealing with fs package after updating - // Go version to 1.16. - fileInfos, err := ioutil.ReadDir(etcDir) - if err != nil { - return false - } - - // fNameSubstr is a part of a name of the desired file. - const fNameSubstr = "release" - osNameData := []byte("OpenWrt") - - for _, fileInfo := range fileInfos { - if fileInfo.IsDir() { - continue - } - - if !strings.Contains(fileInfo.Name(), fNameSubstr) { - continue - } - - var body []byte - body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name())) - if err != nil { - continue - } - - if bytes.Contains(body, osNameData) { - return true - } - } - - return false -} diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit index 859f536f..885e3c9f 100755 --- a/scripts/hooks/pre-commit +++ b/scripts/hooks/pre-commit @@ -14,7 +14,7 @@ fi if [ "$(git diff --cached --name-only -- '*.go' 'go.mod')" ] then - make go-lint go-test + make go-os-check go-lint go-test fi if [ "$(git diff --cached --name-only -- './openapi/openapi.yaml')" ]