diff options
| author | Rose Hogenson <rhogenson@google.com> | 2026-07-23 16:23:52 -0700 |
|---|---|---|
| committer | Rose Hogenson <rhogenson@google.com> | 2026-07-23 16:23:52 -0700 |
| commit | a6563b31cd342334fe7a8b22b410d359adf3bfbb (patch) | |
| tree | e723a457c3dbaa12b594b885a82413389f8b2420 | |
| parent | 8f036c1780ebc2d07d3043f8d805e1dde0725669 (diff) | |
| download | roseh.moe-a6563b31cd342334fe7a8b22b410d359adf3bfbb.tar.zst | |
Add a command index
| -rw-r--r-- | roseh.moe.go | 47 | ||||
| -rw-r--r-- | templates/cmd-index.html.template | 11 |
2 files changed, 52 insertions, 6 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index 407d112..84f3abf 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -824,7 +824,7 @@ func cmd(w http.ResponseWriter, r *http.Request) { return } if r.FormValue("go-get") == "1" { - fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/cmd/%s %s">`, r.PathValue("cmd"), source) + fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/cmd/%s %s">`, r.PathValue("cmd"), source.url) } else { http.Redirect(w, r, "https://pkg.go.dev/roseh.moe/cmd/"+r.PathValue("cmd"), http.StatusFound) } @@ -869,6 +869,48 @@ func pkgIndex(w http.ResponseWriter, r *http.Request) { } } +var ( + //go:embed templates/cmd-index.html.template + cmdIndexString string + cmdIndexTemplate = template.Must(outlinedTemplate(cmdIndexString)) +) + +type cmdIndexTemplateCommand struct { + Name string + Description string + Multi bool +} + +type cmdIndexTemplateArgs struct { + Commands []cmdIndexTemplateCommand +} + +func cmdIndex(w http.ResponseWriter, r *http.Request) { + serviceConfig, err := loadConfig() + if err != nil { + log.Printf("Warning: failed to load config: %s", err) + http.Error(w, "Failed to load config", http.StatusInternalServerError) + return + } + commands := make([]string, 0, len(serviceConfig.commandRedirects)) + for cmd := range serviceConfig.commandRedirects { + commands = append(commands, cmd) + } + slices.Sort(commands) + templateCommands := make([]cmdIndexTemplateCommand, len(commands)) + for i, name := range commands { + cmd := serviceConfig.commandRedirects[name] + templateCommands[i] = cmdIndexTemplateCommand{ + Name: name, + Description: cmd.description, + Multi: cmd.multi, + } + } + if err := cmdIndexTemplate.Execute(w, cmdIndexTemplateArgs{Commands: templateCommands}); err != nil { + log.Printf("Warning: pkgIndex: %s", err) + } +} + func pkg(w http.ResponseWriter, r *http.Request) { serviceConfig, err := loadConfig() if err != nil { @@ -882,7 +924,7 @@ func pkg(w http.ResponseWriter, r *http.Request) { return } if r.FormValue("go-get") == "1" { - fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/pkg/%s %s">`, r.PathValue("pkg"), source) + fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/pkg/%s %s">`, r.PathValue("pkg"), source.url) } else { http.Redirect(w, r, "https://pkg.go.dev/roseh.moe/pkg/"+r.PathValue("pkg"), http.StatusFound) } @@ -926,6 +968,7 @@ func main() { mux.HandleFunc("POST /login", login) mux.HandleFunc("GET /notepad", notepad) mux.HandleFunc("POST /notepad", autosave) + mux.HandleFunc("GET /cmd", cmdIndex) mux.HandleFunc("GET /cmd/{cmd}", cmd) mux.HandleFunc("GET /pkg", pkgIndex) mux.HandleFunc("GET /pkg/{pkg}", pkg) diff --git a/templates/cmd-index.html.template b/templates/cmd-index.html.template index 7628b86..fb8c626 100644 --- a/templates/cmd-index.html.template +++ b/templates/cmd-index.html.template @@ -1,12 +1,15 @@ {{define "head"}} - <title>Packages</title> + <title>Commands</title> {{end}} <section> - <p>Package index</p> + <p>Command index</p> <ul> - {{range .Packages}} - <li><p><a href="https://pkg.go.dev/roseh.moe/pkg/{{.Name}}">{{.Name}}</a> - {{.Description}}</p></li> + {{range .Commands}} + <li> + <p><a href="https://pkg.go.dev/roseh.moe/cmd/{{.Name}}">{{.Name}}</a> - {{.Description}}</p> + <p><code>go install roseh.moe/cmd/{{.Name}}{{if .Multi}}/...{{end}}@latest</code></p> + </li> {{end}} </ul> </section> |
