Skip to content

Commit

Permalink
Add json output to skaffold schema list (#4385)
Browse files Browse the repository at this point in the history
Fixes #4382

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot authored Jun 24, 2020
1 parent a79f260 commit 8f14d11
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cmd/skaffold/app/cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/schema"
)
Expand All @@ -40,6 +41,10 @@ func NewCmdSchemaList() *cobra.Command {
return NewCmd("list").
WithDescription("List skaffold.yaml's json schema versions").
WithExample("List all the versions", "schema list").
WithExample("List all the versions, in json format", "schema list -o json").
WithFlags(func(f *pflag.FlagSet) {
f.StringVarP(&schema.OutputType, "output", "o", "plain", "Type of output: `plain` or `json`.")
}).
NoArgs(schema.List)
}

Expand Down
42 changes: 40 additions & 2 deletions cmd/skaffold/app/cmd/schema/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,55 @@ package schema

import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema"
)

var OutputType string

// List prints to `out` all supported schema versions.
func List(_ context.Context, out io.Writer) error {
for _, version := range schema.SchemaVersions {
fmt.Fprintln(out, version.APIVersion)
return list(out, OutputType)
}

func list(out io.Writer, outputType string) error {
switch outputType {
case "json":
return printJSON(out)
case "plain":
return printPlain(out)
default:
return fmt.Errorf(`invalid output type: %q. Must be "plain" or "json"`, outputType)
}
}

type schemaList struct {
Versions []string `json:"versions"`
}

func printJSON(out io.Writer) error {
return json.NewEncoder(out).Encode(schemaList{
Versions: versions(),
})
}

func printPlain(out io.Writer) error {
for _, version := range versions() {
fmt.Fprintln(out, version)
}

return nil
}

func versions() []string {
var versions []string

for _, version := range schema.SchemaVersions {
versions = append(versions, version.APIVersion)
}

return versions
}
30 changes: 26 additions & 4 deletions cmd/skaffold/app/cmd/schema/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ package schema

import (
"bytes"
"context"
"fmt"
"strings"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestList(t *testing.T) {
func TestListPlain(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
var out bytes.Buffer

err := List(context.Background(), &out)
err := list(&out, "plain")
t.CheckNoError(err)

versions := out.String()
t.CheckNoError(err)
t.CheckTrue(strings.HasSuffix(versions, latest.Version+"\n"))
t.CheckTrue(strings.HasPrefix(versions, `skaffold/v1alpha1
skaffold/v1alpha2
Expand Down Expand Up @@ -61,3 +61,25 @@ skaffold/v1
skaffold/v2alpha1`))
})
}

func TestListJson(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
var out bytes.Buffer

err := list(&out, "json")
t.CheckNoError(err)

versions := out.String()
t.CheckTrue(strings.HasPrefix(versions, `{"versions":["skaffold/v1alpha1","skaffold/v1alpha2",`))
t.CheckTrue(strings.HasSuffix(versions, fmt.Sprintf(",\"%s\"]}\n", latest.Version)))
})
}

func TestListInvalidType(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
var out bytes.Buffer

err := list(&out, "invalid")
t.CheckErrorContains(`invalid output type: "invalid". Must be "plain" or "json"`, err)
})
}
9 changes: 9 additions & 0 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -869,13 +869,22 @@ Examples:
# List all the versions
skaffold schema list
# List all the versions, in json format
skaffold schema list -o json
Options:
-o, --output='plain': Type of output: `plain` or `json`.
Usage:
skaffold schema list [options]
Use "skaffold options" for a list of global command-line options (applies to all commands).
```
Env vars:

* `SKAFFOLD_OUTPUT` (same as `--output`)

### skaffold survey

Expand Down

0 comments on commit 8f14d11

Please sign in to comment.