Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output Status of Running Applications #224

Closed
wants to merge 5 commits into from
Closed
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
66 changes: 66 additions & 0 deletions cmd/puma-dev/command.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"text/tabwriter"

"github.com/puma/puma-dev/homedir"
"github.com/vektra/errors"
Expand All @@ -15,11 +19,73 @@ func command() error {
switch flag.Arg(0) {
case "link":
return link()
case "status":
return status()
default:
return fmt.Errorf("Unknown command: %s\n", flag.Arg(0))
}
}

// App is a running application.
type App struct {
Status string `json:"status"`
Scheme string `json:"scheme"`
Address string `json:"address"`
}

func status() error {
var port string

if *fHTTPPort != 9280 {
port = fmt.Sprintf(":%d", *fHTTPPort)
}

client := &http.Client{}
url := fmt.Sprintf("http://localhost%s/status", port)
req, err := http.NewRequest("GET", url, nil)
req.Host = "puma-dev"
w := tabwriter.NewWriter(os.Stdout, 20, 4, 1, ' ', 0)

if err != nil {
return err
}

res, err := client.Do(req)

if err != nil {
fmt.Printf("Unable to lookup puma-dev status. Is puma-dev listening on port %d?\n", *fHTTPPort)
return nil
}

body, err := ioutil.ReadAll(res.Body)

if err != nil {
return err
}

var apps map[string]*App
err = json.Unmarshal(body, &apps)

if err != nil {
return err
}

if len(apps) > 0 {

fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "NAME", "STATUS", "ADDRESS", "SCHEME")

for name, app := range apps {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", name, app.Status, app.Address, app.Scheme)
}

w.Flush()
} else {
fmt.Println("No apps are currently running.")
}

return nil
}

func link() error {
fs := flag.NewFlagSet("link", flag.ExitOnError)
name := fs.String("n", "", "name to link app as")
Expand Down
4 changes: 4 additions & 0 deletions cmd/puma-dev/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ func TestCommand_link_reassignExistingApp(t *testing.T) {

RemoveAppSymlinkOrFail(t, appAlias)
}

func TestCommand_status(t *testing.T) {
assert.Nil(t, status())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this a bit stronger by utilizing the WithStdOutCaptured helper in the devtest package and assert that this simple case prints the correct error message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and that it reads the port information correctly.

Copy link
Author

@tubbo tubbo Feb 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I am admittedly quite new to writing Go tests...so I wasn't sure if this was enough coverage over what it does, but I couldn't think of something that would be simple enough to include here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course! admittedly, so am i. but, i've been slowly working on adding enough sugar in here to make testing this stuff a bit easier.

take a look here for an example of how to use that helper i mentioned. i'm happy to answer questions / address feedback.

StubFlagArgs([]string{"link", "-n", appAlias, appDir1})
actual1 := WithStdoutCaptured(func() {
if err := command(); err != nil {
assert.Fail(t, err.Error())
}
})
assert.Equal(t, fmt.Sprintf("+ App '%s' created, linked to '%s'\n", appAlias, appDir1), actual1)

}
2 changes: 1 addition & 1 deletion cmd/puma-dev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func init() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()

fmt.Fprintf(os.Stderr, "\nAvailable subcommands: link\n")
fmt.Fprintf(os.Stderr, "\nAvailable subcommands: link, status\n")
tubbo marked this conversation as resolved.
Show resolved Hide resolved
}
}