From 7e1bebc804aa3ce5e06363180fad02242daaa28d Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Sat, 18 Jan 2025 15:05:07 +0100
Subject: [PATCH] formatting fixed, better error handling

---
 server_apps.go | 219 +++++++++++++++++++++++++------------------------
 1 file changed, 114 insertions(+), 105 deletions(-)

diff --git a/server_apps.go b/server_apps.go
index 81b1325..fe20e0c 100644
--- a/server_apps.go
+++ b/server_apps.go
@@ -29,7 +29,7 @@ import (
 )
 
 type appPatch struct{
-    repository string
+	repository string
 	zip       []byte
 	targz     []byte
 }
@@ -37,7 +37,7 @@ type appPatch struct{
 type appStatus struct{
 	tested_version int
 	broken  bool
-    patch *appPatch
+	patch *appPatch
 }
 
 type appRegistry struct {
@@ -71,8 +71,8 @@ type appInfo struct {
 	targz     []byte
 
 	firstErr error
-    tags    []string
-    status *appStatus
+	tags    []string
+	status *appStatus
 }
 
 type GLProject struct {
@@ -149,9 +149,9 @@ func (s *server) zipApp(ctx context.Context, name, pathInRepo, repo string, patc
 		if err != nil {
 			return nil, fmt.Errorf("Blob.Reader: %w", err)
 		}
-        if f.Name == "flow3r.toml" && patch_source != nil{
-            fo.Write([]byte(fmt.Sprintf("patch_source = \"%s\"\n\n", *patch_source)))
-        }
+		if f.Name == "flow3r.toml" && patch_source != nil{
+			fo.Write([]byte(fmt.Sprintf("patch_source = \"%s\"\n\n", *patch_source)))
+		}
 		_, err = io.Copy(fo, rdr)
 		rdr.Close()
 		if err != nil {
@@ -228,10 +228,10 @@ func (s *server) targzApp(ctx context.Context, name, pathInRepo, repo string, pa
 				}
 			}
 		}
-        patch_bytes := []byte("")
-        if f.Name == "flow3r.toml" && patch_source != nil{
-            patch_bytes = []byte(fmt.Sprintf("patch_source = \"%s\"\n\n", *patch_source))
-        }
+		patch_bytes := []byte("")
+		if f.Name == "flow3r.toml" && patch_source != nil{
+		    patch_bytes = []byte(fmt.Sprintf("patch_source = \"%s\"\n\n", *patch_source))
+		}
 		err = t.WriteHeader(&tar.Header{
 			Name:     outPath,
 			Typeflag: tar.TypeReg,
@@ -248,9 +248,9 @@ func (s *server) targzApp(ctx context.Context, name, pathInRepo, repo string, pa
 		if err != nil {
 			return nil, fmt.Errorf("Blob.Reader: %w", err)
 		}
-        if len(patch_bytes) != 0{
-            t.Write(patch_bytes)
-        }
+		if len(patch_bytes) != 0{
+			t.Write(patch_bytes)
+		}
 		if err != nil {
 			return nil, fmt.Errorf("patch_source: %w", err)
 		}
@@ -418,13 +418,13 @@ func (s *server) getAppInfo(ctx context.Context, pathInRepo, repo string, slug *
 	if err != nil {
 		return nil, fmt.Errorf("getting gitlab stars failed: %w", err)
 	}
-    var patch_source *string = nil
-    slug_str := repo
-    if slug != nil{
-        slug_str = *slug
-        psrc := fmt.Sprintf("https://git.flow3r.garden/%s", repo)
-        patch_source = &psrc
-    }
+	var patch_source *string = nil
+	slug_str := repo
+	if slug != nil{
+		slug_str = *slug
+		psrc := fmt.Sprintf("https://git.flow3r.garden/%s", repo)
+		patch_source = &psrc
+	}
 	app := firstTime[highestVer]
 	app.stars = stars
 	zbytes, err := s.zipApp(ctx, app.name, pathInRepo, slug_str, patch_source, app.commitObj)
@@ -449,10 +449,31 @@ func (s *server) getAppInfo(ctx context.Context, pathInRepo, repo string, slug *
 
 	app.firstErr = firstErr
 
-    app.tags = make([]string, 0)
+	app.tags = make([]string, 0)
 	return app, nil
 }
 
+func getFlags(obj *object.Commit) (*map[string]interface{}, error){
+	flags, err := obj.File("tags.json")
+	if err != nil {
+		return nil, err
+	} 
+	flagreader, err := flags.Reader()
+	if err != nil {
+		return nil, err
+	}
+	flagbytes, err := io.ReadAll(flagreader)
+	if err != nil {
+		return nil, err
+	}
+	var flagmap map[string]interface{}
+	err = json.Unmarshal([]byte(flagbytes), &flagmap)
+	if err != nil {
+		return nil, err
+	}
+	return &flagmap, nil
+}
+
 func (s *server) getAppRegistry(ctx context.Context) (*appRegistry, error) {
 	s.gitMu.Lock()
 	defer s.gitMu.Unlock()
@@ -483,29 +504,15 @@ func (s *server) getAppRegistry(ctx context.Context) (*appRegistry, error) {
 	if err != nil {
 		return nil, err
 	}
-    flags, err := obj.File("tags.json")
-	if err != nil {
-		return nil, err
-	}
-    flagreader, err := flags.Reader()
-	if err != nil {
-		return nil, err
-	}
-    flagbytes, err := io.ReadAll(flagreader)
-	if err != nil {
-		return nil, err
-	}
 
-    var flagmap map[string]interface{}
-    
-    err = json.Unmarshal([]byte(flagbytes), &flagmap)
+	iter, err := obj.Files()
 	if err != nil {
 		return nil, err
 	}
 
-	iter, err := obj.Files()
+	flagmap, err := getFlags(obj)
 	if err != nil {
-		return nil, err
+		log.Printf("flags.json: couldn't read: %v", err)
 	}
 
 	var registry appRegistry
@@ -565,8 +572,10 @@ func (s *server) getAppRegistry(ctx context.Context) (*appRegistry, error) {
 	}
 
 	for _, app := range registry.apps {
+		printed := false
 		info, err := s.getAppInfo(ctx, app.pathInRepo, app.repository, nil)
 		if err != nil {
+			printed = true
 			log.Printf("App %q: %v", app.repository, err)
 			registry.shame = append(registry.shame, &appShame{
 				repository: app.repository,
@@ -574,49 +583,49 @@ func (s *server) getAppRegistry(ctx context.Context) (*appRegistry, error) {
 			})
 			continue
 		} else if info.firstErr != nil {
+			printed = true
 			log.Printf("App: %q: was okay, latest has error: %v", app.repository, info.firstErr)
 			registry.shame = append(registry.shame, &appShame{
 				repository: app.repository,
 				errorMsg:   fmt.Sprintf("%v", info.firstErr),
 			})
-		} else if app_flags, ok := flagmap[app.repository].(map[string]interface{}); ok {
-            info.status = new(appStatus)
-            printed := false
-            if tags, ok := app_flags["tags"].([]interface{}); ok {
-                for _, tag := range tags{
-                    if tagtxt, ok := tag.(string); ok {
-                        info.tags = append(info.tags, tagtxt)
-                    }
-                }
-            }
-            if status, ok := app_flags["status"].(map[string]interface{}); ok {
-                if tested, ok := status["tested_version"].(float64); ok {
-                    info.status.tested_version = int(math.Round(tested))
-                }
-                if broken, ok := status["broken"].(bool); ok {
-                    info.status.broken = broken
-                }
-                if patch, ok := status["patch"].(string); ok {
-                    printed = true
-                    patch_info, err := s.getAppInfo(ctx, "", patch, &app.repository)
-                    if err != nil {
-                        log.Printf("App %q: couldn't get patch at %q: %v", app.repository, patch, err)
-                    } else {
-                        log.Printf("App: %q: okay, patch found", app.repository)
-                        info.status.patch = &appPatch{
-                            repository: patch,
-                            zip: patch_info.zip,
-                            targz: patch_info.targz,
-                        }
-                    }
-                }
-            }
-            if !printed{
-			    log.Printf("App: %q: okay", app.repository)
-            }
-		} else {
+		} else if flagmap != nil {
+			if app_flags, ok := (*flagmap)[app.repository].(map[string]interface{}); ok{
+				info.status = new(appStatus)
+				if tags, ok := app_flags["tags"].([]interface{}); ok {
+					for _, tag := range tags{
+						if tagtxt, ok := tag.(string); ok {
+							info.tags = append(info.tags, tagtxt)
+						}
+					}
+				}
+				if status, ok := app_flags["status"].(map[string]interface{}); ok {
+					if tested, ok := status["tested_version"].(float64); ok {
+						info.status.tested_version = int(math.Round(tested))
+					}
+					if broken, ok := status["broken"].(bool); ok {
+						info.status.broken = broken
+					}
+					if patch, ok := status["patch"].(string); ok {
+						printed = true
+						patch_info, err := s.getAppInfo(ctx, "", patch, &app.repository)
+						if err != nil {
+							log.Printf("App %q: couldn't get patch at %q: %v", app.repository, patch, err)
+						} else {
+							log.Printf("App: %q: okay, patch found", app.repository)
+							info.status.patch = &appPatch{
+								repository: patch,
+								zip: patch_info.zip,
+								targz: patch_info.targz,
+							}
+						}
+					}
+				}
+			}
+		}
+		if !printed{
 			log.Printf("App: %q: okay", app.repository)
-        }
+		}
 		app.appInfo = info
 	}
 
@@ -626,7 +635,7 @@ func (s *server) getAppRegistry(ctx context.Context) (*appRegistry, error) {
 }
 
 type jsonAppPatch struct{
-    RepoURL        string    `json:"repoUrl"`
+	RepoURL        string    `json:"repoUrl"`
 	DownloadURL    string    `json:"downloadUrl"`
 	TarDownloadURL string    `json:"tarDownloadUrl"`
 }
@@ -634,7 +643,7 @@ type jsonAppPatch struct{
 type jsonAppStatus struct{
 	TestedVersion  int          `json:"tested_version"`
 	Broken         bool         `json:"broken"`
-    Patch          jsonAppPatch `json:"patch"`
+	Patch          jsonAppPatch `json:"patch"`
 }
 
 type jsonApp struct {
@@ -648,10 +657,10 @@ type jsonApp struct {
 	Description    string    `json:"description"`
 	Version        int       `json:"version"`
 	Stars          int       `json:"stars"`
-    Tags           []string  `json:"tags"`
+	Tags           []string  `json:"tags"`
 	Timestamp      time.Time `json:"timestamp"`
 	Flow3rSeed     string    `json:"flow3rSeed"`
-    Status          *jsonAppStatus  `json:"status"`
+	Status          *jsonAppStatus  `json:"status"`
 }
 
 func makeJsonApp(a *appDescriptor) jsonApp {
@@ -668,23 +677,23 @@ func makeJsonApp(a *appDescriptor) jsonApp {
 		Timestamp:      a.appInfo.commitObj.Committer.When.UTC(),
 		Stars:          a.appInfo.stars,
 		Flow3rSeed:     a.appInfo.flow3rSeed,
-        Tags:           a.appInfo.tags,
-	}
-    if a.appInfo.status != nil{
-        ret.Status = &jsonAppStatus{
-            Broken:             a.appInfo.status.broken,
-            TestedVersion:      a.appInfo.status.tested_version,
-        }
-        if a.appInfo.status.patch != nil{
-            pa := a.appInfo.status.patch.repository
-            ret.Status.Patch = jsonAppPatch{
-		        RepoURL:        "https://git.flow3r.garden/" + pa,
-                DownloadURL:    fmt.Sprintf("%sapps/zip/%s.zip", flagBaseURL, pa),
-                TarDownloadURL: fmt.Sprintf("%sapps/tar/%s.tar.gz", flagBaseURL, pa),
-            }
-        }
-    }
-    return ret
+		Tags:           a.appInfo.tags,
+	}
+	if a.appInfo.status != nil{
+		ret.Status = &jsonAppStatus{
+			Broken:             a.appInfo.status.broken,
+			TestedVersion:      a.appInfo.status.tested_version,
+		}
+		if a.appInfo.status.patch != nil{
+			pa := a.appInfo.status.patch.repository
+			ret.Status.Patch = jsonAppPatch{
+				RepoURL:        "https://git.flow3r.garden/" + pa,
+				DownloadURL:    fmt.Sprintf("%sapps/zip/%s.zip", flagBaseURL, pa),
+				TarDownloadURL: fmt.Sprintf("%sapps/tar/%s.tar.gz", flagBaseURL, pa),
+			}
+		}
+	}
+	return ret
 }
 
 func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
@@ -804,7 +813,7 @@ func (s *server) handleAppZip(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-    repository := fmt.Sprintf("%s/%s", orga, repo)
+	repository := fmt.Sprintf("%s/%s", orga, repo)
 
 	for _, app := range apps {
 		if app.repository == repository {
@@ -812,9 +821,9 @@ func (s *server) handleAppZip(w http.ResponseWriter, r *http.Request) {
 			w.Write(app.appInfo.zip)
 			return
 		}
-        if app.appInfo == nil || app.appInfo.status == nil || app.appInfo.status.patch == nil {
-            continue
-        }
+		if app.appInfo == nil || app.appInfo.status == nil || app.appInfo.status.patch == nil {
+			continue
+		}
 		if app.appInfo.status.patch.repository == repository {
 			w.Header().Add("Content-Type", "application/zip")
 			w.Write(app.appInfo.status.patch.zip)
@@ -835,7 +844,7 @@ func (s *server) handleAppTargz(w http.ResponseWriter, r *http.Request) {
 	}
 	orga := matches[1]
 	repo := matches[2]
-    repository := fmt.Sprintf("%s/%s", orga, repo)
+	repository := fmt.Sprintf("%s/%s", orga, repo)
 
 	apps, err := s.getApps(ctx)
 	if err != nil {
@@ -849,9 +858,9 @@ func (s *server) handleAppTargz(w http.ResponseWriter, r *http.Request) {
 			w.Write(app.appInfo.targz)
 			return
 		}
-        if app.appInfo == nil || app.appInfo.status == nil || app.appInfo.status.patch == nil {
-            continue
-        }
+		if app.appInfo == nil || app.appInfo.status == nil || app.appInfo.status.patch == nil {
+			continue
+		}
 		if app.appInfo.status.patch.repository == repository {
 			w.Header().Add("Content-Type", "application/x-gzip")
 			w.Header().Add("Content-Length", strconv.Itoa(len(app.appInfo.status.patch.targz)))
-- 
GitLab