From f509ed3f77570b8494085eca613fbda7b08bc313 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Sun, 28 Jan 2024 22:56:34 +0300 Subject: [PATCH] fix json handling --- map.go | 46 ++++++++++++++++++++++++++------------ map_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- util/util.go | 3 ++- 3 files changed, 96 insertions(+), 16 deletions(-) diff --git a/map.go b/map.go index b09119c..6dccd1e 100644 --- a/map.go +++ b/map.go @@ -4,13 +4,13 @@ import ( "encoding/csv" "encoding/json" "errors" - "fmt" - "golang.org/x/text/cases" - "golang.org/x/text/language" "io" "os" "path" "strings" + + "golang.org/x/text/cases" + "golang.org/x/text/language" ) var ErrUnsupportedFormat = errors.New("unsupported map format") @@ -96,22 +96,40 @@ func loadMapJSON(f io.Reader) (map[string]string, error) { } for _, file := range data.Assets { - builtUri := fmt.Sprintf("%s/%s_%s.%s", - file.BoardSlug, - distroCaser.String(file.DistroRelease), - file.KernelBranch, - file.Extension) + var sb strings.Builder - m[builtUri] = file.FileURL + sb.WriteString(file.BoardSlug) + sb.WriteString("/") + sb.WriteString(distroCaser.String(file.DistroRelease)) + sb.WriteString("_") + sb.WriteString(file.KernelBranch) + + if file.ImageVariant != "server" { + sb.WriteString("_") + sb.WriteString(file.ImageVariant) + } + + if file.Preinstalled != "" { + sb.WriteString("-") + sb.WriteString(file.Preinstalled) + } if file.Extension == "img.xz" { - noExtUri := fmt.Sprintf("%s/%s_%s", - file.BoardSlug, - distroCaser.String(file.DistroRelease), - file.KernelBranch) + m[sb.String()] = file.FileURL + } - m[noExtUri] = file.FileURL + sb.WriteString(".") + + if file.Extension == "img.xz.sha" { + sb.WriteString("sha") + } else if file.Extension == "img.xz.asc" { + sb.WriteString("asc") + } else { + sb.WriteString(file.Extension) } + + builtUri := sb.String() + m[builtUri] = file.FileURL } return m, nil diff --git a/map_test.go b/map_test.go index 7911f63..014131d 100644 --- a/map_test.go +++ b/map_test.go @@ -1,10 +1,12 @@ package redirector import ( + "fmt" + "strings" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" log "github.com/sirupsen/logrus" - "strings" ) var _ = Describe("Map", func() { @@ -40,4 +42,63 @@ var _ = Describe("Map", func() { Expect(err).To(BeNil()) Expect(m["aml-s9xx-box/Bookworm_current"]).To(Equal("https://dl.armbian.com/aml-s9xx-box/archive/Armbian_23.11.1_Aml-s9xx-box_bookworm_current_6.1.63.img.xz")) }) + + It("Should successfully load the map from a JSON file", func() { + data := `{ + "assets": [ + { + "board_slug": "khadas-vim1", + "file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz", + "file_updated": "2023-11-30T01:06:34Z", + "file_size": "1605260504", + "distro_release": "bookworm", + "kernel_branch": "current", + "image_variant": "xfce", + "preinstalled_application": "", + "promoted": "false", + "download_repository": "archive", + "file_extension": "img.xz" + }, + { + "board_slug": "khadas-vim1", + "file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz.sha", + "file_updated": "2023-11-30T01:06:34Z", + "file_size": "1605260504", + "distro_release": "bookworm", + "kernel_branch": "current", + "image_variant": "xfce", + "preinstalled_application": "", + "promoted": "false", + "download_repository": "archive", + "file_extension": "img.xz.sha" + }, + { + "board_slug": "khadas-vim1", + "file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz", + "file_updated": "2023-11-30T01:06:34Z", + "file_size": "1605260504", + "distro_release": "bookworm", + "kernel_branch": "current", + "image_variant": "xfce", + "preinstalled_application": "test", + "promoted": "false", + "download_repository": "archive", + "file_extension": "img.xz" + } + ] +}` + + m, err := loadMapJSON(strings.NewReader(data)) + + log.Println(m) + + for k, v := range m { + fmt.Printf("%s => %s\n", k, v) + } + + Expect(err).To(BeNil()) + Expect(m["khadas-vim1/Bookworm_current_xfce"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz")) + Expect(m["khadas-vim1/Bookworm_current_xfce.sha"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz.sha")) + Expect(m["khadas-vim1/Bookworm_current_xfce-test"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz")) + }) }) diff --git a/util/util.go b/util/util.go index 211d4ad..0691646 100644 --- a/util/util.go +++ b/util/util.go @@ -1,10 +1,11 @@ package util import ( - "github.com/armbian/redirector/db" "math/rand" "reflect" "strings" + + "github.com/armbian/redirector/db" ) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")