Skip to content

Commit

Permalink
Add show command shyiko#510
Browse files Browse the repository at this point in the history
  • Loading branch information
BugDiver committed Aug 30, 2019
1 parent 6dbdde0 commit 2653823
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
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
}
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 openjdk@1.12.0\n" +
" jabba show 1.12.0 openjdk@1.9.0",
}
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

0 comments on commit 2653823

Please sign in to comment.