Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/stackwhere/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (wc *webCmd) runE(cmd *cobra.Command, args []string) error {
_ = listener.Close()
}()

if _, err := fmt.Fprintf(cmd.OutOrStdout(), "Serving stackwhere web UI on %s\n", startupURL(*wc.flagAddr)); err != nil {
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "Serving stackwhere web UI on %s\n", startupURL(*wc.flagAddr, listener.Addr().String())); err != nil {
return err
}

Expand All @@ -86,11 +86,14 @@ func (wc *webCmd) runE(cmd *cobra.Command, args []string) error {
return nil
}

func startupURL(addr string) string {
func startupURL(addr, boundAddr string) string {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return "http://" + addr
}
if _, boundPort, err := net.SplitHostPort(boundAddr); err == nil {
port = boundPort
}

if host == "" {
host = "127.0.0.1"
Expand Down
45 changes: 28 additions & 17 deletions cmd/stackwhere/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,47 @@ import (

func TestStartupURL(t *testing.T) {
tests := []struct {
name string
addr string
want string
name string
addr string
boundAddr string
want string
}{
{
name: "host omitted",
addr: ":8080",
want: "http://127.0.0.1:8080",
name: "host omitted",
addr: ":8080",
boundAddr: "[::]:8080",
want: "http://127.0.0.1:8080",
},
{
name: "explicit ipv4 host",
addr: "0.0.0.0:8080",
want: "http://0.0.0.0:8080",
name: "explicit ipv4 host",
addr: "0.0.0.0:8080",
boundAddr: "0.0.0.0:8080",
want: "http://0.0.0.0:8080",
},
{
name: "explicit hostname",
addr: "localhost:9090",
want: "http://localhost:9090",
name: "explicit hostname",
addr: "localhost:9090",
boundAddr: "127.0.0.1:9090",
want: "http://localhost:9090",
},
{
name: "explicit ipv6 host",
addr: "[::1]:8080",
want: "http://[::1]:8080",
name: "explicit ipv6 host",
addr: "[::1]:8080",
boundAddr: "[::1]:8080",
want: "http://[::1]:8080",
},
{
name: "ephemeral port",
addr: "127.0.0.1:0",
boundAddr: "127.0.0.1:36833",
want: "http://127.0.0.1:36833",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := startupURL(tt.addr); got != tt.want {
t.Fatalf("startupURL(%q) = %q, want %q", tt.addr, got, tt.want)
if got := startupURL(tt.addr, tt.boundAddr); got != tt.want {
t.Fatalf("startupURL(%q, %q) = %q, want %q", tt.addr, tt.boundAddr, got, tt.want)
}
})
}
Expand Down