Merge pull request 'Add recipe ls command' (#8) from recipe-ls into main

Reviewed-on: https://git.autonomic.zone/coop-cloud/go-abra/pulls/8
This commit is contained in:
decentral1se 2021-07-21 13:30:14 +02:00
commit 05ff163386
3 changed files with 108 additions and 14 deletions

View File

@ -30,7 +30,7 @@ Disclaimer!: List is WIP
- [ ] `undeploy`
- [ ] `volume`
- [ ] `abra recipe`
- [ ] `ls`
- [x] `ls`
- [ ] `create`
- [ ] `release`
- [ ] `versions`

View File

@ -16,19 +16,7 @@ func RunApp(version string, commit string) {
Commands: []*cli.Command{
AppCommand,
ServerCommand,
{
Name: "recipe",
HideHelp: true,
Subcommands: []*cli.Command{
{
Name: "list",
},
{
Name: "create",
},
},
},
RecipeCommand,
VersionCommand,
},
Flags: []cli.Flag{

106
cli/recipe.go Normal file
View File

@ -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,
},
}