From ef3760879bff4866c47d9307ae93090a6bd7527b Mon Sep 17 00:00:00 2001 From: HikariKnight <2557889+HikariKnight@users.noreply.github.com> Date: Sat, 8 Apr 2023 23:40:23 +0200 Subject: [PATCH] cleanup of the code and add more data to config --- go.mod | 1 + go.sum | 3 +++ internal/configs/config_inits.go | 28 ++++++++++++++++++++++ internal/configs/configs.go | 40 +++++++++++++++++++++++++------- internal/ui_main_functions.go | 7 ++++-- pkg/command/command.go | 30 ++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 internal/configs/config_inits.go create mode 100644 pkg/command/command.go diff --git a/go.mod b/go.mod index a37bab6..14e9eec 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/charmbracelet/bubbles v0.15.0 github.com/charmbracelet/bubbletea v0.23.2 github.com/charmbracelet/lipgloss v0.7.1 + github.com/klauspost/cpuid/v2 v2.2.4 ) require ( diff --git a/go.sum b/go.sum index f82d8f1..43838bd 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZ github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= +github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= +github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= @@ -62,6 +64,7 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= diff --git a/internal/configs/config_inits.go b/internal/configs/config_inits.go new file mode 100644 index 0000000..0e2b6c5 --- /dev/null +++ b/internal/configs/config_inits.go @@ -0,0 +1,28 @@ +package configs + +import ( + "github.com/HikariKnight/quickpassthrough/pkg/command" +) + +func getBootloader(config *Config) { + // Check what bootloader handler we are using + // Check for grub-mkconfig + _, err := command.Run("which", "grub-mkconfig") + if err == nil { + // Mark bootloader as grub2 + config.bootloader = "grub2" + } + + // Check for grubby (used by fedora) + _, err = command.Run("which", "grubby") + if err == nil { + // Mark it as unknown as i do not support it yet + config.bootloader = "unknown" + } + + // Check for kernelstub (used by pop os) + _, err = command.Run("which", "kernelstub") + if err == nil { + config.bootloader = "kernelstub" + } +} diff --git a/internal/configs/configs.go b/internal/configs/configs.go index 287fe2c..8ab3cbb 100644 --- a/internal/configs/configs.go +++ b/internal/configs/configs.go @@ -7,6 +7,7 @@ import ( "regexp" "github.com/HikariKnight/ls-iommu/pkg/errorcheck" + "github.com/klauspost/cpuid/v2" ) type Path struct { @@ -19,6 +20,12 @@ type Path struct { MKINITCPIO string } +type Config struct { + bootloader string + cpuvendor string + path *Path +} + func GetConfigPaths() *Path { Paths := &Path{ MODPROBE: "config/etc/modprobe.d", @@ -33,14 +40,30 @@ func GetConfigPaths() *Path { return Paths } +func GetConfig() *Config { + config := &Config{} + config.path = GetConfigPaths() + + // Set default value for bootloader + config.bootloader = "unknown" + + // Detect the bootloader we are using + getBootloader(config) + + // Detect the cpu vendor + config.cpuvendor = cpuid.CPU.VendorID.String() + + return config +} + func InitConfigs() { - config := GetConfigPaths() + config := GetConfig() dirs := []string{ - config.MODPROBE, - config.INITRAMFS, - config.DEFAULT, - config.DRACUT, + config.path.MODPROBE, + config.path.INITRAMFS, + config.path.DEFAULT, + config.path.DRACUT, } // Remove old config @@ -63,9 +86,10 @@ func InitConfigs() { } files := []string{ - config.ETCMODULES, - config.MKINITCPIO, - fmt.Sprintf("%s/modules", config.INITRAMFS), + config.path.ETCMODULES, + config.path.MKINITCPIO, + fmt.Sprintf("%s/modules", config.path.INITRAMFS), + fmt.Sprintf("%s/grub", config.path.DEFAULT), } for _, conffile := range files { diff --git a/internal/ui_main_functions.go b/internal/ui_main_functions.go index b851f3b..df63532 100644 --- a/internal/ui_main_functions.go +++ b/internal/ui_main_functions.go @@ -11,8 +11,6 @@ import ( func (m *model) processSelection() { switch m.focused { case GPUS: - configs.InitConfigs() - // Gets the selected item selectedItem := m.lists[m.focused].SelectedItem() @@ -58,12 +56,17 @@ func (m *model) processSelection() { m.focused++ case VBIOS: + // This is just an OK Dialog m.focused++ case VIDEO: + // This is a YESNO Dialog m.focused++ case INTRO: + // This is an OK Dialog + // Create the config folder and the files related to this system + configs.InitConfigs() m.focused++ case DONE: diff --git a/pkg/command/command.go b/pkg/command/command.go new file mode 100644 index 0000000..26978c6 --- /dev/null +++ b/pkg/command/command.go @@ -0,0 +1,30 @@ +package command + +import ( + "bytes" + "io" + "os/exec" +) + +func Run(binary string, args ...string) ([]string, error) { + var stdout, stderr bytes.Buffer + + // Configure the ls-iommu c--ommand + cmd := exec.Command(binary, args...) + cmd.Stderr = &stderr + cmd.Stdout = &stdout + + // Execute the command + err := cmd.Run() + + // Read the output + output, _ := io.ReadAll(&stdout) + outerr, _ := io.ReadAll(&stderr) + + outputs := []string{} + outputs = append(outputs, string(output)) + outputs = append(outputs, string(outerr)) + + // Return our list of items + return outputs, err +}