Skip to content

Commit

Permalink
fix: repair gnoweb flag parsing (#1476)
Browse files Browse the repository at this point in the history
Before:
![CleanShot 2023-12-21 at 00 31
36@2x](https://github.com/gnolang/gno/assets/94029/9587d366-adac-42a1-9527-0b14c97ff89f)

After:
![CleanShot 2023-12-21 at 00 59
08@2x](https://github.com/gnolang/gno/assets/94029/8560bf0c-a145-47a5-b456-80f38479f390)

Continues #1446

---

Flag parsing was broken because `fs.Parse` was expecting to use
`os.Args[1:]` instead of `os.Args`. I fixed it, simplified the
implementation and added a regression test.

---------

Signed-off-by: moul <[email protected]>
  • Loading branch information
moul authored Dec 21, 2023
1 parent 968f89e commit d10aa9b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
30 changes: 17 additions & 13 deletions gno.land/cmd/gnoweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"net/http"
"os"
"time"
Expand All @@ -13,29 +14,31 @@ import (
// "github.com/gnolang/gno/tm2/pkg/sdk" // for baseapp (info, status)
)

func parseConfigFlags(fs *flag.FlagSet, args []string) (gnoweb.Config, error) {
cfg := gnoweb.NewDefaultConfig()
func main() {
err := runMain(os.Args[1:])
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}

func runMain(args []string) error {
var (
fs = flag.NewFlagSet("gnoweb", flag.ContinueOnError)
cfg = gnoweb.NewDefaultConfig()
bindAddress string
)
fs.StringVar(&cfg.RemoteAddr, "remote", cfg.RemoteAddr, "remote gnoland node address")
fs.StringVar(&cfg.CaptchaSite, "captcha-site", cfg.CaptchaSite, "recaptcha site key (if empty, captcha are disabled)")
fs.StringVar(&cfg.FaucetURL, "faucet-url", cfg.FaucetURL, "faucet server URL")
fs.StringVar(&cfg.ViewsDir, "views-dir", cfg.ViewsDir, "views directory location") // XXX: replace with goembed
fs.StringVar(&cfg.HelpChainID, "help-chainid", cfg.HelpChainID, "help page's chainid")
fs.StringVar(&cfg.HelpRemote, "help-remote", cfg.HelpRemote, "help page's remote addr")
fs.BoolVar(&cfg.WithAnalytics, "with-analytics", cfg.WithAnalytics, "enable privacy-first analytics")

return cfg, fs.Parse(args)
}

func main() {
fs := flag.NewFlagSet("gnoweb", flag.PanicOnError)

var bindAddress string
fs.StringVar(&bindAddress, "bind", "127.0.0.1:8888", "server listening address")

cfg, err := parseConfigFlags(fs, os.Args)
if err != nil {
panic("unable to parse flags: " + err.Error())
if err := fs.Parse(args); err != nil {
return err
}

logger := log.NewTMLogger(os.Stdout)
Expand All @@ -51,4 +54,5 @@ func main() {
if err := server.ListenAndServe(); err != nil {
logger.Error("HTTP server stopped", " error:", err)
}
return nil
}
14 changes: 14 additions & 0 deletions gno.land/cmd/gnoweb/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"errors"
"flag"
"testing"
)

func TestFlagHelp(t *testing.T) {
err := runMain([]string{"-h"})
if !errors.Is(err, flag.ErrHelp) {
t.Errorf("should display usage")
}
}

0 comments on commit d10aa9b

Please sign in to comment.