Skip to content

Commit

Permalink
Move ServerMain to scli package to keep go.mod/go.sum minimal for tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Feb 19, 2023
1 parent ca1ffba commit 12f4cd2
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 104 deletions.
24 changes: 1 addition & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# cli
Reduce boiler plate needed on each new Golang main functions (Command Line Interface) for both tool and servers
Reduce boiler plate needed on each new Golang main functions (Command Line Interface) for both tools and servers (use [fortio.org/scli ServerMain()](https://github.com/fortio/scli#scli) for server)

It abstracts the repetitive parts of a `main()` command line tool, flag parsing, usage, etc...

Expand Down Expand Up @@ -52,28 +52,6 @@ $ sampleTool a b
15:42:17 Hello world, version dev, args [a b]
```

## Server Example

Server example [sampleServer](sampleServer/main.go)


```bash
% go run . -config-dir ./config -config-port 8888 a b
15:45:20 I updater.go:47> Configmap flag value watching on ./config
15:45:20 I updater.go:156> updating loglevel to "verbose\n"
15:45:20 I logger.go:183> Log level is now 1 Verbose (was 2 Info)
15:45:20 I updater.go:97> Now watching . and config
15:45:20 I updater.go:162> Background thread watching config now running
15:45:20 Fortio 1.50.1 config server listening on tcp [::]:8888
15:45:20 Starting sampleServer dev go1.19.6 arm64 darwin
# When visiting the UI
15:46:20 ListFlags: GET / HTTP/1.1 [::1]:52406 () "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
```

With the flags ui on http://localhost:8888

<img width="716" alt="flags UI" src="https://user-images.githubusercontent.com/3664595/219904547-368a024e-1d6a-4301-a7a9-8882e37f5a90.png">

## Additional builtins

### buildinfo
Expand Down
36 changes: 0 additions & 36 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import (
"path/filepath"
"strings"

"fortio.org/dflag/configmap"
"fortio.org/dflag/dynloglevel"
"fortio.org/dflag/endpoint"
"fortio.org/fortio/fhttp"
"fortio.org/log"
"fortio.org/version"
)
Expand Down Expand Up @@ -155,38 +151,6 @@ func ErrArgCount(prefix string, expected, actual int) bool {
return ErrUsage("%s %d %s expected, got %d", prefix, expected, Plural(expected, "argument"), actual)
}

// ServerMain returns true if a config port server has been started
// caller needs to select {} after its own code is ready.
// Will have called ExitFunction (ie exited) if there are usage errors
// (wrong number of arguments, bad flags etc...).
// It sets up (optional) config-dir to watch and listen on config-port for dynamic flag
// changes and UI/api.
func ServerMain() bool {
configDir := flag.String("config-dir", "", "Config `directory` to watch for dynamic flag changes")
configPort := flag.String("config-port", "", "Config `port` to open for dynamic flag UI/api")
dynloglevel.LoggerFlagSetup("loglevel")
ServerMode = true
Main() // will call ExitFunction() if there are usage errors
if *configDir != "" {
if _, err := configmap.Setup(flag.CommandLine, *configDir); err != nil {
log.Critf("Unable to watch config/flag changes in %v: %v", *configDir, err)
}
}
hasStartedServer := false
if *configPort != "" {
mux, addr := fhttp.HTTPServer("config", *configPort) // err already logged
if addr != nil {
hasStartedServer = true
setURL := "/set"
ep := endpoint.NewFlagsEndpoint(flag.CommandLine, setURL)
mux.HandleFunc("/", ep.ListFlags)
mux.HandleFunc(setURL, ep.SetFlag)
}
}
log.Printf("Starting %s %s", ProgramName, LongVersion)
return hasStartedServer
}

// Plural adds an "s" to the noun if i is not 1.
func Plural(i int, noun string) string {
return PluralExt(i, noun, "s")
Expand Down
11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ module fortio.org/cli
go 1.18

require (
fortio.org/dflag v1.4.1
fortio.org/fortio v1.50.1
fortio.org/log v1.2.2
fortio.org/version v1.0.2
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
)
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
fortio.org/assert v1.1.3 h1:zXm8xiNiKvq2xG/YQ3sONAg3287XUuklKIDdjyD9pyg=
fortio.org/dflag v1.4.1 h1:WDhlHMh3yrQFrvspyN5YEyr8WATdKM2dUJlTxsjCDtI=
fortio.org/dflag v1.4.1/go.mod h1:pTEF7UEj6sHP9rj9gZG2GyhAGrrPJE4c6zOO7zB2yyI=
fortio.org/fortio v1.50.1 h1:5FSttAHQsyAsi3dzxDmSByfzDYByrWY/yw53bqOg+Kc=
fortio.org/fortio v1.50.1/go.mod h1:TpAo6nixVbTxeIfE8F2c63D3ffouMTa+KDr9eFAYxys=
fortio.org/log v1.2.2 h1:vs42JjNwiqbMbacittZjJE9+oi72Za6aekML9gKmILg=
fortio.org/log v1.2.2/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
fortio.org/version v1.0.2 h1:8NwxdX58aoeKx7T5xAPO0xlUu1Hpk42nRz5s6e6eKZ0=
fortio.org/version v1.0.2/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
1 change: 0 additions & 1 deletion sampleServer/config/loglevel

This file was deleted.

15 changes: 0 additions & 15 deletions sampleServer/main.go

This file was deleted.

0 comments on commit 12f4cd2

Please sign in to comment.