Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(rexctl): add home screen and command reference page

Hampus69237af9347e
diff --git a/fluxer_rexctl/cmd/docs.go b/fluxer_rexctl/cmd/docs.go
new file mode 100644
index 000000000..b6be3b83a
--- /dev/null
+++ b/fluxer_rexctl/cmd/docs.go
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/pflag"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+const docsHeader = `---
+# SPDX-License-Identifier: AGPL-3.0-or-later
+title: rexctl commands
+description: Every rexctl command and flag, made from rexctl's own help.
+---
+
+This page is made from the help that rexctl prints, and ` + "`rexctl <command> --help`" + ` shows the same text in the terminal.
+`
+
+func newDocsCmd(app *App) *cobra.Command {
+ var out string
+ c := &cobra.Command{
+ Use: "docs",
+ Short: "Write the command reference page",
+ Hidden: true,
+ Args: noArgs,
+ RunE: func(c *cobra.Command, _ []string) error {
+ page := renderDocs(c.Root())
+ if out == "" {
+ _, err := app.UI.Stdout().Write(page)
+ return err
+ }
+ if err := os.WriteFile(out, page, 0o644); err != nil {
+ return ui.Wrap(err, fmt.Sprintf("Couldn't write %s.", out), "Check that the folder exists and that you can write to it.")
+ }
+ return nil
+ },
+ }
+ c.Flags().StringVar(&out, "out", "", "file to write")
+ return c
+}
+
+func renderDocs(root *cobra.Command) []byte {
+ var b bytes.Buffer
+ b.WriteString(docsHeader)
+ b.WriteString("\n## Flags for every command\n\n")
+ writeFlags(&b, root.PersistentFlags())
+ for _, c := range docsCommands(root) {
+ writeCommand(&b, c, 2)
+ }
+ return b.Bytes()
+}
+
+func docsCommands(c *cobra.Command) []*cobra.Command {
+ var out []*cobra.Command
+ for _, sub := range c.Commands() {
+ if sub.Hidden || sub.Deprecated != "" || sub.Name() == "help" || sub.Name() == "completion" {
+ continue
+ }
+ out = append(out, sub)
+ }
+ return out
+}
+
+func writeCommand(b *bytes.Buffer, c *cobra.Command, level int) {
+ fmt.Fprintf(b, "\n%s %s\n\n", strings.Repeat("#", level), c.CommandPath())
+ b.WriteString(docsProse(docsDescription(c)))
+ b.WriteString("\n")
+ if c.Runnable() && c.Annotations[helpOnly] == "" {
+ fmt.Fprintf(b, "\n```sh\n%s\n```\n", c.UseLine())
+ if c.Example != "" {
+ fmt.Fprintf(b, "\n```sh\n%s\n```\n", strings.TrimSpace(c.Example))
+ }
+ if c.HasAvailableLocalFlags() {
+ b.WriteString("\n")
+ writeFlags(b, c.LocalFlags())
+ }
+ }
+ next := level + 1
+ if next > 3 {
+ next = 3
+ }
+ for _, sub := range docsCommands(c) {
+ writeCommand(b, sub, next)
+ }
+}
+
+func docsDescription(c *cobra.Command) string {
+ text := strings.TrimSpace(c.Long)
+ if text == "" {
+ text = strings.TrimSpace(c.Short)
+ }
+ return sentence(text)
+}
+
+func writeFlags(b *bytes.Buffer, fs *pflag.FlagSet) {
+ first := true
+ fs.VisitAll(func(f *pflag.Flag) {
+ if f.Hidden || f.Deprecated != "" || f.Name == "help" {
+ return
+ }
+ if !first {
+ b.WriteString("\n")
+ }
+ first = false
+ name, usage := pflag.UnquoteUsage(f)
+ head := "`--" + f.Name
+ if name != "" && f.Value.Type() != "bool" {
+ head += " " + name
+ }
+ head += "`"
+ if f.Shorthand != "" {
+ head = "`-" + f.Shorthand + "`, " + head
+ }
+ fmt.Fprintf(b, "- %s\n\n %s", head, docsProse(sentence(usage)))
+ if d := flagDefault(f); d != "" {
+ fmt.Fprintf(b, " The default is `%s`.", d)
+ }
+ b.WriteString("\n")
+ })
+}
+
+func flagDefault(f *pflag.Flag) string {
+ switch f.DefValue {
+ case "", "false", "[]", "0", "0s":
+ return ""
+ }
+ return f.DefValue
+}
+
+func sentence(s string) string {
+ s = strings.TrimSpace(s)
+ if s == "" {
+ return s
+ }
+ s = strings.ToUpper(s[:1]) + s[1:]
+ if !strings.HasSuffix(s, ".") && !strings.HasSuffix(s, "?") && !strings.HasSuffix(s, "!") {
+ s += "."
+ }
+ return s
+}
+
+func docsProse(s string) string {
+ var b strings.Builder
+ code := false
+ for _, r := range s {
+ if r == '`' {
+ code = !code
+ }
+ if !code && strings.ContainsRune("<{}", r) {
+ b.WriteByte('\\')
+ }
+ b.WriteRune(r)
+ }
+ return b.String()
+}
diff --git a/fluxer_rexctl/cmd/home.go b/fluxer_rexctl/cmd/home.go
new file mode 100644
index 000000000..3bb702935
--- /dev/null
+++ b/fluxer_rexctl/cmd/home.go
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "strings"
+
+ "github.com/charmbracelet/x/ansi"
+ "github.com/spf13/cobra"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+var (
+ homeInstalled = []string{"status", "upgrade", "backup", "users", "invites", "doctor"}
+ homeLegacy = []string{"install", "status"}
+ homeEmpty = []string{"install", "restore"}
+)
+
+func runHome(_ context.Context, app *App, c *cobra.Command) error {
+ if !app.UI.Interactive() {
+ return c.Help()
+ }
+ dir, err := app.Dir()
+ if err != nil {
+ return err
+ }
+ inst, err := instance.Find(dir)
+ switch {
+ case err == nil:
+ homeGreet(app, fmt.Sprintf("Your instance is at %s.", inst.Origin()))
+ homeCommands(app, c, homeInstalled)
+ case errors.Is(err, instance.ErrLegacy):
+ homeGreet(app, fmt.Sprintf("There's a Fluxer instance in %s.", dir))
+ app.UI.Info("Run %s to take it over.", app.takeOverCommand())
+ app.UI.Info("")
+ homeCommands(app, c, homeLegacy)
+ default:
+ homeGreet(app, ui.WelcomeLine)
+ homeCommands(app, c, homeEmpty)
+ }
+ app.UI.Info("")
+ app.UI.Info("Run rexctl --help to see every command.")
+ return nil
+}
+
+func homeGreet(app *App, line string) {
+ if app.UI.ShowsRex() {
+ app.UI.Rex(ui.Greeting, line)
+ return
+ }
+ app.UI.Info("%s", line)
+ app.UI.Info("")
+}
+
+func homeCommands(app *App, root *cobra.Command, names []string) {
+ type row struct{ use, short string }
+ var rows []row
+ width := 0
+ for _, name := range names {
+ sub, _, err := root.Find([]string{name})
+ if err != nil || sub == root {
+ continue
+ }
+ use := "rexctl " + sub.Name()
+ rows = append(rows, row{use, sub.Short})
+ width = max(width, ansi.StringWidth(use))
+ }
+ accent := app.UI.Theme().Accent
+ for _, r := range rows {
+ pad := strings.Repeat(" ", width-ansi.StringWidth(r.use))
+ app.UI.Info(" %s%s %s", accent.Render(r.use), pad, r.short)
+ }
+}