-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
422d48e
commit abaa6c0
Showing
9 changed files
with
287 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/jawher/mow.cli" | ||
) | ||
|
||
func appList(cmd *cli.Cmd) { | ||
cmd.Action = func() { | ||
apps, err := GetClient().GetApplications() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if len(apps) == 0 { | ||
fmt.Println() | ||
return | ||
} | ||
|
||
lengths := []int{8, 32, 24, 32, 12} | ||
tabsPrint(columns{"APPID", "NAME", "SHORT_NAME", "DEPLOY_NAME", "SURCHARGE"}, lengths) | ||
for _, app := range apps { | ||
tabsPrint(columns{app.ID, app.Name, app.ShortName, app.DeployName, app.Surcharge}, lengths) | ||
} | ||
tabsFlush() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package lib | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// Application on Vultr | ||
type Application struct { | ||
ID string `json:"APPID"` | ||
Name string `json:"name"` | ||
ShortName string `json:"short_name"` | ||
DeployName string `json:"deploy_name"` | ||
Surcharge float64 `json:"surcharge"` | ||
} | ||
|
||
type applications []Application | ||
|
||
func (s applications) Len() int { return len(s) } | ||
func (s applications) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s applications) Less(i, j int) bool { | ||
return strings.ToLower(s[i].Name) < strings.ToLower(s[j].Name) | ||
} | ||
|
||
// GetApplications returns a list of all available applications on Vultr | ||
func (c *Client) GetApplications() ([]Application, error) { | ||
var appMap map[string]Application | ||
if err := c.get(`app/list`, &appMap); err != nil { | ||
return nil, err | ||
} | ||
|
||
var appList []Application | ||
for _, app := range appMap { | ||
appList = append(appList, app) | ||
} | ||
sort.Sort(applications(appList)) | ||
return appList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lib | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Applications_GetApplications_Error(t *testing.T) { | ||
server, client := getTestServerAndClient(http.StatusNotAcceptable, `{error}`) | ||
defer server.Close() | ||
|
||
apps, err := client.GetApplications() | ||
assert.Nil(t, apps) | ||
if assert.NotNil(t, err) { | ||
assert.Equal(t, `{error}`, err.Error()) | ||
} | ||
} | ||
|
||
func Test_Applications_GetApplications_NoApplication(t *testing.T) { | ||
server, client := getTestServerAndClient(http.StatusOK, `[]`) | ||
defer server.Close() | ||
|
||
apps, err := client.GetApplications() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
assert.Nil(t, apps) | ||
} | ||
|
||
func Test_Applications_GetApplications_OK(t *testing.T) { | ||
server, client := getTestServerAndClient(http.StatusOK, `{ | ||
"2": {"APPID": "2","name": "WordPress","short_name": "wordpress","deploy_name": "WordPress on CentOS 6 x64","surcharge": 0}, | ||
"1": {"APPID": "1","name": "LEMP","short_name": "lemp","deploy_name": "LEMP on CentOS 6 x64","surcharge": 5} | ||
}`) | ||
defer server.Close() | ||
|
||
apps, err := client.GetApplications() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if assert.NotNil(t, apps) { | ||
assert.Equal(t, 2, len(apps)) | ||
|
||
assert.Equal(t, "1", apps[0].ID) | ||
assert.Equal(t, "LEMP", apps[0].Name) | ||
assert.Equal(t, "lemp", apps[0].ShortName) | ||
assert.Equal(t, "LEMP on CentOS 6 x64", apps[0].DeployName) | ||
assert.Equal(t, float64(5), apps[0].Surcharge) | ||
|
||
assert.Equal(t, "2", apps[1].ID) | ||
assert.Equal(t, "WordPress", apps[1].Name) | ||
assert.Equal(t, "wordpress", apps[1].ShortName) | ||
assert.Equal(t, "WordPress on CentOS 6 x64", apps[1].DeployName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.