fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildefeat: add web UI (#617)d672bf4

ignore
1.0 KiB39 linesraw
1package webui
2
3import (
4	"fmt"
5	"os/exec"
6	"runtime"
7	"strings"
8)
9
10// browserCommand builds the command used to open url. When cmd is non-empty it
11// is treated as a launcher command line (the URL is appended as the final
12// argument); otherwise the platform default handler for goos is used.
13func browserCommand(goos, url, cmd string) (name string, args []string, err error) {
14	if cmd != "" {
15		fields := strings.Fields(cmd)
16		if len(fields) == 0 {
17			return "", nil, fmt.Errorf("empty browser command")
18		}
19		return fields[0], append(fields[1:], url), nil
20	}
21
22	switch goos {
23	case "darwin":
24		return "open", []string{url}, nil
25	case "windows":
26		return "rundll32", []string{"url.dll,FileProtocolHandler", url}, nil
27	default:
28		return "xdg-open", []string{url}, nil
29	}
30}
31
32// openBrowser opens url in a browser using the configured or default launcher.
33func openBrowser(url, cmd string) error {
34	name, args, err := browserCommand(runtime.GOOS, url, cmd)
35	if err != nil {
36		return err
37	}
38	return exec.Command(name, args...).Start()
39}