1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package main
import (
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
)
// =============================================================================
// Utilities
// =============================================================================
// termLink returns an OSC 8 hyperlink sequence: the text is displayed but
// the URL is what activates on click in supporting terminals (e.g. kitty,
// iTerm2, GNOME Terminal, WezTerm). Falls back gracefully — non-supporting
// terminals just print the raw text with the escape sequences stripped or
// ignored.
func termLink(url, text string) string {
return fmt.Sprintf("\033]8;;%s\033\\%s\033]8;;\033\\", url, text)
}
// freePort asks the OS for a free TCP port by briefly binding on :0.
func freePort() (string, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", fmt.Errorf("finding free port: %w", err)
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()
return strconv.Itoa(port), nil
}
func runCmd(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stderr // progress to stderr, not stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
return out.Close()
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func dirExists(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
|