Skip to content

Commit

Permalink
Merge pull request #588 from carolynvs/mixin-versions
Browse files Browse the repository at this point in the history
Include mixin version in porter mixins list output
  • Loading branch information
carolynvs-msft authored Sep 10, 2019
2 parents f99e7c2 + 766851a commit 2e2c2c5
Show file tree
Hide file tree
Showing 22 changed files with 444 additions and 137 deletions.
2 changes: 0 additions & 2 deletions cmd/exec/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"io"
"os"

Expand All @@ -12,7 +11,6 @@ import (
func main() {
cmd := buildRootCommand(os.Stdin)
if err := cmd.Execute(); err != nil {
fmt.Printf("err: %s\n", err)
os.Exit(1)
}
}
Expand Down
18 changes: 15 additions & 3 deletions cmd/exec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ package main

import (
"github.com/deislabs/porter/pkg/exec"
"github.com/deislabs/porter/pkg/porter/version"
"github.com/spf13/cobra"
)

func buildVersionCommand(m *exec.Mixin) *cobra.Command {
return &cobra.Command{
opts := version.Options{}

cmd := &cobra.Command{
Use: "version",
Short: "Print the mixin version",
Run: func(cmd *cobra.Command, args []string) {
m.PrintVersion()
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return m.PrintVersion(opts)
},
}

f := cmd.Flags()
f.StringVarP(&opts.RawFormat, "output", "o", string(version.DefaultVersionFormat),
"Specify an output format. Allowed values: json, plaintext")

return cmd
}
20 changes: 16 additions & 4 deletions cmd/kubernetes/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ package main

import (
"github.com/deislabs/porter/pkg/kubernetes"
"github.com/deislabs/porter/pkg/porter/version"
"github.com/spf13/cobra"
)

func buildVersionCommand(mixin *kubernetes.Mixin) *cobra.Command {
return &cobra.Command{
func buildVersionCommand(m *kubernetes.Mixin) *cobra.Command {
opts := version.Options{}

cmd := &cobra.Command{
Use: "version",
Short: "Print the mixin verison",
Run: func(cmd *cobra.Command, args []string) {
mixin.PrintVersion()
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return m.PrintVersion(opts)
},
}

f := cmd.Flags()
f.StringVarP(&opts.RawFormat, "output", "o", string(version.DefaultVersionFormat),
"Specify an output format. Allowed values: json, plaintext")

return cmd
}
5 changes: 3 additions & 2 deletions cmd/porter/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"github.com/deislabs/porter/pkg/porter"
"github.com/deislabs/porter/pkg/porter/version"
"github.com/spf13/cobra"
)

func buildVersionCommand(p *porter.Porter) *cobra.Command {
opts := porter.VersionOptions{}
opts := version.Options{}
cmd := &cobra.Command{
Use: "version",
Short: "Print the application version",
Expand All @@ -22,7 +23,7 @@ func buildVersionCommand(p *porter.Porter) *cobra.Command {
}

f := cmd.Flags()
f.StringVarP(&opts.RawFormat, "output", "o", string(porter.DefaultVersionFormat),
f.StringVarP(&opts.RawFormat, "output", "o", string(version.DefaultVersionFormat),
"Specify an output format. Allowed values: json, plaintext")

return cmd
Expand Down
19 changes: 12 additions & 7 deletions docs/content/mixin-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,21 @@ status:

# version

The version command (optional) is not used by porter. We encourage you to
implement it to be consistent with the other mixins and help users know that
they have the correct version of your mixin installed.
The version command (optional) is used by porter when listing installed mixins
via `porter mixins list`. It should support an `--output|o` flag that accepts
either `plaintext` or `json` as values, defaulting to `plaintext`.

Example:

```console
$ ~/.porter/mixins/exec/exec version
exec mixin v0.4.0-ralpha.1+dubonnet (2aa921d)
exec mixin v0.13.1-beta.1 (37f3637)
$ ~/.porter/mixins/exec/exec version --output json
{
"name": "exec",
"version": "v0.13.1-beta.1",
"commit": "37f3637",
"author": "DeisLabs"
}
```



16 changes: 12 additions & 4 deletions pkg/exec/version.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package exec

import (
"fmt"

"github.com/deislabs/porter/pkg"
"github.com/deislabs/porter/pkg/mixin"
"github.com/deislabs/porter/pkg/porter/version"
)

func (m *Mixin) PrintVersion() {
fmt.Fprintf(m.Out, "exec mixin %s (%s)\n", pkg.Version, pkg.Commit)
func (m *Mixin) PrintVersion(opts version.Options) error {
metadata := mixin.Metadata{
Name: "exec",
VersionInfo: mixin.VersionInfo{
Version: pkg.Version,
Commit: pkg.Commit,
Author: "DeisLabs",
},
}
return version.PrintVersion(m.Context, opts, metadata)
}
38 changes: 36 additions & 2 deletions pkg/exec/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"strings"
"testing"

"github.com/deislabs/porter/pkg/printer"

"github.com/deislabs/porter/pkg/porter/version"
"github.com/stretchr/testify/require"

"github.com/deislabs/porter/pkg"
)

Expand All @@ -12,10 +17,39 @@ func TestPrintVersion(t *testing.T) {
pkg.Version = "v1.2.3"

m := NewTestMixin(t)
m.PrintVersion()

opts := version.Options{}
err := opts.Validate()
require.NoError(t, err)
m.PrintVersion(opts)

gotOutput := m.TestContext.GetOutput()
wantOutput := "exec v1.2.3 (abc123) by DeisLabs"
if !strings.Contains(gotOutput, wantOutput) {
t.Fatalf("invalid output:\nWANT:\t%q\nGOT:\t%q\n", wantOutput, gotOutput)
}
}

func TestPrintJsonVersion(t *testing.T) {
pkg.Commit = "abc123"
pkg.Version = "v1.2.3"

m := NewTestMixin(t)

opts := version.Options{}
opts.RawFormat = string(printer.FormatJson)
err := opts.Validate()
require.NoError(t, err)
m.PrintVersion(opts)

gotOutput := m.TestContext.GetOutput()
wantOutput := "exec mixin v1.2.3 (abc123)"
wantOutput := `{
"name": "exec",
"version": "v1.2.3",
"commit": "abc123",
"author": "DeisLabs"
}
`
if !strings.Contains(gotOutput, wantOutput) {
t.Fatalf("invalid output:\nWANT:\t%q\nGOT:\t%q\n", wantOutput, gotOutput)
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/kubernetes/version.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package kubernetes

import (
"fmt"

"github.com/deislabs/porter/pkg"
"github.com/deislabs/porter/pkg/mixin"
"github.com/deislabs/porter/pkg/porter/version"
)

func (m *Mixin) PrintVersion() {
fmt.Fprintf(m.Out, "kubernetes mixin %s (%s)\n", pkg.Version, pkg.Commit)
func (m *Mixin) PrintVersion(opts version.Options) error {
metadata := mixin.Metadata{
Name: "kubernetes",
VersionInfo: mixin.VersionInfo{
Version: pkg.Version,
Commit: pkg.Commit,
Author: "DeisLabs",
},
}
return version.PrintVersion(m.Context, opts, metadata)
}
56 changes: 56 additions & 0 deletions pkg/kubernetes/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package kubernetes

import (
"strings"
"testing"

"github.com/deislabs/porter/pkg/printer"

"github.com/deislabs/porter/pkg/porter/version"
"github.com/stretchr/testify/require"

"github.com/deislabs/porter/pkg"
)

func TestPrintVersion(t *testing.T) {
pkg.Commit = "abc123"
pkg.Version = "v1.2.3"

m := NewTestMixin(t)

opts := version.Options{}
err := opts.Validate()
require.NoError(t, err)
m.PrintVersion(opts)

gotOutput := m.TestContext.GetOutput()
wantOutput := "kubernetes v1.2.3 (abc123) by DeisLabs"
if !strings.Contains(gotOutput, wantOutput) {
t.Fatalf("invalid output:\nWANT:\t%q\nGOT:\t%q\n", wantOutput, gotOutput)
}
}

func TestPrintJsonVersion(t *testing.T) {
pkg.Commit = "abc123"
pkg.Version = "v1.2.3"

m := NewTestMixin(t)

opts := version.Options{}
opts.RawFormat = string(printer.FormatJson)
err := opts.Validate()
require.NoError(t, err)
m.PrintVersion(opts)

gotOutput := m.TestContext.GetOutput()
wantOutput := `{
"name": "kubernetes",
"version": "v1.2.3",
"commit": "abc123",
"author": "DeisLabs"
}
`
if !strings.Contains(gotOutput, wantOutput) {
t.Fatalf("invalid output:\nWANT:\t%q\nGOT:\t%q\n", wantOutput, gotOutput)
}
}
11 changes: 11 additions & 0 deletions pkg/mixin/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mixin

import (
"testing"

"github.com/deislabs/porter/pkg/test"
)

func TestMain(m *testing.M) {
test.TestMainWithMockedCommandHandlers(m)
}
22 changes: 13 additions & 9 deletions pkg/mixin/metadata.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package mixin

// Metadata about a mixin
// Metadata about an installed mixin.
type Metadata struct {
// Mixin Name
Name string
Name string `json:"name"`
// Mixin Directory
Dir string
Dir string `json:"dir,omitempty"`
// Path to the client executable
ClientPath string
// Version
// Repository or Source (where did it come from)
// Author
// Is it up to date
// etc
ClientPath string `json:"clientPath,omitempty"`
// Metadata about the mixin version returned from calling version on the mixin
VersionInfo
}

// Information from running the version command against the mixin.
type VersionInfo struct {
Version string `json:"version"`
Commit string `json:"commit"`
Author string `json:"author,omitempty"`
}
32 changes: 32 additions & 0 deletions pkg/mixin/provider/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mixinprovider

import (
"bytes"
"encoding/json"
"io/ioutil"
"path/filepath"

Expand Down Expand Up @@ -71,6 +72,8 @@ func (p *FileSystem) GetSchema(m mixin.Metadata) (string, error) {
return mixinSchema.String(), nil
}

// GetVersion is the obsolete form of retrieving mixin version, e.g. exec version, which returned an unstructured
// version string. It will be deprecated soon and is replaced by GetVersionMetadata.
func (p *FileSystem) GetVersion(m mixin.Metadata) (string, error) {
r := mixin.NewRunner(m.Name, m.Dir, false)
r.Command = "version"
Expand All @@ -92,3 +95,32 @@ func (p *FileSystem) GetVersion(m mixin.Metadata) (string, error) {

return mixinVersion.String(), nil
}

// GetVersionMetadata is the new form of retrieving mixin version, e.g. exec version --output json, which returns
// a structured version string. It replaces GetVersion.
func (p *FileSystem) GetVersionMetadata(m mixin.Metadata) (*mixin.VersionInfo, error) {
r := mixin.NewRunner(m.Name, m.Dir, false)
r.Command = "version --output json"

// Copy the existing context and tweak to pipe the output differently
jsonB := &bytes.Buffer{}
var mixinContext context.Context
mixinContext = *p.Context
mixinContext.Out = jsonB
if !p.Debug {
mixinContext.Err = ioutil.Discard
}
r.Context = &mixinContext

err := r.Run()
if err != nil {
return nil, err
}

var response mixin.VersionInfo
err = json.Unmarshal(jsonB.Bytes(), &response)
if err != nil {
return nil, err
}
return &response, nil
}
Loading

0 comments on commit 2e2c2c5

Please sign in to comment.