Skip to content

Commit

Permalink
Take -a and -p from environment as default
Browse files Browse the repository at this point in the history
Take the values for the flags `-a` and `-p` from environment variables
as the default, still allowing them to be overridden on the command
line.

Sometimes using `servedir`, you may always want to listen on all
interfaces but the default invocation of `servedir` in a Makefile recipe
does not specify `-a`. You can now set `SERVEDIR_ALL_INTERFACES=true` in
the environment and these pre-canned `servedir` invocations can work
with your defaults.
  • Loading branch information
camh- committed Jan 1, 2024
1 parent e9682ff commit 4ea0c40
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# --- Global -------------------------------------------------------------------
O = out
COVERAGE = 59
COVERAGE = 66
VERSION ?= $(shell git describe --tags --dirty --always)

all: build test check-coverage lint ## build, test, check coverage and lint
Expand Down
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//
// Simple HTTP server, serving files from given directory.
//
// -a listen on all interfaces not just localhost
// -a listen on all interfaces not just localhost (env: SERVEDIR_ALL_INTERFACES)
// -p int
// port number (default: os chosen free port)
// port number (default: os chosen free port) (env: SERVEDIR_PORT)
// <dir> defaults to current directory if not specified
package main

Expand All @@ -20,6 +20,7 @@ import (
"net"
"net/http"
"os"
"strconv"
"strings"
)

Expand Down Expand Up @@ -67,9 +68,23 @@ type config struct {
}

func parseFlags(args ...string) config {
var portDefault int = 0
if s, ok := os.LookupEnv("SERVEDIR_PORT"); ok {
if v, err := strconv.ParseInt(s, 0, strconv.IntSize); err == nil {
portDefault = int(v)
}
}

var allInterfacesDefault bool = false
if s, ok := os.LookupEnv("SERVEDIR_ALL_INTERFACES"); ok {
if v, err := strconv.ParseBool(s); err == nil {
allInterfacesDefault = v
}
}

fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
port := fs.Int("p", 0, "port number (default: os chosen free port)")
allInterfaces := fs.Bool("a", false, "listen on all interfaces not just localhost")
port := fs.Int("p", portDefault, "port number (default: os chosen free port)")
allInterfaces := fs.Bool("a", allInterfacesDefault, "listen on all interfaces not just localhost")
fs.Usage = func() { usage(fs) }
fs.Parse(args) //nolint:errcheck // ExitOnError means this does not return an error
return config{
Expand Down
11 changes: 11 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ func TestParseFlags(t *testing.T) {
got := parseFlags()
require.Equal(t, want, got)
}

func TestParseFlagsFromEnvironment(t *testing.T) {
want := config{
listenAddr: ":1",
dir: ".",
}
t.Setenv("SERVEDIR_ALL_INTERFACES", "true")
t.Setenv("SERVEDIR_PORT", "1")
got := parseFlags()
require.Equal(t, want, got)
}

0 comments on commit 4ea0c40

Please sign in to comment.