Minor changes to util functions
This commit is contained in:
parent
601089672c
commit
751a2facdb
2 changed files with 25 additions and 5 deletions
|
|
@ -11,7 +11,7 @@ type Params map[string]any
|
||||||
type Option func(Params)
|
type Option func(Params)
|
||||||
|
|
||||||
// Extract all parameters from the options passed as map[string]any.
|
// Extract all parameters from the options passed as map[string]any.
|
||||||
func GetParams(opts ...Option) Params {
|
func ToDict(opts ...Option) Params {
|
||||||
params := Params{}
|
params := Params{}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(params)
|
opt(params)
|
||||||
|
|
@ -45,8 +45,8 @@ func WithDefault[T any](v T) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Syntactic sugar generic function to get parameter from util.Params.
|
// Sugary generic function to get parameter from util.Params.
|
||||||
func Get[T any](params Params, key string, opts ...Option) *T {
|
func Get[T any](params Params, key string) *T {
|
||||||
if v, ok := params[key].(T); ok {
|
if v, ok := params[key].(T); ok {
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
|
|
@ -55,3 +55,16 @@ func Get[T any](params Params, key string, opts ...Option) *T {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOpt[T any](opts []Option, key string) *T {
|
||||||
|
return Get[T](ToDict(opts...), "required_claims")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Params) GetVerbose() bool {
|
||||||
|
if verbose, ok := p["verbose"].(bool); ok {
|
||||||
|
return verbose
|
||||||
|
}
|
||||||
|
|
||||||
|
// default setting
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,14 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cmp"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,7 +30,7 @@ func IsDirectory(path string) (bool, error) {
|
||||||
// This returns an *os.FileInfo type
|
// This returns an *os.FileInfo type
|
||||||
fileInfo, err := os.Stat(path)
|
fileInfo, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("failed to stat path: %v", err)
|
return false, fmt.Errorf("failed to stat path (%s): %v", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsDir is short for fileInfo.Mode().IsDir()
|
// IsDir is short for fileInfo.Mode().IsDir()
|
||||||
|
|
@ -63,7 +65,7 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string]
|
||||||
// NOTE: This currently requires git to be installed.
|
// NOTE: This currently requires git to be installed.
|
||||||
// TODO: Change how this is done to not require executing a command.
|
// TODO: Change how this is done to not require executing a command.
|
||||||
func GitCommit() string {
|
func GitCommit() string {
|
||||||
c := exec.Command("git", "rev-parse", "HEAD")
|
c := exec.Command("git", "rev-parse", "--short=8", "HEAD")
|
||||||
stdout, err := c.Output()
|
stdout, err := c.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
|
|
@ -80,6 +82,11 @@ func RemoveIndex[T comparable](s []T, index int) []T {
|
||||||
return append(ret, s[index+1:]...)
|
return append(ret, s[index+1:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveDuplicates[T cmp.Ordered](s []T) []T {
|
||||||
|
slices.Sort(s)
|
||||||
|
return slices.Compact(s)
|
||||||
|
}
|
||||||
|
|
||||||
// General function to copy elements from slice if condition is true.
|
// General function to copy elements from slice if condition is true.
|
||||||
func CopyIf[T comparable](s []T, condition func(t T) bool) []T {
|
func CopyIf[T comparable](s []T, condition func(t T) bool) []T {
|
||||||
var f = make([]T, 0)
|
var f = make([]T, 0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue