Start working on applying the config to the system

This commit is contained in:
HikariKnight 2023-04-11 14:45:14 +02:00 committed by HikariKnight
parent 5ee76f5ece
commit e7c61c564e
5 changed files with 106 additions and 26 deletions

View file

@ -17,13 +17,20 @@ import (
// Preference is given to kernelstub because it is WAY easier to safely edit compared to grub2 // Preference is given to kernelstub because it is WAY easier to safely edit compared to grub2
func getBootloader(config *Config) { func getBootloader(config *Config) {
// Check what bootloader handler we are using // Check what bootloader handler we are using
// Check for grub-mkconfig // Check for grub2-mkconfig
_, err := command.Run("which", "grub2-mkconfig") _, err := command.Run("which", "grub2-mkconfig")
if err == nil { if err == nil {
// Mark bootloader as grub2 // Mark bootloader as grub2
config.Bootloader = "grub2" config.Bootloader = "grub2"
} }
// Check for grub2-mkconfig
_, err = command.Run("which", "grub-mkconfig")
if err == nil {
// Mark bootloader as grub2
config.Bootloader = "grub2"
}
// Check for grubby (used by fedora) // Check for grubby (used by fedora)
_, err = command.Run("which", "grubby") _, err = command.Run("which", "grubby")
if err == nil { if err == nil {
@ -64,7 +71,7 @@ func Set_Cmdline(gpu_IDs []string) {
} }
// Configures systemd-boot using kernelstub // Configures systemd-boot using kernelstub
func Set_KernelStub() { func Set_KernelStub() string {
// Get the config // Get the config
config := GetConfig() config := GetConfig()
@ -77,10 +84,13 @@ func Set_KernelStub() {
// Run the command // Run the command
_, err := command.Run("sudo", "kernelstub", "-a", kernel_args) _, err := command.Run("sudo", "kernelstub", "-a", kernel_args)
errorcheck.ErrorCheck(err, "Error, kernelstub command returned exit code 1") errorcheck.ErrorCheck(err, "Error, kernelstub command returned exit code 1")
// Return what we did
return fmt.Sprintf("sudo kernelstub -a \"%s\"", kernel_args)
} }
// Configures grub2 and/or systemd-boot using grubby // Configures grub2 and/or systemd-boot using grubby
func Set_Grubby() { func Set_Grubby() string {
// Get the config // Get the config
config := GetConfig() config := GetConfig()
@ -93,9 +103,12 @@ func Set_Grubby() {
// Run the command // Run the command
_, err := command.Run("sudo", "grubby", "--update-kernel=ALL", fmt.Sprintf("--args=%s", kernel_args)) _, err := command.Run("sudo", "grubby", "--update-kernel=ALL", fmt.Sprintf("--args=%s", kernel_args))
errorcheck.ErrorCheck(err, "Error, grubby command returned exit code 1") errorcheck.ErrorCheck(err, "Error, grubby command returned exit code 1")
// Return what we did
return fmt.Sprintf("sudo grubby --update-kernel=ALL --args=\"%s\"", kernel_args)
} }
func Set_Grub2() { func Configure_Grub2() {
// Get the config struct // Get the config struct
config := GetConfig() config := GetConfig()
@ -184,3 +197,49 @@ func clean_Grub2_Args(old_kernel_args []string) []string {
// Return cleaned up arguments // Return cleaned up arguments
return clean_kernel_args return clean_kernel_args
} }
// This function copies our config to /etc/default/grub and updates grub
func Set_Grub2() ([]string, error) {
// Get the config
config := GetConfig()
// Get the conf file
conffile := fmt.Sprintf("%s/grub", config.Path.DEFAULT)
// Write to logger
logger.Printf("Executing command:\nsudo cp -v \"%s\" /etc/default/grub", conffile)
// Since we should be elevated with our sudo token we will copy with cp
// (using built in functions will not work as we are running as the normal user)
output, err := command.Run("sudo", "cp", "-v", conffile, "/etc/default/grub")
errorcheck.ErrorCheck(err, fmt.Sprintf("Failed to copy %s to /etc/default/grub", conffile))
// Write output to logger
logger.Printf(strings.Join(output, "\n"))
// Set a variable for the mkconfig command
mkconfig := "grub-mkconfig"
// Check for grub-mkconfig
_, err = command.Run("which", "grub-mkconfig")
if err == nil {
// Set binary as grub-mkconfig
mkconfig = "grub-mkconfig"
} else {
mkconfig = "grub2-mkconfig"
}
// Update grub.cfg
if fileio.FileExist("/boot/grub/grub.cfg") {
output = append(output, fmt.Sprintf("sudo %s -o /boot/grub/grub.cfg", mkconfig))
mklog, err := command.Run("sudo", mkconfig, "-o", "/boot/grub/grub.cfg")
logger.Printf(strings.Join(mklog, "\n"))
errorcheck.ErrorCheck(err, "Failed to update /boot/grub/grub.cfg")
} else {
output = append(output, fmt.Sprintf("sudo %s -o /boot/grub/grub.cfg\nSee debug.log for more detailed output", mkconfig))
mklog, err := command.Run("sudo", mkconfig, "-o", "/boot/grub2/grub.cfg")
logger.Printf(strings.Join(mklog, "\n"))
errorcheck.ErrorCheck(err, "Failed to update /boot/grub/grub.cfg")
}
return output, err
}

View file

@ -23,8 +23,10 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter": case "enter":
if m.width != 0 { if m.width != 0 {
// Process the selected item, if the return value is true then exit the application // Process the selected item, if the return value is true then exit alt screen
m.processSelection() if !m.processSelection() {
return m, tea.ExitAltScreen
}
} }
case "ctrl+z", "backspace": case "ctrl+z", "backspace":
// Go backwards in the model // Go backwards in the model
@ -70,13 +72,13 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.authDialog.SetValue("") m.authDialog.SetValue("")
// Start installation and send the password to the command // Start installation and send the password to the command
m.install() m.installOutput = m.install()
// Move to the DONE dialog // Move to the DONE dialog
m.focused++ m.focused++
// Exit the alt screen as the output on the done dialog needs to be scrollable // Exit the alt screen as the output on the done dialog needs to be scrollable
return m, tea.ExitAltScreen //return m, tea.ExitAltScreen
} else { } else {
// Quit the application if we are on a different view // Quit the application if we are on a different view

View file

@ -118,6 +118,13 @@ func (m *model) processSelection() bool {
configs.Set_Mkinitcpio() configs.Set_Mkinitcpio()
} }
// Configure grub2 here as we can make the config without sudo
if config.Bootloader == "grub2" {
// Write to logger
logger.Printf("Configuring grub2 manually")
configs.Configure_Grub2()
}
// Go to the next view // Go to the next view
//m.focused++ //m.focused++
@ -144,31 +151,38 @@ func (m *model) processSelection() bool {
// This function starts the install process // This function starts the install process
// It takes 1 auth string as variable // It takes 1 auth string as variable
func (m *model) install() { func (m *model) install() []string {
// Get the config // Get the config
config := configs.GetConfig() config := configs.GetConfig()
// Make a stringlist to keep the output to show the user
var output []string
// Based on the bootloader, setup the configuration // Based on the bootloader, setup the configuration
if config.Bootloader == "kernelstub" { if config.Bootloader != "kernelstub" {
// Write to logger // Write to logger
logger.Printf("Configuring systemd-boot using kernelstub") logger.Printf("Configuring systemd-boot using kernelstub")
// Configure kernelstub // Configure kernelstub
configs.Set_KernelStub() output = append(output, configs.Set_KernelStub())
} else if config.Bootloader == "grubby" { } else if config.Bootloader == "grubby" {
// Write to logger // Write to logger
logger.Printf("Configuring bootloader using grubby") logger.Printf("Configuring bootloader using grubby")
// Configure kernelstub // Configure kernelstub
configs.Set_Grubby() output = append(output, configs.Set_Grubby())
} else if config.Bootloader == "grub2" { } else if config.Bootloader != "grub2" {
// Write to logger // Write to logger
logger.Printf("Configuring grub2 manually") logger.Printf("Configuring grub2 manually")
configs.Set_Grub2() grub_output, _ := configs.Set_Grub2()
output = append(output, grub_output...)
} else { } else {
kernel_args := fileio.ReadFile(config.Path.CMDLINE) kernel_args := fileio.ReadFile(config.Path.CMDLINE)
logger.Printf("Unsupported bootloader, please add the below line to your bootloaders kernel arguments\n%s", kernel_args) logger.Printf("Unsupported bootloader, please add the below line to your bootloaders kernel arguments\n%s", kernel_args)
} }
return output
} }

View file

@ -12,7 +12,7 @@ import (
func (m model) View() string { func (m model) View() string {
if m.width != 0 { if m.width != 0 {
title := "" title := ""
view := "Empty View :(" view := ""
switch m.focused { switch m.focused {
case INTRO: case INTRO:
title = dialogStyle.Render( title = dialogStyle.Render(
@ -137,6 +137,10 @@ func (m model) View() string {
) )
view = m.authDialog.View() view = m.authDialog.View()
case DONE:
title = titleStyle.Render("Applying configurations!")
view = dialogStyle.Render(strings.Join(m.installOutput, "\n"))
} }
//return listStyle.SetString(fmt.Sprintf("%s\n\n", title)).Render(m.lists[m.focused].View()) //return listStyle.SetString(fmt.Sprintf("%s\n\n", title)).Render(m.lists[m.focused].View())
return lipgloss.JoinVertical(lipgloss.Left, fmt.Sprintf("%s\n%s\n", title, view)) return lipgloss.JoinVertical(lipgloss.Left, fmt.Sprintf("%s\n%s\n", title, view))

View file

@ -25,17 +25,18 @@ func (i item) FilterValue() string { return i.title }
// Main Model // Main Model
type model struct { type model struct {
fetched []bool fetched []bool
lists []list.Model lists []list.Model
gpu_group string gpu_group string
gpu_IDs []string gpu_IDs []string
vbios_path string vbios_path string
focused status focused status
offsetx []int offsetx []int
offsety []int offsety []int
width int width int
height int height int
authDialog textinput.Model authDialog textinput.Model
installOutput []string
} }
// Consts used to navigate the main model // Consts used to navigate the main model