Add recipe ls command #8

Merged
decentral1se merged 1 commits from recipe-ls into main 2021-07-21 11:30:15 +00:00
3 changed files with 108 additions and 14 deletions
Showing only changes of commit 302ebcb394 - Show all commits

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
decentral1se marked this conversation as resolved Outdated

Your function never returns this error. It is just logging it and exiting the program. You should change this to return nil, err. This can then be dealt with by the command itself.

Your function never returns this error. It is just logging it and exiting the program. You should change this to `return nil, err`. This can then be dealt with by the command itself.
}
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())
decentral1se marked this conversation as resolved Outdated

This will never be run because you never return an error, see comment on GetAppsJSON

This will never be run because you never return an error, see comment on `GetAppsJSON`
}
tableCol := []string{"Name", "Category", "Status"}
table := createTable(tableCol)
for _, name := range sortByAppName(apps) {
roxxers marked this conversation as resolved Outdated

I am not totally against this approach. I worry about the speed of it but honestly might just say fuck it and use this as our baseline for sorting maps.

I am not totally against this approach. I worry about the speed of it but honestly might just say fuck it and use this as our baseline for sorting maps.
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,
},
}