2021-07-21 08:58:13 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-07-21 16:21:45 +00:00
|
|
|
"io/ioutil"
|
2021-07-21 08:58:13 +00:00
|
|
|
"net/http"
|
2021-07-21 16:21:45 +00:00
|
|
|
"os"
|
2021-07-21 08:58:13 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
"coopcloud.tech/abra/config"
|
|
|
|
|
2021-07-21 08:58:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-07-22 12:25:37 +00:00
|
|
|
type Image struct {
|
2021-07-21 08:58:13 +00:00
|
|
|
Image string `json:"image"`
|
|
|
|
Rating string `json:"rating"`
|
|
|
|
Source string `json:"source"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
type Feature struct {
|
2021-07-22 12:25:37 +00:00
|
|
|
Backups string `json:"backups"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Healthcheck string `json:"healthcheck"`
|
|
|
|
Image Image `json:"image"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Tests string `json:"tests"`
|
2021-07-21 08:58:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
type Version struct {
|
2021-07-21 08:58:13 +00:00
|
|
|
Digest string `json:"digest"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
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"`
|
2021-07-21 08:58:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
type Apps map[string]App
|
|
|
|
|
|
|
|
var httpClient = &http.Client{Timeout: 5 * time.Second}
|
|
|
|
|
|
|
|
var AppsUrl = "https://apps.coopcloud.tech"
|
2021-07-21 08:58:13 +00:00
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
func readJson(url string, target interface{}) error {
|
|
|
|
res, err := httpClient.Get(url)
|
2021-07-21 08:58:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
return json.NewDecoder(res.Body).Decode(target)
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
func AppsFSIsLatest() (bool, error) {
|
|
|
|
res, err := httpClient.Head(AppsUrl)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastModified := res.Header["Last-Modified"][0]
|
|
|
|
parsed, err := time.Parse(time.RFC1123, lastModified)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(config.APPS_JSON)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
localModifiedTime := info.ModTime().Unix()
|
|
|
|
remoteModifiedTime := parsed.Unix()
|
|
|
|
|
|
|
|
if localModifiedTime < remoteModifiedTime {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadAppsFS(target interface{}) error {
|
|
|
|
appsJsonFS, err := ioutil.ReadFile(config.APPS_JSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(appsJsonFS, &target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadAppsWeb() (Apps, error) {
|
|
|
|
apps := make(Apps)
|
|
|
|
|
|
|
|
appsFSIsLatest, err := AppsFSIsLatest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !appsFSIsLatest {
|
|
|
|
if err := readJson(AppsUrl, &apps); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
appsJson, err := json.MarshalIndent(apps, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(config.APPS_JSON, appsJson, 0644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return apps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ReadAppsFS(&apps); err != nil {
|
2021-07-21 08:58:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-21 16:21:45 +00:00
|
|
|
|
2021-07-21 08:58:13 +00:00
|
|
|
return apps, nil
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:21:45 +00:00
|
|
|
func SortByAppName(apps Apps) []string {
|
2021-07-21 08:58:13 +00:00
|
|
|
var names []string
|
2021-07-21 15:36:46 +00:00
|
|
|
for name := range apps {
|
2021-07-21 08:58:13 +00:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
2021-07-21 16:12:35 +00:00
|
|
|
sort.Strings(names)
|
2021-07-21 08:58:13 +00:00
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
var recipeListCommand = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Aliases: []string{"ls"},
|
|
|
|
Action: func(c *cli.Context) error {
|
2021-07-21 16:21:45 +00:00
|
|
|
appSpecs, err := ReadAppsWeb()
|
2021-07-21 08:58:13 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
tableCol := []string{"Name", "Category", "Status"}
|
|
|
|
table := createTable(tableCol)
|
2021-07-21 16:21:45 +00:00
|
|
|
for _, appName := range SortByAppName(appSpecs) {
|
|
|
|
appSpec := appSpecs[appName]
|
|
|
|
status := fmt.Sprintf("%v", appSpec.Features.Status)
|
|
|
|
tableRow := []string{appSpec.Name, appSpec.Category, status}
|
2021-07-21 08:58:13 +00:00
|
|
|
table.Append(tableRow)
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var RecipeCommand = &cli.Command{
|
|
|
|
Name: "recipe",
|
|
|
|
HideHelp: true,
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
recipeListCommand,
|
|
|
|
},
|
|
|
|
}
|