-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.go
More file actions
114 lines (94 loc) · 2.7 KB
/
Copy pathserver.go
File metadata and controls
114 lines (94 loc) · 2.7 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
)
//go:embed ui
var ui embed.FS
func runServeCmd(args []string) error {
flagSet := flag.NewFlagSet("serve", flag.ExitOnError)
addr := flagSet.String("addr", ":8080", "Address to serve on")
outputDir := flagSet.String("output-dir", "", "Directory containing the generated JSON files")
devUIServe := flagSet.Bool("dev-ui", false, "Serve the UI from filesystem instead of embedded")
servePath := flagSet.String("serve-path", "/", "Path to serve the UI on")
if err := flagSet.Parse(args); err != nil {
return err
}
if *outputDir == "" {
return fmt.Errorf("no -output-dir specified")
}
return runServe(*addr, *outputDir, *devUIServe, *servePath)
}
func runServe(addr, outputDir string, devUIServe bool, servePath string) error {
uiFS, _ := fs.Sub(ui, "ui")
if devUIServe {
uiFS = os.DirFS("ui")
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.FS(uiFS)))
mux.Handle("/plans/", http.StripPrefix("/plans/", http.FileServer(http.FS(os.DirFS(outputDir)))))
// otherwise StripPrefix will redirect /foo to foo, which will cause redirect loops
servePath = strings.TrimRight(servePath, "/")
log.Printf("Serving UI on %s%s", addr, servePath)
return http.ListenAndServe(addr, http.StripPrefix(servePath, mux))
}
func runVisualizeDriftsCmd(args []string) error {
fs := flag.NewFlagSet("visualize-drifts", flag.ExitOnError)
plansDir := fs.String("plans-dir", "", "Directory containing plan files (required)")
addr := fs.String("addr", ":8080", "Address to serve on")
if err := fs.Parse(args); err != nil {
return err
}
if *plansDir == "" {
return fmt.Errorf("no -plans-dir specified")
}
tmpDir, err := os.MkdirTemp("", "drift-ui-")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
const driftID = "drift"
if err := runDrift(*plansDir, tmpDir, driftID); err != nil {
return err
}
go func() {
if err := runServe(*addr, tmpDir, false, "/"); err != nil {
log.Fatal(err)
}
}()
time.Sleep(100 * time.Millisecond)
url := fmt.Sprintf("http://localhost%s/#%s", *addr, driftID)
if err := openBrowser(url); err != nil {
log.Printf("failed to open browser: %v", err)
log.Printf("open %s manually", url)
}
<-sigCh
return nil
}
func openBrowser(url string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "linux":
cmd = exec.Command("xdg-open", url)
case "darwin":
cmd = exec.Command("open", url)
case "windows":
cmd = exec.Command("cmd", "/c", "start", url)
default:
return fmt.Errorf("unsupported platform")
}
return cmd.Start()
}