From 302ebcb3949a01c4ea7dbcd0bc3a4b7039e951bd Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 21 Jul 2021 10:58:13 +0200 Subject: [PATCH] feat: add recipe ls command --- TODO.md | 2 +- cli/cli.go | 14 +------ cli/recipe.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 cli/recipe.go diff --git a/TODO.md b/TODO.md index e92615f..f8d32fb 100644 --- a/TODO.md +++ b/TODO.md @@ -30,7 +30,7 @@ Disclaimer!: List is WIP - [ ] `undeploy` - [ ] `volume` - [ ] `abra recipe` - - [ ] `ls` + - [x] `ls` - [ ] `create` - [ ] `release` - [ ] `versions` diff --git a/cli/cli.go b/cli/cli.go index a3315f1..8f491cb 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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{ diff --git a/cli/recipe.go b/cli/recipe.go new file mode 100644 index 0000000..24c928b --- /dev/null +++ b/cli/recipe.go @@ -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, + }, +}