Add recipe ls command #8
2
TODO.md
2
TODO.md
|
@ -30,7 +30,7 @@ Disclaimer!: List is WIP
|
||||||
- [ ] `undeploy`
|
- [ ] `undeploy`
|
||||||
- [ ] `volume`
|
- [ ] `volume`
|
||||||
- [ ] `abra recipe`
|
- [ ] `abra recipe`
|
||||||
- [ ] `ls`
|
- [x] `ls`
|
||||||
- [ ] `create`
|
- [ ] `create`
|
||||||
- [ ] `release`
|
- [ ] `release`
|
||||||
- [ ] `versions`
|
- [ ] `versions`
|
||||||
|
|
14
cli/cli.go
14
cli/cli.go
|
@ -16,19 +16,7 @@ func RunApp(version string, commit string) {
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
AppCommand,
|
AppCommand,
|
||||||
ServerCommand,
|
ServerCommand,
|
||||||
{
|
RecipeCommand,
|
||||||
|
|
||||||
Name: "recipe",
|
|
||||||
HideHelp: true,
|
|
||||||
Subcommands: []*cli.Command{
|
|
||||||
{
|
|
||||||
Name: "list",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "create",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
VersionCommand,
|
VersionCommand,
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Image struct {
|
||||||
|
Image string `json:"image"`
|
||||||
|
Rating string `json:"rating"`
|
||||||
|
Source string `json:"source"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AppFeatureSpec struct {
|
||||||
|
Backups string `json:"backups"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Healthcheck string `json:"healthcheck"`
|
||||||
|
Image Image `json:"image"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
Tests string `json:"tests"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AppVersionSpec struct {
|
||||||
|
Digest string `json:"digest"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
Tag string `json:"tag"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AppSpec struct {
|
||||||
|
Category string `json:"category"`
|
||||||
|
DefaultBranch string `json:"default_branch"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Features AppFeatureSpec `json:"features"`
|
||||||
|
Icon string `json:"icon"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Repository string `json:"repository"`
|
||||||
|
Versions map[string]map[string]AppVersionSpec `json:"versions"`
|
||||||
|
Website string `json:"website"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AppsJson map[string]AppSpec
|
||||||
|
|
||||||
|
func getJson(url string, target interface{}) error {
|
||||||
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
|
res, err := client.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
return json.NewDecoder(res.Body).Decode(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAppsJSON() (AppsJson, error) {
|
||||||
|
url := "https://apps.coopcloud.tech"
|
||||||
|
apps := make(AppsJson)
|
||||||
|
if err := getJson(url, &apps); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apps, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortByAppName(apps AppsJson) []string {
|
||||||
|
var names []string
|
||||||
|
for name, _ := range apps {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Slice(names, func(i, j int) bool {
|
||||||
|
return names[i] < names[j]
|
||||||
|
})
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
var recipeListCommand = &cli.Command{
|
||||||
|
Name: "list",
|
||||||
|
Aliases: []string{"ls"},
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
apps, err := GetAppsJSON()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err.Error())
|
||||||
|
}
|
||||||
|
tableCol := []string{"Name", "Category", "Status"}
|
||||||
|
table := createTable(tableCol)
|
||||||
|
for _, name := range sortByAppName(apps) {
|
||||||
|
appSpec := apps[name]
|
||||||
|
tableRow := []string{appSpec.Name, appSpec.Category, fmt.Sprintf("%v", appSpec.Features.Status)}
|
||||||
|
table.Append(tableRow)
|
||||||
|
}
|
||||||
|
table.Render()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var RecipeCommand = &cli.Command{
|
||||||
|
Name: "recipe",
|
||||||
|
HideHelp: true,
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
recipeListCommand,
|
||||||
|
},
|
||||||
|
}
|
Loading…
Reference in New Issue