Merge branch 'main' into e2e
This commit is contained in:
commit
a66731641c
60 changed files with 4572 additions and 3400 deletions
|
@ -11,14 +11,13 @@ import (
|
|||
// CachingEmbedFS is a wrapper around embed.FS that allows setting a ModTime, so that the
|
||||
// default static file server can send 304s back. It can be used like this:
|
||||
//
|
||||
// var (
|
||||
// //go:embed docs
|
||||
// docsStaticFs embed.FS
|
||||
// docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
|
||||
// )
|
||||
//
|
||||
// http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)
|
||||
// var (
|
||||
// //go:embed docs
|
||||
// docsStaticFs embed.FS
|
||||
// docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
|
||||
// )
|
||||
//
|
||||
// http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)
|
||||
type CachingEmbedFS struct {
|
||||
ModTime time.Time
|
||||
FS embed.FS
|
||||
|
|
|
@ -3,7 +3,6 @@ package util
|
|||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -31,8 +30,8 @@ func Gzip(next http.Handler) http.Handler {
|
|||
}
|
||||
|
||||
var gzPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
w := gzip.NewWriter(ioutil.Discard)
|
||||
New: func() any {
|
||||
w := gzip.NewWriter(io.Discard)
|
||||
return w
|
||||
},
|
||||
}
|
||||
|
|
23
util/util.go
23
util/util.go
|
@ -35,8 +35,8 @@ func FileExists(filename string) bool {
|
|||
return stat != nil
|
||||
}
|
||||
|
||||
// InStringList returns true if needle is contained in haystack
|
||||
func InStringList(haystack []string, needle string) bool {
|
||||
// Contains returns true if needle is contained in haystack
|
||||
func Contains[T comparable](haystack []T, needle T) bool {
|
||||
for _, s := range haystack {
|
||||
if s == needle {
|
||||
return true
|
||||
|
@ -45,8 +45,8 @@ func InStringList(haystack []string, needle string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// InStringListAll returns true if all needles are contained in haystack
|
||||
func InStringListAll(haystack []string, needles []string) bool {
|
||||
// ContainsAll returns true if all needles are contained in haystack
|
||||
func ContainsAll[T comparable](haystack []T, needles []T) bool {
|
||||
matches := 0
|
||||
for _, s := range haystack {
|
||||
for _, needle := range needles {
|
||||
|
@ -58,16 +58,6 @@ func InStringListAll(haystack []string, needles []string) bool {
|
|||
return matches == len(needles)
|
||||
}
|
||||
|
||||
// InIntList returns true if needle is contained in haystack
|
||||
func InIntList(haystack []int, needle int) bool {
|
||||
for _, s := range haystack {
|
||||
if s == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SplitNoEmpty splits a string using strings.Split, but filters out empty strings
|
||||
func SplitNoEmpty(s string, sep string) []string {
|
||||
res := make([]string, 0)
|
||||
|
@ -263,7 +253,7 @@ func BasicAuth(user, pass string) string {
|
|||
|
||||
// MaybeMarshalJSON returns a JSON string of the given object, or "<cannot serialize>" if serialization failed.
|
||||
// This is useful for logging purposes where a failure doesn't matter that much.
|
||||
func MaybeMarshalJSON(v interface{}) string {
|
||||
func MaybeMarshalJSON(v any) string {
|
||||
jsonBytes, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
return "<cannot serialize>"
|
||||
|
@ -280,7 +270,8 @@ func MaybeMarshalJSON(v interface{}) string {
|
|||
// Warning: Never use this function with the intent to run the resulting command.
|
||||
//
|
||||
// Example:
|
||||
// []string{"ls", "-al", "Document Folder"} -> ls -al "Document Folder"
|
||||
//
|
||||
// []string{"ls", "-al", "Document Folder"} -> ls -al "Document Folder"
|
||||
func QuoteCommand(command []string) string {
|
||||
var quoted []string
|
||||
for _, c := range command {
|
||||
|
|
|
@ -2,7 +2,7 @@ package util
|
|||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
@ -19,27 +19,27 @@ func TestRandomString(t *testing.T) {
|
|||
|
||||
func TestFileExists(t *testing.T) {
|
||||
filename := filepath.Join(t.TempDir(), "somefile.txt")
|
||||
require.Nil(t, ioutil.WriteFile(filename, []byte{0x25, 0x86}, 0600))
|
||||
require.Nil(t, os.WriteFile(filename, []byte{0x25, 0x86}, 0600))
|
||||
require.True(t, FileExists(filename))
|
||||
require.False(t, FileExists(filename+".doesnotexist"))
|
||||
}
|
||||
|
||||
func TestInStringList(t *testing.T) {
|
||||
s := []string{"one", "two"}
|
||||
require.True(t, InStringList(s, "two"))
|
||||
require.False(t, InStringList(s, "three"))
|
||||
require.True(t, Contains(s, "two"))
|
||||
require.False(t, Contains(s, "three"))
|
||||
}
|
||||
|
||||
func TestInStringListAll(t *testing.T) {
|
||||
s := []string{"one", "two", "three", "four"}
|
||||
require.True(t, InStringListAll(s, []string{"two", "four"}))
|
||||
require.False(t, InStringListAll(s, []string{"three", "five"}))
|
||||
require.True(t, ContainsAll(s, []string{"two", "four"}))
|
||||
require.False(t, ContainsAll(s, []string{"three", "five"}))
|
||||
}
|
||||
|
||||
func TestInIntList(t *testing.T) {
|
||||
func TestContains(t *testing.T) {
|
||||
s := []int{1, 2}
|
||||
require.True(t, InIntList(s, 2))
|
||||
require.False(t, InIntList(s, 3))
|
||||
require.True(t, Contains(s, 2))
|
||||
require.False(t, Contains(s, 3))
|
||||
}
|
||||
|
||||
func TestSplitNoEmpty(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue