mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 11:37:00 -07:00
Fixed issue with generate and added some documentation to funcs
This commit is contained in:
parent
7494468bed
commit
cd840b2bf0
9 changed files with 219 additions and 158 deletions
|
|
@ -6,9 +6,11 @@ import (
|
|||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
type Params = map[string]any
|
||||
// Params are accessible in generator.Generate().
|
||||
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 {
|
||||
params := Params{}
|
||||
for _, opt := range opts {
|
||||
|
|
@ -17,7 +19,8 @@ func GetParams(opts ...Option) Params {
|
|||
return params
|
||||
}
|
||||
|
||||
func OptionExists(params Params, opt string) bool {
|
||||
// Test if an option is present in params
|
||||
func (p *Params) OptionExists(params Params, opt string) bool {
|
||||
var k []string = maps.Keys(params)
|
||||
return slices.Contains(k, opt)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// Wrapper function to simplify checking if a path exists.
|
||||
func PathExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
|
|
@ -22,6 +23,19 @@ func PathExists(path string) (bool, error) {
|
|||
return false, err
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// IsDir is short for fileInfo.Mode().IsDir()
|
||||
return fileInfo.IsDir(), nil
|
||||
}
|
||||
|
||||
// Wrapper function to confine making a HTTP request into a single function
|
||||
// instead of multiple.
|
||||
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
||||
|
|
@ -44,14 +58,9 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string]
|
|||
return res, b, err
|
||||
}
|
||||
|
||||
func ConvertMapOutput(m map[string][]byte) map[string]string {
|
||||
n := make(map[string]string, len(m))
|
||||
for k, v := range m {
|
||||
n[k] = string(v)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// Returns the git commit string by executing command.
|
||||
// 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")
|
||||
stdout, err := c.Output()
|
||||
|
|
@ -62,13 +71,15 @@ func GitCommit() string {
|
|||
return strings.TrimRight(string(stdout), "\n")
|
||||
}
|
||||
|
||||
// NOTE: would it be better to use slices.DeleteFunc instead
|
||||
// General function to remove element by a given index.
|
||||
// NOTE: would it be better to use slices.DeleteFunc instead?
|
||||
func RemoveIndex[T comparable](s []T, index int) []T {
|
||||
ret := make([]T, 0)
|
||||
ret = append(ret, s[:index]...)
|
||||
return append(ret, s[index+1:]...)
|
||||
}
|
||||
|
||||
// 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)
|
||||
for _, e := range s {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue