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 show command #516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
104 changes: 104 additions & 0 deletions command/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package command

import (
"fmt"
"net/url"
"runtime"
"strings"

"github.com/shyiko/jabba/semver"
"github.com/spf13/pflag"
)

const notAvailable = "Not Available"

type versionInfo struct {
name string
isInstalled bool
downloadURL string
javaMajorVersion string
vendor string
err error
}

func newVeriosnInfo(selector string) *versionInfo {
return &versionInfo{name: selector,
isInstalled: false,
downloadURL: notAvailable,
vendor: notAvailable,
javaMajorVersion: notAvailable,
}
}

func (v *versionInfo) formatString() string {
templ := "%s\n" +
" Installed:\t\t%s\n" +
" DownloadUrl:\t\t%s\n" +
" JavaMajorVersion:\t%s\n" +
" Vendor:\t\t%s\n"
isInstalled := "No"
if v.isInstalled {
isInstalled = "Yes"
}
return fmt.Sprintf(templ, v.name, isInstalled, v.downloadURL, v.javaMajorVersion, v.vendor)
}

// Show prints the information about given slelectors
func Show(args []string) error {
if len(args) == 0 {
return pflag.ErrHelp
}
for _, s := range args {
info := getVersionInfoFor(s)
if info.err != nil {
fmt.Printf("%s: %s\n", s, info.err)
} else {
fmt.Println(info.formatString())
}
}

return nil
}

// This is a wrapper for Ls which allow to mock Ls functionality for tests
var ls = func() ([]*semver.Version, error) { return Ls() }

// This is a wrapper for Ls which allow to mock LsRemote functionality for tests
var lsRemote = func() (map[*semver.Version]string, error) {
return LsRemote(runtime.GOOS, runtime.GOARCH)
}

func getVersionInfoFor(selector string) *versionInfo {
ver, err := semver.ParseVersion(selector)
if err != nil {
return &versionInfo{err: fmt.Errorf("%s is not a valid version", selector)}
}
info := newVeriosnInfo(selector)
installed, err := ls()
for _, v := range installed {
if v.Equals(ver) {
info.isInstalled = true
info.javaMajorVersion = fmt.Sprintf("%d.%d", v.Major(), v.Minor())
}
}
releaseMap, _ := lsRemote()
for v, url := range releaseMap {
if v.Equals(ver) {
info.downloadURL = strings.Join(strings.Split(url, "+")[1:], "")
info.vendor = getVendor(info.downloadURL)
info.javaMajorVersion = fmt.Sprintf("%d.%d", v.Major(), v.Minor())
}
}
return info
}

func getVendor(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return ""
}
parts := strings.Split(u.Hostname(), ".")
fmt.Println(parts)
domain := parts[len(parts)-2] + "." + parts[len(parts)-1]
return domain
}
142 changes: 142 additions & 0 deletions command/show_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package command

import (
"fmt"
"reflect"
"testing"

"github.com/shyiko/jabba/semver"
)

func TestGetVersionInfoForGivenSelectorForInvalidVersion(t *testing.T) {
selector := "sdfsd"
info := getVersionInfoFor(selector)

if info.err == nil {
t.Errorf("Expected error. Got %s", info.err.Error())
}
expectedError := fmt.Sprintf("%s is not a valid version", selector)
if info.err.Error() != expectedError {
t.Errorf("Expected: %s\nGot: %s", expectedError, info.err.Error())
}
}

func TestGetVersionInfoForGivenSelectorForValidVersionButNotAvailableAndNotInstalled(t *testing.T) {
selector := "[email protected]"
ls = func() ([]*semver.Version, error) { return []*semver.Version{}, nil }
lsRemote = func() (map[*semver.Version]string, error) { return make(map[*semver.Version]string, 0), nil }
info := getVersionInfoFor(selector)

if info.err != nil {
t.Errorf("Expected error to be nil. Got %s", info.err.Error())
}
expected := &versionInfo{
name: selector,
downloadURL: notAvailable,
isInstalled: false,
javaMajorVersion: notAvailable,
vendor: notAvailable,
err: nil,
}
if !reflect.DeepEqual(info, expected) {
t.Errorf("Expected: %v\nGot: %v", expected, info)
}
}

func TestGetVersionInfoForGivenSelectorForValidVersionInstalled(t *testing.T) {
selector := "[email protected]"
v, _ := semver.ParseVersion(selector)
ls = func() ([]*semver.Version, error) {
return []*semver.Version{v}, nil
}
lsRemote = func() (map[*semver.Version]string, error) { return make(map[*semver.Version]string, 0), nil }
info := getVersionInfoFor(selector)

if info.err != nil {
t.Errorf("Expected error to be nil. Got %s", info.err.Error())
}
expected := &versionInfo{
name: selector,
downloadURL: notAvailable,
isInstalled: true,
javaMajorVersion: "1.1",
vendor: notAvailable,
err: nil,
}
if !reflect.DeepEqual(info, expected) {
t.Errorf("Expected: %v\nGot: %v", expected, info)
}
}

func TestGetVersionInfoForGivenSelectorForValidVersionAvailableButNotIsnstall(t *testing.T) {
selector := "[email protected]"
v, _ := semver.ParseVersion(selector)
ls = func() ([]*semver.Version, error) { return []*semver.Version{}, nil }
releaseMap := map[*semver.Version]string{}
releaseMap[v] = "tgz+https://dl.foo.com/some/some-java.tar.gz"
lsRemote = func() (map[*semver.Version]string, error) { return releaseMap, nil }
info := getVersionInfoFor(selector)

if info.err != nil {
t.Errorf("Expected error to be nil. Got %s", info.err.Error())
}
expected := &versionInfo{
name: selector,
downloadURL: "https://dl.foo.com/some/some-java.tar.gz",
isInstalled: false,
javaMajorVersion: "1.1",
vendor: "foo.com",
err: nil,
}
if !reflect.DeepEqual(info, expected) {
t.Errorf("Expected: %v\nGot: %v", expected, info)
}
}

func TestGetVersionInfoForGivenSelectorForValidVersionAvailableAndIsnstalled(t *testing.T) {
selector := "[email protected]"
v, _ := semver.ParseVersion(selector)
ls = func() ([]*semver.Version, error) { return []*semver.Version{v}, nil }
releaseMap := map[*semver.Version]string{}
releaseMap[v] = "tgz+https://dl.foo.com/some/some-java.tar.gz"
lsRemote = func() (map[*semver.Version]string, error) { return releaseMap, nil }
info := getVersionInfoFor(selector)

if info.err != nil {
t.Errorf("Expected error to be nil. Got %s", info.err.Error())
}
expected := &versionInfo{
name: selector,
downloadURL: "https://dl.foo.com/some/some-java.tar.gz",
isInstalled: true,
javaMajorVersion: "1.1",
vendor: "foo.com",
err: nil,
}
if !reflect.DeepEqual(info, expected) {
t.Errorf("Expected: %v\nGot: %v", expected, info)
}
}

func TestGetVersionInfoForGivenSelectorFormatString(t *testing.T) {
selector := "[email protected]"
v, _ := semver.ParseVersion(selector)
ls = func() ([]*semver.Version, error) { return []*semver.Version{v}, nil }
releaseMap := map[*semver.Version]string{}
releaseMap[v] = "tgz+https://dl.foo.com/some/some-java.tar.gz"
lsRemote = func() (map[*semver.Version]string, error) { return releaseMap, nil }
info := getVersionInfoFor(selector)

if info.err != nil {
t.Errorf("Expected error to be nil. Got %s", info.err.Error())
}
expected := `[email protected]
Installed: Yes
DownloadUrl: https://dl.foo.com/some/some-java.tar.gz
JavaMajorVersion: 1.1
Vendor: foo.com
`
if expected != info.formatString() {
t.Errorf("\nExpected: %v\nGot: %v", expected, info.formatString())
}
}
10 changes: 10 additions & 0 deletions jabba.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ func main() {
return nil
},
}
showCmd := &cobra.Command{
Use: "show [name]",
Short: "Show information about given version",
RunE: func(cmd *cobra.Command, args []string) error {
return command.Show(args)
},
Example: " jabba show [email protected]\n" +
" jabba show 1.12.0 [email protected]",
}
lsRemoteCmd := &cobra.Command{
Use: "ls-remote",
Short: "List remote versions available for install",
Expand Down Expand Up @@ -282,6 +291,7 @@ func main() {
},
},
lsCmd,
showCmd,
lsRemoteCmd,
&cobra.Command{
Use: "deactivate",
Expand Down