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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ By default, Valar uses the default valarconfig file in `$HOME/.valar/config`. If
VALAR_TOKEN=... valar build push --wait
```

### Machine-readable output

With `--json`, or `VALAR_OUTPUT=json`, every command prints its result as one JSON document on stdout. Lists are arrays of objects whose fields match the API's JSON. Logs (`service logs`, `build logs`, `build watch`, `batch logs`) are printed as one JSON object per line, progress goes to stderr, and an error is printed on stderr as:

```json
{"error": {"kind": "notFound", "message": "no builds available"}}
```

```bash
valar service list --json | jq -r '.[].name'
```

### Exit codes

| Code | Kind | Meaning |
|---|---|---|
| 0 | | Success |
| 1 | `failed` | The operation ran and failed, such as a build or deployment |
| 2 | `invalid` | Bad arguments, flags or configuration |
| 3 | `notFound` | Not found |
| 4 | `notAllowed` | Missing or rejected credentials or permissions |
| 5 | `conflict` | Conflict with the current state |
| 6 | `unavailable` | The server could not be reached or answered with an error |

`valar build status` exits with 1 for any build that is not done.

### Configuration

#### Initialize configuration
Expand All @@ -46,6 +72,14 @@ valar config init [--token token] [--project project] [--url url] [--name name]
valar config view
```

#### Print the JSON Schema of `.valar.yml`

```bash
valar config schema
```

The schema is also checked in at [`schema/valar.yml.json`](schema/valar.yml.json), for editors. The CLI refuses a `.valar.yml` with keys the schema does not know, naming the key and its line.

#### Add an API endpoint

```bash
Expand Down
68 changes: 50 additions & 18 deletions cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -43,7 +44,7 @@ latest successful build is used.`,
}
env, err := parseBatchEnv(batchRunEnv)
if err != nil {
return err
return withKind(kindInvalid, err)
}
exec, err := client.TriggerBatchExecution(cfg.Project(), cfg.Service(), &api.BatchExecutionRequest{
Build: batchRunBuild,
Expand All @@ -53,25 +54,37 @@ latest successful build is used.`,
if err != nil {
return err
}
fmt.Printf("Execution %s %s\n", exec.ID, colorize(exec.Status))
if !outputJSON {
fmt.Printf("Execution %s %s\n", exec.ID, colorize(exec.Status))
}
if !batchRunFollow && !batchRunWait {
return nil
return result(exec, func(io.Writer) error { return nil })
}
if batchRunFollow {
if err := followBatchOutput(client, cfg, exec.ID); err != nil {
// In JSON mode the output is progress, so it goes to stderr.
var out io.Writer = os.Stdout
if outputJSON {
out = os.Stderr
}
if err := followBatchOutput(client, cfg, exec.ID, out); err != nil {
fmt.Fprintf(os.Stderr, "could not read execution output: %v\n", err)
}
}
final, err := awaitBatchCompletion(client, cfg, exec.ID)
if err != nil {
return err
}
fmt.Printf("Execution %s %s (exit %d)\n", final.ID, colorize(final.Status), final.ExitCode)
if err := result(final, func(w io.Writer) error {
_, err := fmt.Fprintf(w, "Execution %s %s (exit %d)\n", final.ID, colorize(final.Status), final.ExitCode)
return err
}); err != nil {
return err
}
if final.Status != "succeeded" {
if final.Error != "" {
fmt.Fprintln(os.Stderr, final.Error)
return newError(kindFailed, "%s", final.Error)
}
os.Exit(1)
return &cliError{kind: kindFailed, err: fmt.Errorf("execution %s %s", final.ID, final.Status), silent: true}
}
return nil
}),
Expand All @@ -89,7 +102,10 @@ latest successful build is used.`,
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
if outputJSON {
return writeJSON(stdout, execs)
}
tw := tabwriter.NewWriter(stdout, 0, 0, 1, ' ', 0)
fmt.Fprintln(tw, "ID\tSTATUS\tEXIT\tBUILD\tCREATED\tNOTE")
for _, exec := range execs {
fmt.Fprintln(tw, strings.Join([]string{
Expand Down Expand Up @@ -118,7 +134,10 @@ latest successful build is used.`,
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
if outputJSON {
return writeJSON(stdout, exec)
}
tw := tabwriter.NewWriter(stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(tw, "ID:\t%s\n", exec.ID)
fmt.Fprintf(tw, "Status:\t%s\n", colorize(exec.Status))
fmt.Fprintf(tw, "Exit code:\t%d\n", exec.ExitCode)
Expand Down Expand Up @@ -158,11 +177,16 @@ latest successful build is used.`,
if err != nil {
return err
}
w, flush := logOutput()
if batchLogsFollow {
return followBatchOutput(client, cfg, args[0])
err = followBatchOutput(client, cfg, args[0], w)
} else {
_, err = client.ReadBatchExecutionLogs(cfg.Project(), cfg.Service(), args[0], 0, w)
}
_, err = client.ReadBatchExecutionLogs(cfg.Project(), cfg.Service(), args[0], 0, os.Stdout)
return err
if err != nil {
return err
}
return flush()
}),
}

Expand All @@ -178,8 +202,13 @@ latest successful build is used.`,
if err := client.CancelBatchExecution(cfg.Project(), cfg.Service(), args[0]); err != nil {
return err
}
fmt.Printf("Execution %s cancelled\n", args[0])
return nil
return result(struct {
ID string `json:"id"`
Cancelled bool `json:"cancelled"`
}{args[0], true}, func(w io.Writer) error {
_, err := fmt.Fprintf(w, "Execution %s cancelled\n", args[0])
return err
})
}),
}

Expand Down Expand Up @@ -223,7 +252,10 @@ latest successful build is used.`,
return err
}
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
if outputJSON {
return writeJSON(stdout, current)
}
tw := tabwriter.NewWriter(stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(tw, "Max parallelism:\t%s\n", zeroAsDefault(current.MaxParallelism, "1"))
fmt.Fprintf(tw, "Timeout:\t%s\n", zeroAsDefault(current.TimeoutSeconds, "3600 (platform default)"))
fmt.Fprintf(tw, "Weight:\t%s\n", zeroAsDefault(current.Weight, "1 (platform default)"))
Expand Down Expand Up @@ -327,7 +359,7 @@ func awaitBatchCompletion(client *api.Client, cfg config.ServiceConfig, id strin
// server never closes a followed batch log, so a follower cannot tell when the
// execution is done and simply hangs -- which it did. Polling puts the
// termination condition where the answer actually lives: the execution's status.
func followBatchOutput(client *api.Client, cfg config.ServiceConfig, id string) error {
func followBatchOutput(client *api.Client, cfg config.ServiceConfig, id string, w io.Writer) error {
offset := 0
for {
exec, err := client.InspectBatchExecution(cfg.Project(), cfg.Service(), id)
Expand All @@ -337,7 +369,7 @@ func followBatchOutput(client *api.Client, cfg config.ServiceConfig, id string)
// A queued execution has no log yet, and asking for one is an error
// rather than an empty read.
if exec.Status != "pending" {
n, err := client.ReadBatchExecutionLogs(cfg.Project(), cfg.Service(), id, offset, os.Stdout)
n, err := client.ReadBatchExecutionLogs(cfg.Project(), cfg.Service(), id, offset, w)
offset += n
if err != nil && !isTerminalBatchStatus(exec.Status) {
// Transient while the container is still coming up.
Expand All @@ -350,7 +382,7 @@ func followBatchOutput(client *api.Client, cfg config.ServiceConfig, id string)
if isTerminalBatchStatus(exec.Status) {
// The status was read before the log, so anything written between
// the two reads is still outstanding.
n, err := client.ReadBatchExecutionLogs(cfg.Project(), cfg.Service(), id, offset, os.Stdout)
n, err := client.ReadBatchExecutionLogs(cfg.Project(), cfg.Service(), id, offset, w)
offset += n
if err != nil {
return err
Expand Down
Loading