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

Add ability to specify a custom name and port for added healthchecks #123

Merged
merged 1 commit into from
Oct 15, 2023
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 13 additions & 2 deletions commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type AddCommand struct {
ifEmpty bool
inPlace bool
listeningCheck bool
name string
prettyPrint bool
port int
uptime int
warn bool
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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("{}")
}
}
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 13 additions & 1 deletion test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down