107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
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,
|
|
},
|
|
}
|