garm/util/util.go
Gabriel Adrian Samfira eec158b32c Add SPA UI for GARM
This change adds a single page application front-end to GARM. It uses
a generated REST client, built from the swagger definitions, the websocket
interface for live updates of entities and eager loading of everything
except runners, as users may have many runners and we don't want to load
hundreds of runners in memory.

Proper pagination should be implemented in the API, in future commits,
to avoid loading lots of elements for no reason.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2025-08-16 09:09:13 +00:00

113 lines
2.8 KiB
Go

// Copyright 2022 Cloudbase Solutions SRL
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package util
import (
"context"
"net/http"
"unicode/utf8"
"github.com/pkg/errors"
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm/runner/common"
)
func FetchTools(ctx context.Context, cli common.GithubClient) ([]commonParams.RunnerApplicationDownload, error) {
tools, ghResp, err := cli.ListEntityRunnerApplicationDownloads(ctx)
if err != nil {
if ghResp != nil && ghResp.StatusCode == http.StatusUnauthorized {
return nil, errors.Wrap(runnerErrors.ErrUnauthorized, "fetching tools")
}
return nil, errors.Wrap(err, "fetching runner tools")
}
ret := []commonParams.RunnerApplicationDownload{}
for _, tool := range tools {
if tool == nil {
continue
}
ret = append(ret, commonParams.RunnerApplicationDownload(*tool))
}
return ret, nil
}
func ASCIIEqualFold(s, t string) bool {
// Fast ASCII path for equal-length ASCII strings
if len(s) == len(t) && isASCII(s) && isASCII(t) {
for i := 0; i < len(s); i++ {
a, b := s[i], t[i]
if a != b {
if 'A' <= a && a <= 'Z' {
a = a + 'a' - 'A'
}
if 'A' <= b && b <= 'Z' {
b = b + 'a' - 'A'
}
if a != b {
return false
}
}
}
return true
}
// UTF-8 path - handle different byte lengths correctly
i, j := 0, 0
for i < len(s) && j < len(t) {
sr, sizeS := utf8.DecodeRuneInString(s[i:])
tr, sizeT := utf8.DecodeRuneInString(t[j:])
// Handle invalid UTF-8 - they must be identical
if sr == utf8.RuneError || tr == utf8.RuneError {
// For invalid UTF-8, compare the raw bytes
if sr == utf8.RuneError && tr == utf8.RuneError {
if sizeS == sizeT && s[i:i+sizeS] == t[j:j+sizeT] {
i += sizeS
j += sizeT
continue
}
}
return false
}
if sr != tr {
// Apply ASCII case folding only
if 'A' <= sr && sr <= 'Z' {
sr = sr + 'a' - 'A'
}
if 'A' <= tr && tr <= 'Z' {
tr = tr + 'a' - 'A'
}
if sr != tr {
return false
}
}
i += sizeS
j += sizeT
}
return i == len(s) && j == len(t)
}
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= 0x80 {
return false
}
}
return true
}