diff --git a/internal/configs/configs.go b/internal/configs/configs.go index f1bec83..6f4772d 100644 --- a/internal/configs/configs.go +++ b/internal/configs/configs.go @@ -4,7 +4,9 @@ import ( "errors" "fmt" "os" + "path/filepath" "regexp" + "strings" "github.com/klauspost/cpuid/v2" @@ -300,12 +302,32 @@ func CopyToSystem(isRoot bool, conffile, sysfile string) { // ExecAndLogSudo will write to the logger, so just print here fmt.Printf("Copying: %s to %s\n", conffile, sysfile) + if isRoot { + logger.Printf("Copying %s to %s\n", conffile, sysfile) + fmt.Printf("Copying %s to %s\n", conffile, sysfile) + fDat, err := os.ReadFile(conffile) + common.ErrorCheck(err, fmt.Sprintf("Failed to read %s", conffile)) + err = os.WriteFile(sysfile, fDat, 0644) + common.ErrorCheck(err, fmt.Sprintf("Failed to write %s", sysfile)) + return + } + + if !filepath.IsAbs(conffile) { + conffile, _ = filepath.Abs(conffile) + } + + conffile = strings.ReplaceAll(conffile, " ", "\\ ") + cmd := fmt.Sprintf("cp -v %s %s", conffile, sysfile) + + err := command.ExecAndLogSudo(isRoot, false, cmd) + + errMsg := "" + if err != nil { + errMsg = err.Error() + } + // [command.ExecAndLogSudo] will log the command's output - common.ErrorCheck(command.ExecAndLogSudo(isRoot, false, - fmt.Sprintf("cp -v \"%s\" %s", conffile, sysfile), - ), // if error, log and exit - fmt.Sprintf("Failed to copy %s to %s", conffile, sysfile), - ) + common.ErrorCheck(err, fmt.Sprintf("Failed to copy %s to %s:\n%s", conffile, sysfile, errMsg)) // --------------------------------------------------------------------------------- // note that if we failed the error check, the following will not appear in the log! diff --git a/internal/configs/configs_test.go b/internal/configs/configs_test.go new file mode 100644 index 0000000..8cc6290 --- /dev/null +++ b/internal/configs/configs_test.go @@ -0,0 +1,33 @@ +package configs + +import ( + "os" + "path/filepath" + "testing" +) + +func TestCopyToSystem(t *testing.T) { + if err := os.Mkdir("testdir", 0755); err != nil { + t.Fatal(err) + } + tFilePath := filepath.Join("testdir", "testfile") + if err := os.WriteFile(tFilePath, []byte("test"), 0644); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := os.RemoveAll("testdir"); err != nil { + t.Fatal(err) + } + }) + isRoot := os.Getuid() == 0 + switch isRoot { + case true: + t.Run("TestCopyToSystem_AsRoot", func(t *testing.T) { + CopyToSystem(true, tFilePath, "/etc/testfile") + }) + default: + t.Run("TestCopyToSystem_AsUser", func(t *testing.T) { + CopyToSystem(false, tFilePath, "/etc/testfile") + }) + } +} diff --git a/pkg/command/command.go b/pkg/command/command.go index 4f1b553..65702d5 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -133,8 +133,14 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error { fmt.Printf("Executing (elevated): %s\nSee debug.log for detailed output\n", cmd) } + wd, err := os.Getwd() + if err != nil { + return err + } + cs := strings.Fields(cmd) r := exec.Command(cs[0], cs[1:]...) + r.Dir = wd cmdCombinedOut, err := r.CombinedOutput() outStr := string(cmdCombinedOut) @@ -148,5 +154,9 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error { fmt.Printf("%s\n", outStr) } + if err != nil { + err = fmt.Errorf("failed to execute %s: %w\n%s", cmd, err, outStr) + } + return err }