From 4ac7e99c6af6571e3ac88f086d9b945e49e1a355 Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 19 Nov 2022 11:37:38 +0000 Subject: [PATCH 1/2] [disk][host]: move back Warnings from internal to disk and host. fix #1377 --- disk/disk_windows.go | 2 +- {internal/common => disk}/warnings.go | 2 +- host/host_linux.go | 2 +- host/warnings.go | 30 +++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) rename {internal/common => disk}/warnings.go (96%) create mode 100644 host/warnings.go diff --git a/disk/disk_windows.go b/disk/disk_windows.go index b7f0c515e..3e3d99114 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -80,7 +80,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { } func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { - warnings := common.Warnings{ + warnings := Warnings{ Verbose: true, } var ret []PartitionStat diff --git a/internal/common/warnings.go b/disk/warnings.go similarity index 96% rename from internal/common/warnings.go rename to disk/warnings.go index a4aaadaf5..ce13aa925 100644 --- a/internal/common/warnings.go +++ b/disk/warnings.go @@ -1,4 +1,4 @@ -package common +package disk import "fmt" diff --git a/host/host_linux.go b/host/host_linux.go index a6bb3fca2..940415c9c 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -395,7 +395,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err } } - var warns common.Warnings + var warns Warnings if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files files, err = filepath.Glob(common.HostSys("/class/thermal/thermal_zone*/")) diff --git a/host/warnings.go b/host/warnings.go new file mode 100644 index 000000000..182a2937b --- /dev/null +++ b/host/warnings.go @@ -0,0 +1,30 @@ +package host + +import "fmt" + +type Warnings struct { + List []error + Verbose bool +} + +func (w *Warnings) Add(err error) { + w.List = append(w.List, err) +} + +func (w *Warnings) Reference() error { + if len(w.List) > 0 { + return w + } + return nil +} + +func (w *Warnings) Error() string { + if w.Verbose { + str := "" + for i, e := range w.List { + str += fmt.Sprintf("\tError %d: %s\n", i, e.Error()) + } + return str + } + return fmt.Sprintf("Number of warnings: %v", len(w.List)) +} From 5473fd114d2bf9a10bd886649c12982d189b8f67 Mon Sep 17 00:00:00 2001 From: shirou Date: Wed, 30 Nov 2022 23:17:41 +0000 Subject: [PATCH 2/2] [disk][host]: change to use type alias. --- disk/disk_windows.go | 2 ++ host/host_linux.go | 2 ++ host/warnings.go | 30 --------------------------- {disk => internal/common}/warnings.go | 2 +- 4 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 host/warnings.go rename {disk => internal/common}/warnings.go (96%) diff --git a/disk/disk_windows.go b/disk/disk_windows.go index 3e3d99114..6aa47a702 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -15,6 +15,8 @@ import ( "golang.org/x/sys/windows/registry" ) +type Warnings = common.Warnings + var ( procGetDiskFreeSpaceExW = common.Modkernel32.NewProc("GetDiskFreeSpaceExW") procGetLogicalDriveStringsW = common.Modkernel32.NewProc("GetLogicalDriveStringsW") diff --git a/host/host_linux.go b/host/host_linux.go index 940415c9c..949babac4 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -19,6 +19,8 @@ import ( "golang.org/x/sys/unix" ) +type Warnings = common.Warnings + type lsbStruct struct { ID string Release string diff --git a/host/warnings.go b/host/warnings.go deleted file mode 100644 index 182a2937b..000000000 --- a/host/warnings.go +++ /dev/null @@ -1,30 +0,0 @@ -package host - -import "fmt" - -type Warnings struct { - List []error - Verbose bool -} - -func (w *Warnings) Add(err error) { - w.List = append(w.List, err) -} - -func (w *Warnings) Reference() error { - if len(w.List) > 0 { - return w - } - return nil -} - -func (w *Warnings) Error() string { - if w.Verbose { - str := "" - for i, e := range w.List { - str += fmt.Sprintf("\tError %d: %s\n", i, e.Error()) - } - return str - } - return fmt.Sprintf("Number of warnings: %v", len(w.List)) -} diff --git a/disk/warnings.go b/internal/common/warnings.go similarity index 96% rename from disk/warnings.go rename to internal/common/warnings.go index ce13aa925..a4aaadaf5 100644 --- a/disk/warnings.go +++ b/internal/common/warnings.go @@ -1,4 +1,4 @@ -package disk +package common import "fmt"