Skip to content

Commit 92cf6ca

Browse files
authored
explorer: allow hiding stages without an effect (+misc) (#1760)
* explorer: allow hiding stages without an effect (+misc) Also changes the web page to not feature an editable policy -- you have that in your IDE when you get to that page already. --------- Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
1 parent 728ae28 commit 92cf6ca

2 files changed

Lines changed: 37 additions & 20 deletions

File tree

internal/web/assets/main.tpl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@
77
</head>
88
<body>
99
<main class="crowded">
10+
<details class="info">
11+
<summary>Policy</summary>
12+
<pre><code>{{ .Code }}</code></pre>
13+
</details>
1014
<form>
1115
<div class="f-row">
12-
<textarea name="code"
13-
style="height: 300px"
14-
hx-post="/?tmpl=output"
15-
hx-target="#output"
16-
hx-trigger="keyup changed delay:200ms"
17-
class="flex-grow:1 monospace"
18-
>{{ .Code }}</textarea>
16+
<input hx-get="?tmpl=output"
17+
hx-target="#output"
18+
hx-trigger="change"
19+
type="checkbox"
20+
name="hide_identical"
21+
id="hide_identical">
22+
<label for="hide_identical">Hide stages without effect on code</label>
1923
</div>
2024
</form>
2125
<section id="output">
2226
{{ block "output" . }}
2327
{{ range .Result }}
24-
<details class={{ .Class }} {{ if .Show }}open{{ end }}>
28+
<details class="{{ .Class }}" {{ if .Show }}open{{ end }}>
2529
<summary>{{ .Stage }}</summary>
2630
<pre><code>{{ .Output }}</code></pre>
2731
</details>

internal/web/server.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package web
22

33
import (
4+
"cmp"
45
"context"
56
"embed"
67
"fmt"
@@ -9,6 +10,7 @@ import (
910
"net/http"
1011
"net/http/pprof"
1112
"os"
13+
"slices"
1214
"strings"
1315

1416
"github.com/arl/statsviz"
@@ -73,7 +75,7 @@ func (s *Server) SetBaseURL(baseURL string) {
7375
s.baseURL = baseURL
7476
}
7577

76-
func (s *Server) Start(_ context.Context) {
78+
func (s *Server) Start(context.Context) {
7779
mux := http.NewServeMux()
7880
if err := statsviz.Register(mux); err != nil {
7981
s.log.Message("failed to register statsviz handler: %v", err)
@@ -89,41 +91,46 @@ func (s *Server) Start(_ context.Context) {
8991
return
9092
}
9193

92-
var enableStrict, enableAnnotationProcessing, enablePrint bool
94+
var (
95+
enableStrict, enableAnnotationProcessing, enablePrint bool // TODO(sr): expose
96+
hideIdentical bool
97+
tmpl string
98+
)
9399

94100
if err := r.ParseForm(); err == nil {
95101
enableStrict = r.Form.Get("strict") == "on"
96102
enableAnnotationProcessing = r.Form.Get("annotations") == "on"
97103
enablePrint = r.Form.Get("print") == "on"
104+
hideIdentical = r.Form.Get("hide_identical") == "on"
105+
tmpl = cmp.Or(r.Form.Get("tmpl"), mainTemplate)
98106
}
99107

100108
cs := explorer.CompilerStages(path, policy, enableStrict, enableAnnotationProcessing, enablePrint)
101-
st := state{Code: policy, Result: make([]stringResult, len(cs)+1)}
109+
n := len(cs)
110+
st := state{Code: policy, Result: make([]stringResult, n+1)}
102111

103112
for i := range cs {
104-
st.Result[i].Stage = cs[i].Stage
113+
show := i == 0 || st.Result[i-1].Output != cs[i].Result.String()
114+
115+
st.Result[i] = stringResult{Stage: cs[i].Stage, Show: show}
116+
105117
if cs[i].Error != "" {
106118
st.Result[i].Output = cs[i].Error
107119
st.Result[i].Class = "bad"
108120
} else {
109121
st.Result[i].Output = cs[i].Result.String()
110122
}
111123

112-
st.Result[i].Show = i == 0 || st.Result[i-1].Output != st.Result[i].Output
113124
if st.Result[i].Class == "" {
114-
if st.Result[i].Show {
125+
if show {
115126
st.Result[i].Class = "ok"
116127
} else {
117128
st.Result[i].Class = "plain"
118129
}
119130
}
120131
}
121132

122-
n := len(cs)
123-
124-
st.Result[n].Stage = "Plan"
125-
st.Result[n].Show = true
126-
133+
st.Result[n] = stringResult{Stage: "Plan", Show: true}
127134
if p, err := explorer.Plan(r.Context(), path, policy, enablePrint); err != nil {
128135
st.Result[n].Class = "bad"
129136
st.Result[n].Output = err.Error()
@@ -132,7 +139,13 @@ func (s *Server) Start(_ context.Context) {
132139
st.Result[n].Output = p
133140
}
134141

135-
if err := tpl.ExecuteTemplate(w, mainTemplate, st); err != nil {
142+
if hideIdentical {
143+
st.Result = slices.CompactFunc(st.Result, func(a, b stringResult) bool {
144+
return a.Output == b.Output
145+
})
146+
}
147+
148+
if err := tpl.ExecuteTemplate(w, tmpl, st); err != nil {
136149
http.Error(w, err.Error(), http.StatusInternalServerError)
137150
}
138151
})

0 commit comments

Comments
 (0)