summaryrefslogtreecommitdiff
path: root/cmd/lectio-web
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-07-23 23:45:07 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-07-23 23:45:07 +0200
commitc1b954ad517cef00bf480d5229b253a8c6524be7 (patch)
tree376a281de7604782722d5780657aff319c32a1d6 /cmd/lectio-web
parent369c995292b0ae830e077c5845f0b09fbe3508a4 (diff)
downloadlectio-c1b954ad517cef00bf480d5229b253a8c6524be7.tar.gz
lectio-c1b954ad517cef00bf480d5229b253a8c6524be7.zip
cli-ui,web: real flag parsing for lectio-ui/lectio-web (accept useful flags, reject unknown, -h/-v)
Diffstat (limited to 'cmd/lectio-web')
-rw-r--r--cmd/lectio-web/main.go122
-rw-r--r--cmd/lectio-web/main_test.go87
2 files changed, 205 insertions, 4 deletions
diff --git a/cmd/lectio-web/main.go b/cmd/lectio-web/main.go
index 44845b8..2e7fa5a 100644
--- a/cmd/lectio-web/main.go
+++ b/cmd/lectio-web/main.go
@@ -1,21 +1,135 @@
+// Command lectio-web serves lectio's browser reading UI.
package main
import (
+ "flag"
"fmt"
+ "io"
"os"
"github.com/lukaszkasprzak/lectio/internal/config"
"github.com/lukaszkasprzak/lectio/internal/web"
)
+const helpText = `lectio-web — browser daily liturgy reader (Polish + 4 versions)
+
+Usage:
+ lectio-web [flags]
+
+Flags:
+ -p, --port N port (overrides web_port; 0 = auto)
+ -o, --offline cache/sigla only, no network
+ -l, --lectionary WHICH new|trad (trad -> traditional)
+ -g, --lang LANG traditional lectionary language: pl|en
+ -v, --version print the version and exit
+ -h, --help this help
+
+Flags override config. Exit codes: 0 ok, 1 runtime error, 2 usage error.
+`
+
func main() {
+ os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
+}
+
+// run is lectio-web's testable entry point: parse args, validate, load
+// config, apply overrides, and launch the server. Returns a process exit
+// code.
+func run(args []string, stdout, stderr io.Writer) int {
+ if wantsHelp(args) {
+ fmt.Fprint(stdout, helpText)
+ return 0
+ }
+ if wantsVersion(args) {
+ fmt.Fprintln(stdout, "lectio-web "+config.Version)
+ return 0
+ }
+
cfg, err := config.Load()
if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
+ fmt.Fprintln(stderr, "lectio-web:", err)
+ return 1
}
+
+ var offline bool
+ var port int
+ var lectionary, lang string
+
+ fs := flag.NewFlagSet("lectio-web", flag.ContinueOnError)
+ fs.SetOutput(stderr)
+ fs.Usage = func() { fmt.Fprint(stderr, helpText) }
+
+ fs.IntVar(&port, "p", cfg.WebPort, "port (overrides web_port; 0 = auto)")
+ fs.IntVar(&port, "port", cfg.WebPort, "port (overrides web_port; 0 = auto)")
+ fs.BoolVar(&offline, "o", cfg.Offline, "cache/sigla only, no network")
+ fs.BoolVar(&offline, "offline", cfg.Offline, "cache/sigla only, no network")
+ fs.StringVar(&lectionary, "l", "", "new|trad")
+ fs.StringVar(&lectionary, "lectionary", "", "new|trad")
+ fs.StringVar(&lang, "g", "", "pl|en")
+ fs.StringVar(&lang, "lang", "", "pl|en")
+
+ if err := fs.Parse(args); err != nil {
+ return 2
+ }
+ if fs.NArg() > 0 {
+ fmt.Fprintf(stderr, "lectio-web: unexpected argument %q; see 'lectio-web -h'\n", fs.Arg(0))
+ return 2
+ }
+
+ lectionary, err = normalizeLectionary(lectionary)
+ if err != nil {
+ fmt.Fprintln(stderr, "lectio-web:", err)
+ return 2
+ }
+ if lang != "" && lang != "pl" && lang != "en" {
+ fmt.Fprintf(stderr, "lectio-web: invalid --lang %q (want pl|en)\n", lang)
+ return 2
+ }
+
+ cfg.WebPort = port
+ cfg.Offline = offline
+ if lectionary != "" {
+ cfg.Lectionary = lectionary
+ }
+ if lang != "" {
+ cfg.TraditionalLang = lang
+ }
+
if err := web.Run(cfg); err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
+ fmt.Fprintln(stderr, err)
+ return 1
+ }
+ return 0
+}
+
+// wantsHelp reports whether -h/--help appears anywhere in args.
+func wantsHelp(args []string) bool {
+ for _, a := range args {
+ if a == "-h" || a == "--help" {
+ return true
+ }
+ }
+ return false
+}
+
+// wantsVersion reports whether -v/--version appears anywhere in args.
+func wantsVersion(args []string) bool {
+ for _, a := range args {
+ if a == "-v" || a == "--version" {
+ return true
+ }
+ }
+ return false
+}
+
+// normalizeLectionary maps -l/--lectionary's accepted spellings via
+// config.NormalizeLectionary; "" (flag not given) passes through unchanged.
+func normalizeLectionary(lectionary string) (string, error) {
+ if lectionary == "" {
+ return "", nil
+ }
+ v, ok := config.NormalizeLectionary(lectionary)
+ if !ok {
+ return "", fmt.Errorf("invalid --lectionary %q (want new|trad)", lectionary)
}
+ return v, nil
}
diff --git a/cmd/lectio-web/main_test.go b/cmd/lectio-web/main_test.go
new file mode 100644
index 0000000..766e0d9
--- /dev/null
+++ b/cmd/lectio-web/main_test.go
@@ -0,0 +1,87 @@
+package main
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+)
+
+func TestHelpFlag(t *testing.T) {
+ var out, errb bytes.Buffer
+ if code := run([]string{"-h"}, &out, &errb); code != 0 {
+ t.Errorf("-h code=%d (stderr=%q)", code, errb.String())
+ }
+ if !strings.Contains(out.String(), "lectio-web") {
+ t.Errorf("-h output missing lectio-web: %q", out.String())
+ }
+}
+
+func TestHelpLongFlag(t *testing.T) {
+ var out, errb bytes.Buffer
+ if code := run([]string{"--help"}, &out, &errb); code != 0 {
+ t.Errorf("--help code=%d (stderr=%q)", code, errb.String())
+ }
+ if !strings.Contains(out.String(), "lectio-web") {
+ t.Errorf("--help output missing lectio-web: %q", out.String())
+ }
+}
+
+func TestVersionFlag(t *testing.T) {
+ var out, errb bytes.Buffer
+ if code := run([]string{"-v"}, &out, &errb); code != 0 {
+ t.Errorf("-v code=%d (stderr=%q)", code, errb.String())
+ }
+ if !strings.Contains(out.String(), "lectio-web") {
+ t.Errorf("-v output missing lectio-web: %q", out.String())
+ }
+}
+
+func TestVersionLongFlag(t *testing.T) {
+ var out, errb bytes.Buffer
+ if code := run([]string{"--version"}, &out, &errb); code != 0 {
+ t.Errorf("--version code=%d (stderr=%q)", code, errb.String())
+ }
+ if !strings.Contains(out.String(), "lectio-web") {
+ t.Errorf("--version output missing lectio-web: %q", out.String())
+ }
+}
+
+func TestUnknownFlag(t *testing.T) {
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ var out, errb bytes.Buffer
+ if code := run([]string{"-x"}, &out, &errb); code != 2 {
+ t.Errorf("-x code=%d want 2 (stderr=%q)", code, errb.String())
+ }
+}
+
+func TestUnknownLongFlag(t *testing.T) {
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ var out, errb bytes.Buffer
+ if code := run([]string{"--bogus"}, &out, &errb); code != 2 {
+ t.Errorf("--bogus code=%d want 2 (stderr=%q)", code, errb.String())
+ }
+}
+
+func TestUnexpectedPositional(t *testing.T) {
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ var out, errb bytes.Buffer
+ if code := run([]string{"bogus"}, &out, &errb); code != 2 {
+ t.Errorf("unexpected positional code=%d want 2 (stderr=%q)", code, errb.String())
+ }
+}
+
+func TestLectionaryBogus(t *testing.T) {
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ var out, errb bytes.Buffer
+ if code := run([]string{"-l", "bogus"}, &out, &errb); code != 2 {
+ t.Errorf("bogus lectionary code=%d want 2 (stderr=%q)", code, errb.String())
+ }
+}
+
+func TestLangBogus(t *testing.T) {
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ var out, errb bytes.Buffer
+ if code := run([]string{"-g", "xx"}, &out, &errb); code != 2 {
+ t.Errorf("bogus lang code=%d want 2 (stderr=%q)", code, errb.String())
+ }
+}