mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-19 19:17:01 -07:00
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)
|
||||
|
||||
// Extract all parameters from the options passed as map[string]any.
|
||||
func GetParams(opts ...Option) Params {
|
||||
func ToDict(opts ...Option) Params {
|
||||
params := Params{}
|
||||
for _, opt := range opts {
|
||||
opt(params)
|
||||
|
|
@ -45,8 +45,8 @@ func WithDefault[T any](v T) Option {
|
|||
}
|
||||
}
|
||||
|
||||
// Syntactic sugar generic function to get parameter from util.Params.
|
||||
func Get[T any](params Params, key string, opts ...Option) *T {
|
||||
// Sugary generic function to get parameter from util.Params.
|
||||
func Get[T any](params Params, key string) *T {
|
||||
if v, ok := params[key].(T); ok {
|
||||
return &v
|
||||
}
|
||||
|
|
@ -55,3 +55,16 @@ func Get[T any](params Params, key string, opts ...Option) *T {
|
|||
}
|
||||
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 (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -28,7 +30,7 @@ func IsDirectory(path string) (bool, error) {
|
|||
// This returns an *os.FileInfo type
|
||||
fileInfo, err := os.Stat(path)
|
||||
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()
|
||||
|
|
@ -63,7 +65,7 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string]
|
|||
// NOTE: This currently requires git to be installed.
|
||||
// TODO: Change how this is done to not require executing a command.
|
||||
func GitCommit() string {
|
||||
c := exec.Command("git", "rev-parse", "HEAD")
|
||||
c := exec.Command("git", "rev-parse", "--short=8", "HEAD")
|
||||
stdout, err := c.Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
|
|
@ -80,6 +82,11 @@ func RemoveIndex[T comparable](s []T, index int) []T {
|
|||
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.
|
||||
func CopyIf[T comparable](s []T, condition func(t T) bool) []T {
|
||||
var f = make([]T, 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue