refactor(recipe): better naming, sorting and types

In order to arrange various types of sorting for the app catalogue, it
seems like the recommended approach is to maintain a separate data
structure alongside the JSON map we get from apps.coopcloud.tech.

Therefore, I attempt to provide a ToList() method and accompanying
sort.Sort interface sorting implementations. For now, this is just
sorting by app name.

I am testing this type of implementation here before moving on to
arrange different types of sorting for the `app list` command.
This commit is contained in:
decentral1se 2021-07-26 17:25:08 +02:00
parent 1f62ace524
commit 60a70d2d83
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
1 changed files with 43 additions and 31 deletions

View File

@ -8,6 +8,7 @@ import (
"os"
"path"
"sort"
"strings"
"text/template"
"time"
@ -34,38 +35,48 @@ type Feature struct {
Tests string `json:"tests"`
}
type Version struct {
type Tag = string
type Service = string
type ServiceMeta struct {
Digest string `json:"digest"`
Image string `json:"image"`
Tag string `json:"tag"`
}
type App struct {
Category string `json:"category"`
DefaultBranch string `json:"default_branch"`
Description string `json:"description"`
Features Feature `json:"features"`
Icon string `json:"icon"`
Name string `json:"name"`
Repository string `json:"repository"`
Versions map[string]map[string]Version `json:"versions"`
Website string `json:"website"`
Category string `json:"category"`
DefaultBranch string `json:"default_branch"`
Description string `json:"description"`
Features Feature `json:"features"`
Icon string `json:"icon"`
Name string `json:"name"`
Repository string `json:"repository"`
Versions map[Tag]map[Service]ServiceMeta `json:"versions"`
Website string `json:"website"`
}
type Apps map[string]App
type Name = string
type AppsCatalogue map[Name]App
func (a Apps) SortByName() []string {
var names []string
func (a AppsCatalogue) ToList() []App {
apps := make([]App, 0, len(a))
for name := range a {
names = append(names, name)
apps = append(apps, a[name])
}
sort.Strings(names)
return names
return apps
}
type ByAppName []App
func (a ByAppName) Len() int { return len(a) }
func (a ByAppName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAppName) Less(i, j int) bool {
return strings.ToLower(a[i].Name) < strings.ToLower(a[j].Name)
}
var httpClient = &http.Client{Timeout: 5 * time.Second}
var AppsUrl = "https://apps.coopcloud.tech"
var AppsCatalogueURL = "https://apps.coopcloud.tech"
func readJson(url string, target interface{}) error {
res, err := httpClient.Get(url)
@ -76,8 +87,8 @@ func readJson(url string, target interface{}) error {
return json.NewDecoder(res.Body).Decode(target)
}
func AppsFSIsLatest() (bool, error) {
res, err := httpClient.Head(AppsUrl)
func AppsCatalogueFSIsLatest() (bool, error) {
res, err := httpClient.Head(AppsCatalogueURL)
if err != nil {
return false, err
}
@ -106,29 +117,29 @@ func AppsFSIsLatest() (bool, error) {
return true, nil
}
func ReadApps() (Apps, error) {
apps := make(Apps)
func ReadAppsCatalogue() (AppsCatalogue, error) {
apps := make(AppsCatalogue)
appsFSIsLatest, err := AppsFSIsLatest()
appsFSIsLatest, err := AppsCatalogueFSIsLatest()
if err != nil {
return nil, err
}
if !appsFSIsLatest {
if err := ReadAppsWeb(&apps); err != nil {
if err := ReadAppsCatalogueWeb(&apps); err != nil {
return nil, err
}
return apps, nil
}
if err := ReadAppsFS(&apps); err != nil {
if err := ReadAppsCatalogueFS(&apps); err != nil {
return nil, err
}
return apps, nil
}
func ReadAppsFS(target interface{}) error {
func ReadAppsCatalogueFS(target interface{}) error {
appsJsonFS, err := ioutil.ReadFile(config.APPS_JSON)
if err != nil {
return err
@ -139,8 +150,8 @@ func ReadAppsFS(target interface{}) error {
return nil
}
func ReadAppsWeb(target interface{}) error {
if err := readJson(AppsUrl, &target); err != nil {
func ReadAppsCatalogueWeb(target interface{}) error {
if err := readJson(AppsCatalogueURL, &target); err != nil {
return err
}
@ -160,14 +171,15 @@ var recipeListCommand = &cli.Command{
Name: "list",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
apps, err := ReadApps()
catalogue, err := ReadAppsCatalogue()
if err != nil {
logrus.Fatal(err.Error())
}
apps := catalogue.ToList()
sort.Sort(ByAppName(apps))
tableCol := []string{"Name", "Category", "Status"}
table := createTable(tableCol)
for _, name := range apps.SortByName() {
app := apps[name]
for _, app := range apps {
status := fmt.Sprintf("%v", app.Features.Status)
tableRow := []string{app.Name, app.Category, status}
table.Append(tableRow)
@ -188,7 +200,7 @@ var recipeVersionCommand = &cli.Command{
return nil
}
apps, err := ReadApps()
apps, err := ReadAppsCatalogue()
if err != nil {
logrus.Fatal(err)
return nil