chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:49:06 +08:00
commit 60552ca0ce
144 changed files with 20883 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
package utils
import (
"fmt"
"os/exec"
"runtime"
"github.com/rs/zerolog/log"
)
func OpenBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Error().Err(err).Msg("While trying to open a browser")
}
}
+12
View File
@@ -0,0 +1,12 @@
package utils
const (
Black = "\033[1;30m%s\033[0m"
Red = "\033[1;31m%s\033[0m"
Green = "\033[1;32m%s\033[0m"
Yellow = "\033[1;33m%s\033[0m"
Blue = "\033[1;34m%s\033[0m"
Magenta = "\033[1;35m%s\033[0m"
Cyan = "\033[1;36m%s\033[0m"
White = "\033[1;37m%s\033[0m"
)
+69
View File
@@ -0,0 +1,69 @@
package utils
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
)
const (
X_KUBESHARK_CAPTURE_HEADER_KEY = "X-Kubeshark-Capture"
X_KUBESHARK_CAPTURE_HEADER_IGNORE_VALUE = "ignore"
)
// Get - When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
func Get(url string, client *http.Client) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
AddIgnoreCaptureHeader(req)
return checkError(client.Do(req))
}
// Post - When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
func Post(url, contentType string, body io.Reader, client *http.Client, licenseKey string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
AddIgnoreCaptureHeader(req)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("License-Key", licenseKey)
return checkError(client.Do(req))
}
// Do - When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
func Do(req *http.Request, client *http.Client) (*http.Response, error) {
return checkError(client.Do(req))
}
func checkError(response *http.Response, errInOperation error) (*http.Response, error) {
if errInOperation != nil {
return response, errInOperation
// Check only if status != 200 (and not status >= 300). Hub return only 200 on success.
} else if response.StatusCode != http.StatusOK {
body, err := io.ReadAll(response.Body)
response.Body.Close()
response.Body = io.NopCloser(bytes.NewBuffer(body)) // rewind
if err != nil {
return response, err
}
errorMsg := strings.ReplaceAll(string(body), "\n", ";")
return response, fmt.Errorf("got response with status code: %d, body: %s", response.StatusCode, errorMsg)
}
return response, nil
}
func AddIgnoreCaptureHeader(req *http.Request) {
req.Header.Set(X_KUBESHARK_CAPTURE_HEADER_KEY, X_KUBESHARK_CAPTURE_HEADER_IGNORE_VALUE)
}
+17
View File
@@ -0,0 +1,17 @@
package utils
import (
"strconv"
"strings"
"github.com/rs/zerolog/log"
)
func UnescapeUnicodeCharacters(raw string) string {
str, err := strconv.Unquote(strings.Replace(strconv.Quote(raw), `\\u`, `\u`, -1))
if err != nil {
log.Error().Err(err).Send()
return raw
}
return str
}
+19
View File
@@ -0,0 +1,19 @@
package utils
import (
"bytes"
"github.com/goccy/go-yaml"
)
func PrettyYaml(data interface{}) (result string, err error) {
buffer := new(bytes.Buffer)
encoder := yaml.NewEncoder(buffer, yaml.Indent(2))
err = encoder.Encode(data)
if err != nil {
return
}
result = buffer.String()
return
}
+54
View File
@@ -0,0 +1,54 @@
package utils
func Contains(slice []string, containsValue string) bool {
for _, sliceValue := range slice {
if sliceValue == containsValue {
return true
}
}
return false
}
func Unique(slice []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func EqualStringSlices(slice1 []string, slice2 []string) bool {
if len(slice1) != len(slice2) {
return false
}
for _, v := range slice1 {
if !Contains(slice2, v) {
return false
}
}
return true
}
// Diff returns the elements in `a` that aren't in `b`.
func Diff(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []string
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}
+26
View File
@@ -0,0 +1,26 @@
package utils
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/rs/zerolog/log"
)
func WaitForTermination(ctx context.Context, cancel context.CancelFunc) {
log.Debug().Msg("Waiting to finish...")
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
// block until ctx cancel is called or termination signal is received
select {
case <-ctx.Done():
log.Debug().Msg("Context done.")
break
case <-sigChan:
log.Debug().Msg("Got a termination signal, canceling execution...")
cancel()
}
}