mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
refactor: move browser open into shared modules
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed splash_screen.html
|
||||
var SplashScreenTemplate string
|
||||
+33
-3
@@ -1,8 +1,38 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"bytes"
|
||||
"errors"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
//go:embed splash_screen.html
|
||||
var SplashScreenTemplate string
|
||||
type CommandResponse struct {
|
||||
Stdout string `json:"stdout"`
|
||||
Stderr string `json:"stderr"`
|
||||
ExitCode int `json:"exitCode"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
func RunShellCommand(cmd *exec.Cmd) CommandResponse {
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
|
||||
commandOutput := CommandResponse{
|
||||
Stdout: stdout.String(),
|
||||
Stderr: stderr.String(),
|
||||
ExitCode: cmd.ProcessState.ExitCode(),
|
||||
}
|
||||
if err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
commandOutput.ExitCode = exitErr.ExitCode()
|
||||
} else {
|
||||
commandOutput.Stderr = err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return commandOutput
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
|
||||
func OpenBrowserWindow(url string, fullscreen bool, temp bool) error {
|
||||
bins := []string{"chromium", "chromium-browser"}
|
||||
|
||||
args := []string{fmt.Sprintf("--app=%s", url), "--autoplay-policy=no-user-gesture-required"}
|
||||
if fullscreen {
|
||||
args = append(args, "--start-fullscreen")
|
||||
}
|
||||
if temp {
|
||||
tempDirPath, err := os.MkdirTemp("", "plg-mudics-browser-")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
arg := fmt.Sprintf("--user-data-dir=%s", tempDirPath)
|
||||
args = append(args, arg)
|
||||
}
|
||||
|
||||
for _, bin := range bins {
|
||||
cmd := exec.Command(bin, args...)
|
||||
fmt.Println(cmd)
|
||||
commandOutput := RunShellCommand(cmd)
|
||||
if commandOutput.ExitCode == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return errors.New("chromium not found in PATH")
|
||||
}
|
||||
Reference in New Issue
Block a user