Compare commits

...

2 Commits

Author SHA1 Message Date
decentral1se fce1ab6c02
refactor: better naming for loop scoped variables
continuous-integration/drone/push Build is passing Details
2021-07-22 14:53:08 +02:00
decentral1se 381de28e83
refactor: make ReadApps main API entrypoint
This allows AppsReadFS/AppsReadWeb to be used behind the scenes of this
API for the conditional loading logic. All functions are left as public
for now while we're experimenting.
2021-07-22 14:51:56 +02:00
1 changed files with 36 additions and 29 deletions

View File

@ -94,6 +94,28 @@ func AppsFSIsLatest() (bool, error) {
return true, nil
}
func ReadApps() (Apps, error) {
apps := make(Apps)
appsFSIsLatest, err := AppsFSIsLatest()
if err != nil {
return nil, err
}
if !appsFSIsLatest {
if err := ReadAppsWeb(&apps); err != nil {
return nil, err
}
return apps, nil
}
if err := ReadAppsFS(&apps); err != nil {
return nil, err
}
return apps, nil
}
func ReadAppsFS(target interface{}) error {
appsJsonFS, err := ioutil.ReadFile(config.APPS_JSON)
if err != nil {
@ -105,36 +127,21 @@ func ReadAppsFS(target interface{}) error {
return nil
}
func ReadAppsWeb() (Apps, error) {
apps := make(Apps)
func ReadAppsWeb(target interface{}) error {
if err := readJson(AppsUrl, &target); err != nil {
return err
}
appsFSIsLatest, err := AppsFSIsLatest()
appsJson, err := json.MarshalIndent(target, "", " ")
if err != nil {
return nil, err
return 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 := ioutil.WriteFile(config.APPS_JSON, appsJson, 0644); err != nil {
return err
}
if err := ReadAppsFS(&apps); err != nil {
return nil, err
}
return apps, nil
return nil
}
func SortByAppName(apps Apps) []string {
@ -150,16 +157,16 @@ var recipeListCommand = &cli.Command{
Name: "list",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
appSpecs, err := ReadAppsWeb()
apps, err := ReadApps()
if err != nil {
logrus.Fatal(err.Error())
}
tableCol := []string{"Name", "Category", "Status"}
table := createTable(tableCol)
for _, appName := range SortByAppName(appSpecs) {
appSpec := appSpecs[appName]
status := fmt.Sprintf("%v", appSpec.Features.Status)
tableRow := []string{appSpec.Name, appSpec.Category, status}
for _, name := range SortByAppName(apps) {
app := apps[name]
status := fmt.Sprintf("%v", app.Features.Status)
tableRow := []string{app.Name, app.Category, status}
table.Append(tableRow)
}
table.Render()