diff --git a/README.md b/README.md index 0c905be..19122eb 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,29 @@ Runs healthchecks against local docker containers ## Usage +### add command + +Add a healthcheck to an existing `app.json` file, specified by the `--app-json` flag. If the file does not exist, an empty `app.json` file will be assumed. + +```shell +# creates a default startup uptime healthcheck +# docker-container-healthchecker add $PROCESS_TYPE +docker-container-healthchecker add web +``` + +By default, the output is written to `stdout`, though it can be written to the file specified via the `--in-place` flag. + +```shell +docker-container-healthchecker add web --in-place +``` + +The `add` command supports adding a listening check, which optionally supports a `--port` flag (default: `5000`): + +```shell +# listening check +docker-container-healthchecker add web --listening-check --port 3000 +``` + ### check command After creating an app.json file, execute the healthchecker like so: diff --git a/commands/add.go b/commands/add.go index a25adf1..c608b43 100644 --- a/commands/add.go +++ b/commands/add.go @@ -19,7 +19,9 @@ type AddCommand struct { ifEmpty bool inPlace bool listeningCheck bool + name string prettyPrint bool + port int uptime int warn bool } @@ -70,9 +72,11 @@ func (c *AddCommand) FlagSet() *flag.FlagSet { f.BoolVar(&c.ifEmpty, "if-empty", false, "only add if there are no healthchecks for the process") f.BoolVar(&c.inPlace, "in-place", false, "modify any app.json file in place") f.StringVar(&c.appJSONFile, "app-json", "app.json", "full path to app.json file to update") + f.StringVar(&c.name, "name", "", "name to use for added check") f.StringVar(&c.checkType, "type", "startup", "check to interpret") f.BoolVar(&c.listeningCheck, "listening-check", false, "use a listening instead of uptime check") f.BoolVar(&c.warn, "warn-only", false, "only warn on error") + f.IntVar(&c.port, "port", 5000, "container port to use") f.IntVar(&c.uptime, "uptime", 1, "amount of time the container should be running for at minimum") return f } @@ -84,6 +88,8 @@ func (c *AddCommand) AutocompleteFlags() complete.Flags { "--app-json": complete.PredictAnything, "--if-empty": complete.PredictNothing, "--in-place": complete.PredictNothing, + "--name": complete.PredictAnything, + "--port": complete.PredictAnything, "--pretty": complete.PredictNothing, "--type": complete.PredictSet("liveness", "readiness", "startup"), "--uptime": complete.PredictAnything, @@ -113,8 +119,8 @@ func (c *AddCommand) Run(args []string) int { if _, err := os.Stat(c.appJSONFile); err == nil { contents, err = os.ReadFile(c.appJSONFile) if err != nil { - c.Ui.Error(err.Error()) - return 1 + c.Ui.Warn(err.Error()) + contents = []byte("{}") } } } @@ -142,8 +148,13 @@ func (c *AddCommand) Run(args []string) int { Warn: c.warn, } + if c.name != "" { + healthcheck.Name = c.name + } + if c.listeningCheck { healthcheck.Listening = true + healthcheck.Port = c.port } else { healthcheck.Uptime = c.uptime } diff --git a/test.bats b/test.bats index d7727f9..879524f 100644 --- a/test.bats +++ b/test.bats @@ -36,6 +36,18 @@ teardown() { echo "status: $status" assert_success assert_output '{"healthchecks":{"web":[{"name":"default","type":"startup","uptime":1}]}}' + + run "$BIN_NAME" add --name custom-check + echo "output: $output" + echo "status: $status" + assert_success + assert_output '{"healthchecks":{"web":[{"name":"custom-check","type":"startup","uptime":1}]}}' + + run "$BIN_NAME" add --app-json non-existing.json + echo "output: $output" + echo "status: $status" + assert_success + assert_output '{"healthchecks":{"web":[{"name":"default","type":"startup","uptime":1}]}}' } @test "[add] default in-place" { @@ -107,7 +119,7 @@ teardown() { echo "output: $output" echo "status: $status" assert_success - assert_output '{"healthchecks":{"web":[{"listening":true,"name":"default","type":"startup"}]}}' + assert_output '{"healthchecks":{"web":[{"listening":true,"name":"default","port":5000,"type":"startup"}]}}' } @test "[add] default warn-only" {