From 13cf842f72f4a803c9118d08d155751d65b42635 Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Fri, 15 Mar 2024 20:15:55 +0000 Subject: [PATCH 1/2] Update function Execute usage Signed-off-by: Shuo Wu --- pkg/checker/checker.go | 4 ++-- pkg/installer/installer.go | 2 +- pkg/installer/spdk.go | 2 +- pkg/pkgmgr/apt.go | 22 +++++++++++----------- pkg/pkgmgr/pacman.go | 22 +++++++++++----------- pkg/pkgmgr/pkgmgr.go | 2 +- pkg/pkgmgr/yum.go | 22 +++++++++++----------- pkg/pkgmgr/zypper.go | 22 +++++++++++----------- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/pkg/checker/checker.go b/pkg/checker/checker.go index 32598983..6a309231 100644 --- a/pkg/checker/checker.go +++ b/pkg/checker/checker.go @@ -183,7 +183,7 @@ func (c *Checker) CheckHugePages() { } func (c *Checker) isHugePagesTotalEqualOrLargerThan(requiredHugePages int) (bool, error) { - output, err := c.pkgMgr.Execute("grep", []string{"HugePages_Total", "/proc/meminfo"}, lhtypes.ExecuteNoTimeout) + output, err := c.pkgMgr.Execute([]string{}, "grep", []string{"HugePages_Total", "/proc/meminfo"}, lhtypes.ExecuteNoTimeout) if err != nil { return false, errors.Wrap(err, "failed to get total number of HugePages") } @@ -211,7 +211,7 @@ func (c *Checker) CheckCpuInstructionSet(instructionSets map[string][]string) { } for _, set := range sets { - _, err := c.pkgMgr.Execute("grep", []string{set, "/proc/cpuinfo"}, lhtypes.ExecuteNoTimeout) + _, err := c.pkgMgr.Execute([]string{}, "grep", []string{set, "/proc/cpuinfo"}, lhtypes.ExecuteNoTimeout) if err != nil { logrus.Errorf("CPU instruction set %v is not supported", set) } else { diff --git a/pkg/installer/installer.go b/pkg/installer/installer.go index 6eb15a29..5096def3 100644 --- a/pkg/installer/installer.go +++ b/pkg/installer/installer.go @@ -31,7 +31,7 @@ func NewInstaller(pkgMgrType pkgmgr.PackageManagerType) (*Installer, error) { return nil, err } - kernelRelease, err := executor.Execute("uname", []string{"-r"}, lhtypes.ExecuteNoTimeout) + kernelRelease, err := executor.Execute([]string{}, "uname", []string{"-r"}, lhtypes.ExecuteNoTimeout) if err != nil { return nil, err } diff --git a/pkg/installer/spdk.go b/pkg/installer/spdk.go index ca77337e..fc0a6469 100644 --- a/pkg/installer/spdk.go +++ b/pkg/installer/spdk.go @@ -31,7 +31,7 @@ func (i *Installer) ConfigureSPDKEnv() error { // Configure SPDK environment logrus.Infof("Configuring SPDK environment") args := getArgsForConfiguringSPDKEnv() - if _, err := i.pkgMgr.Execute("bash", args, lhtypes.ExecuteNoTimeout); err != nil { + if _, err := i.pkgMgr.Execute([]string{}, "bash", args, lhtypes.ExecuteNoTimeout); err != nil { logrus.WithError(err).Errorf("Failed to configure SPDK environment") } else { logrus.Infof("Successfully configured SPDK environment") diff --git a/pkg/pkgmgr/apt.go b/pkg/pkgmgr/apt.go index 34fd6e74..1bf77a67 100644 --- a/pkg/pkgmgr/apt.go +++ b/pkg/pkgmgr/apt.go @@ -19,51 +19,51 @@ func NewAptPackageManager(executor *lhns.Executor) *AptPackageManager { // UpdatePackageList updates list of available packages func (c *AptPackageManager) UpdatePackageList() (string, error) { - return c.executor.Execute("apt", []string{"update", "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "apt", []string{"update", "-y"}, lhtypes.ExecuteNoTimeout) } // InstallPackage executes the installation command func (c *AptPackageManager) InstallPackage(name string) (string, error) { - return c.executor.Execute("apt", []string{"install", name, "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "apt", []string{"install", name, "-y"}, lhtypes.ExecuteNoTimeout) } // UninstallPackage executes the uninstallation command func (c *AptPackageManager) UninstallPackage(name string) (string, error) { - return c.executor.Execute("apt", []string{"remove", name, "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "apt", []string{"remove", name, "-y"}, lhtypes.ExecuteNoTimeout) } // Execute executes the given command with the specified environment variables, binary, and arguments. -func (c *AptPackageManager) Execute(binary string, args []string, timeout time.Duration) (string, error) { - return c.executor.Execute(binary, args, timeout) +func (c *AptPackageManager) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) { + return c.executor.Execute(envs, binary, args, timeout) } // Modprobe executes the modprobe command func (c *AptPackageManager) Modprobe(module string) (string, error) { - return c.executor.Execute("modprobe", []string{module}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "modprobe", []string{module}, lhtypes.ExecuteNoTimeout) } // CheckModLoaded checks if a module is loaded func (c *AptPackageManager) CheckModLoaded(module string) error { - _, err := c.executor.Execute("grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) + _, err := c.executor.Execute([]string{}, "grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) return err } // StartService executes the service start command func (c *AptPackageManager) StartService(name string) (string, error) { - output, err := c.executor.Execute("systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) + output, err := c.executor.Execute([]string{}, "systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) if err != nil { return output, err } - return c.executor.Execute("systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) } // GetServiceStatus executes the service status command func (c *AptPackageManager) GetServiceStatus(name string) (string, error) { - return c.executor.Execute("systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) } // CheckPackageInstalled checks if a package is installed func (c *AptPackageManager) CheckPackageInstalled(name string) (string, error) { - return c.executor.Execute("dpkg-query", []string{"-l", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "dpkg-query", []string{"-l", name}, lhtypes.ExecuteNoTimeout) } diff --git a/pkg/pkgmgr/pacman.go b/pkg/pkgmgr/pacman.go index 109f248f..648fb6b5 100644 --- a/pkg/pkgmgr/pacman.go +++ b/pkg/pkgmgr/pacman.go @@ -19,51 +19,51 @@ func NewPacmanPackageManager(executor *lhns.Executor) *PacmanPackageManager { // UpdatePackageList updates list of available packages func (c *PacmanPackageManager) UpdatePackageList() (string, error) { - return c.executor.Execute("pacman", []string{"-Syu", "--noconfirm"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "pacman", []string{"-Syu", "--noconfirm"}, lhtypes.ExecuteNoTimeout) } // InstallPackage executes the installation command func (c *PacmanPackageManager) InstallPackage(name string) (string, error) { - return c.executor.Execute("pacman", []string{"-S", "--noconfirm", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "pacman", []string{"-S", "--noconfirm", name}, lhtypes.ExecuteNoTimeout) } // UninstallPackage executes the uninstallation command func (c *PacmanPackageManager) UninstallPackage(name string) (string, error) { - return c.executor.Execute("pacman", []string{"-R", "--noconfirm", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "pacman", []string{"-R", "--noconfirm", name}, lhtypes.ExecuteNoTimeout) } // Execute executes the given command with the specified environment variables, binary, and arguments. -func (c *PacmanPackageManager) Execute(binary string, args []string, timeout time.Duration) (string, error) { - return c.executor.Execute(binary, args, timeout) +func (c *PacmanPackageManager) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) { + return c.executor.Execute(envs, binary, args, timeout) } // Modprobe executes the modprobe command func (c *PacmanPackageManager) Modprobe(module string) (string, error) { - return c.executor.Execute("modprobe", []string{module}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "modprobe", []string{module}, lhtypes.ExecuteNoTimeout) } // CheckModLoaded checks if a module is loaded func (c *PacmanPackageManager) CheckModLoaded(module string) error { - _, err := c.executor.Execute("grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) + _, err := c.executor.Execute([]string{}, "grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) return err } // StartService executes the service start command func (c *PacmanPackageManager) StartService(name string) (string, error) { - output, err := c.executor.Execute("systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) + output, err := c.executor.Execute([]string{}, "systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) if err != nil { return output, err } - return c.executor.Execute("systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) } // GetServiceStatus executes the service status command func (c *PacmanPackageManager) GetServiceStatus(name string) (string, error) { - return c.executor.Execute("systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) } // CheckPackageInstalled checks if a package is installed func (c *PacmanPackageManager) CheckPackageInstalled(name string) (string, error) { - return c.executor.Execute("pacman", []string{"-Q", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "pacman", []string{"-Q", name}, lhtypes.ExecuteNoTimeout) } diff --git a/pkg/pkgmgr/pkgmgr.go b/pkg/pkgmgr/pkgmgr.go index d2a8d4c4..d547a423 100644 --- a/pkg/pkgmgr/pkgmgr.go +++ b/pkg/pkgmgr/pkgmgr.go @@ -27,7 +27,7 @@ type PackageManager interface { StartService(name string) (string, error) GetServiceStatus(name string) (string, error) CheckPackageInstalled(name string) (string, error) - Execute(binary string, args []string, timeout time.Duration) (string, error) + Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) } func New(pkgMgrType PackageManagerType, executor *lhns.Executor) (PackageManager, error) { diff --git a/pkg/pkgmgr/yum.go b/pkg/pkgmgr/yum.go index 2757655a..7972be67 100644 --- a/pkg/pkgmgr/yum.go +++ b/pkg/pkgmgr/yum.go @@ -19,51 +19,51 @@ func NewYumPackageManager(executor *lhns.Executor) *YumPackageManager { // UpdatePackageList updates list of available packages func (c *YumPackageManager) UpdatePackageList() (string, error) { - return c.executor.Execute("yum", []string{"update", "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "yum", []string{"update", "-y"}, lhtypes.ExecuteNoTimeout) } // InstallPackage executes the installation command func (c *YumPackageManager) InstallPackage(name string) (string, error) { - return c.executor.Execute("yum", []string{"install", name, "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "yum", []string{"install", name, "-y"}, lhtypes.ExecuteNoTimeout) } // UninstallPackage executes the uninstallation command func (c *YumPackageManager) UninstallPackage(name string) (string, error) { - return c.executor.Execute("yum", []string{"remove", name, "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "yum", []string{"remove", name, "-y"}, lhtypes.ExecuteNoTimeout) } // Execute executes the given command with the specified environment variables, binary, and arguments. -func (c *YumPackageManager) Execute(binary string, args []string, timeout time.Duration) (string, error) { - return c.executor.Execute(binary, args, timeout) +func (c *YumPackageManager) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) { + return c.executor.Execute(envs, binary, args, timeout) } // Modprobe executes the modprobe command func (c *YumPackageManager) Modprobe(module string) (string, error) { - return c.executor.Execute("modprobe", []string{module}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "modprobe", []string{module}, lhtypes.ExecuteNoTimeout) } // CheckModLoaded checks if a module is loaded func (c *YumPackageManager) CheckModLoaded(module string) error { - _, err := c.executor.Execute("grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) + _, err := c.executor.Execute([]string{}, "grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) return err } // StartService executes the service start command func (c *YumPackageManager) StartService(name string) (string, error) { - output, err := c.executor.Execute("systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) + output, err := c.executor.Execute([]string{}, "systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) if err != nil { return output, err } - return c.executor.Execute("systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) } // GetServiceStatus executes the service status command func (c *YumPackageManager) GetServiceStatus(name string) (string, error) { - return c.executor.Execute("systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) } // CheckPackageInstalled checks if a package is installed func (c *YumPackageManager) CheckPackageInstalled(name string) (string, error) { - return c.executor.Execute("rpm", []string{"-q", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "rpm", []string{"-q", name}, lhtypes.ExecuteNoTimeout) } diff --git a/pkg/pkgmgr/zypper.go b/pkg/pkgmgr/zypper.go index 2d2454a3..b3c5aa77 100644 --- a/pkg/pkgmgr/zypper.go +++ b/pkg/pkgmgr/zypper.go @@ -19,51 +19,51 @@ func NewZypperPackageManager(executor *lhns.Executor) *ZypperPackageManager { // UpdatePackageList updates list of available packages func (c *ZypperPackageManager) UpdatePackageList() (string, error) { - return c.executor.Execute("zypper", []string{"update", "-y"}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "zypper", []string{"update", "-y"}, lhtypes.ExecuteNoTimeout) } // InstallPackage executes the installation command func (c *ZypperPackageManager) InstallPackage(name string) (string, error) { - return c.executor.Execute("zypper", []string{"--non-interactive", "install", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "zypper", []string{"--non-interactive", "install", name}, lhtypes.ExecuteNoTimeout) } // UninstallPackage executes the uninstallation command func (c *ZypperPackageManager) UninstallPackage(name string) (string, error) { - return c.executor.Execute("zypper", []string{"--non-interactive", "remove", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "zypper", []string{"--non-interactive", "remove", name}, lhtypes.ExecuteNoTimeout) } // Execute executes the given command with the specified environment variables, binary, and arguments. -func (c *ZypperPackageManager) Execute(binary string, args []string, timeout time.Duration) (string, error) { - return c.executor.Execute(binary, args, timeout) +func (c *ZypperPackageManager) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) { + return c.executor.Execute(envs, binary, args, timeout) } // Modprobe executes the modprobe command func (c *ZypperPackageManager) Modprobe(module string) (string, error) { - return c.executor.Execute("modprobe", []string{module}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "modprobe", []string{module}, lhtypes.ExecuteNoTimeout) } // CheckModLoaded checks if a module is loaded func (c *ZypperPackageManager) CheckModLoaded(module string) error { - _, err := c.executor.Execute("grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) + _, err := c.executor.Execute([]string{}, "grep", []string{module, "/proc/modules"}, lhtypes.ExecuteNoTimeout) return err } // StartService executes the service start command func (c *ZypperPackageManager) StartService(name string) (string, error) { - output, err := c.executor.Execute("systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) + output, err := c.executor.Execute([]string{}, "systemctl", []string{"-q", "enable", name}, lhtypes.ExecuteNoTimeout) if err != nil { return output, err } - return c.executor.Execute("systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"start", name}, lhtypes.ExecuteNoTimeout) } // GetServiceStatus executes the service status command func (c *ZypperPackageManager) GetServiceStatus(name string) (string, error) { - return c.executor.Execute("systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "systemctl", []string{"status", "--no-pager", name}, lhtypes.ExecuteNoTimeout) } // CheckPackageInstalled checks if a package is installed func (c *ZypperPackageManager) CheckPackageInstalled(name string) (string, error) { - return c.executor.Execute("rpm", []string{"-q", name}, lhtypes.ExecuteNoTimeout) + return c.executor.Execute([]string{}, "rpm", []string{"-q", name}, lhtypes.ExecuteNoTimeout) } From 7892f3e0771adf8527821ee95828a1e358aa0240 Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Fri, 15 Mar 2024 20:19:48 +0000 Subject: [PATCH 2/2] Update import after repo renaming Signed-off-by: Shuo Wu --- cmd/app/check.go | 4 ++-- cmd/app/install.go | 4 ++-- go.mod | 2 +- main.go | 4 ++-- pkg/checker/checker.go | 4 ++-- pkg/installer/installer.go | 2 +- pkg/utils/os.go | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/app/check.go b/cmd/app/check.go index b202899b..63ad12f8 100644 --- a/cmd/app/check.go +++ b/cmd/app/check.go @@ -6,8 +6,8 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli" - "github.com/longhorn/longhorn-preflight/pkg/checker" - "github.com/longhorn/longhorn-preflight/pkg/pkgmgr" + "github.com/longhorn/cli/pkg/checker" + "github.com/longhorn/cli/pkg/pkgmgr" ) func PreflightCheckCmd(pkgMgrType pkgmgr.PackageManagerType) cli.Command { diff --git a/cmd/app/install.go b/cmd/app/install.go index 2f2956a0..c66ca15a 100644 --- a/cmd/app/install.go +++ b/cmd/app/install.go @@ -6,8 +6,8 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli" - "github.com/longhorn/longhorn-preflight/pkg/installer" - "github.com/longhorn/longhorn-preflight/pkg/pkgmgr" + "github.com/longhorn/cli/pkg/installer" + "github.com/longhorn/cli/pkg/pkgmgr" ) func PreflightInstallCmd(pkgMgrType pkgmgr.PackageManagerType) cli.Command { diff --git a/go.mod b/go.mod index 366aecc7..3f071c03 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/longhorn/longhorn-preflight +module github.com/longhorn/cli go 1.21 diff --git a/main.go b/main.go index 8a8a5a1e..282fb7b8 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,8 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli" - "github.com/longhorn/longhorn-preflight/cmd/app" - "github.com/longhorn/longhorn-preflight/pkg/utils" + "github.com/longhorn/cli/cmd/app" + "github.com/longhorn/cli/pkg/utils" ) func main() { diff --git a/pkg/checker/checker.go b/pkg/checker/checker.go index 6a309231..c79a7858 100644 --- a/pkg/checker/checker.go +++ b/pkg/checker/checker.go @@ -13,8 +13,8 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" - "github.com/longhorn/longhorn-preflight/pkg/pkgmgr" - "github.com/longhorn/longhorn-preflight/pkg/utils" + "github.com/longhorn/cli/pkg/pkgmgr" + "github.com/longhorn/cli/pkg/utils" ) type Checker struct { diff --git a/pkg/installer/installer.go b/pkg/installer/installer.go index 5096def3..f8a20340 100644 --- a/pkg/installer/installer.go +++ b/pkg/installer/installer.go @@ -7,7 +7,7 @@ import ( lhns "github.com/longhorn/go-common-libs/ns" lhtypes "github.com/longhorn/go-common-libs/types" - "github.com/longhorn/longhorn-preflight/pkg/pkgmgr" + "github.com/longhorn/cli/pkg/pkgmgr" ) type Installer struct { diff --git a/pkg/utils/os.go b/pkg/utils/os.go index 8d002a26..353256d3 100644 --- a/pkg/utils/os.go +++ b/pkg/utils/os.go @@ -9,7 +9,7 @@ import ( "regexp" "strings" - "github.com/longhorn/longhorn-preflight/pkg/pkgmgr" + "github.com/longhorn/cli/pkg/pkgmgr" ) func GetPackageManagerType(platform string) (pkgmgr.PackageManagerType, error) {