Skip to content

Commit

Permalink
fix json handling
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn authored and igorpecovnik committed Jan 28, 2024
1 parent 2048c14 commit f509ed3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 16 deletions.
46 changes: 32 additions & 14 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check failure on line 16 in map.go

View workflow job for this annotation

GitHub Actions / build

exported var ErrUnsupportedFormat should have comment or be unexported
Expand Down Expand Up @@ -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()

Check failure on line 131 in map.go

View workflow job for this annotation

GitHub Actions / build

var builtUri should be builtURI
m[builtUri] = file.FileURL
}

return m, nil
Expand Down
63 changes: 62 additions & 1 deletion map_test.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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"))
})
})
3 changes: 2 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit f509ed3

Please sign in to comment.