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

feat: add version command #70

Merged
merged 1 commit into from
Aug 31, 2021
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
1 change: 1 addition & 0 deletions cmd/ckp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ func NewCKPCommand(config config.Config) *cobra.Command {
ckpCommand.AddCommand(NewRmCommand(config))
ckpCommand.AddCommand(NewEditCommand(config))
ckpCommand.AddCommand(NewRunCommand(config))
ckpCommand.AddCommand(NewVersionCommand(config))
return ckpCommand
}
36 changes: 36 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"

"github.com/elhmn/ckp/internal/config"
"github.com/spf13/cobra"
)

//This version will be set by a goreleaser ldflag
var version = "0.0.0.dev"
var versionString = `Version: %s
Build by elhmn
Support osscameroon here https://opencollective.com/osscameroon
`

//NewVersionCommand output program version
func NewVersionCommand(conf config.Config) *cobra.Command {
command := &cobra.Command{
Use: "version",
Short: "Show ckp version",
Run: func(cmd *cobra.Command, args []string) {
if err := versionCommand(conf); err != nil {
fmt.Fprintf(conf.OutWriter, "Error: %s\n", err)
return
}
},
}

return command
}

func versionCommand(conf config.Config) error {
fmt.Fprintf(conf.OutWriter, versionString, version)
return nil
}
32 changes: 32 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd_test

import (
"bytes"
"testing"

"github.com/elhmn/ckp/cmd"
"github.com/stretchr/testify/assert"
)

//TestVersionCommand test the `ckp version` command
func TestVersionCommand(t *testing.T) {
t.Run("showed version successfully", func(t *testing.T) {
conf := createConfig(t)
writer := &bytes.Buffer{}
conf.OutWriter = writer

command := cmd.NewVersionCommand(conf)

//Set writer
command.SetOutput(conf.OutWriter)

err := command.Execute()
if err != nil {
t.Errorf("Error: failed with %s", err)
}

got := writer.String()
exp := "Version: 0.0.0.dev\nBuild by elhmn\nSupport osscameroon here https://opencollective.com/osscameroon\n"
assert.Contains(t, got, exp)
})
}