Moved internal/ to pkg/ to make building external plugins possible
This commit is contained in:
parent
075b1a1f7f
commit
7361ec739f
19 changed files with 4 additions and 3 deletions
57
pkg/util/params.go
Normal file
57
pkg/util/params.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
opt(params)
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Assert that the options exists within the params map
|
||||
func AssertOptionsExist(params Params, opts ...string) []string {
|
||||
foundKeys := []string{}
|
||||
for k := range params {
|
||||
index := slices.IndexFunc(opts, func(s string) bool {
|
||||
return s == k
|
||||
})
|
||||
if index >= 0 {
|
||||
foundKeys = append(foundKeys, k)
|
||||
}
|
||||
}
|
||||
return foundKeys
|
||||
}
|
||||
|
||||
func WithDefault[T any](v T) Option {
|
||||
return func(p Params) {
|
||||
p["default"] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Syntactic sugar generic function to get parameter from util.Params.
|
||||
func Get[T any](params Params, key string, opts ...Option) *T {
|
||||
if v, ok := params[key].(T); ok {
|
||||
return &v
|
||||
}
|
||||
if defaultValue, ok := params["default"].(T); ok {
|
||||
return &defaultValue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
92
pkg/util/util.go
Normal file
92
pkg/util/util.go
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Wrapper function to simplify checking if a path exists.
|
||||
func PathExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Wrapper function to simplify checking if a path is a directory.
|
||||
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))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not create new HTTP request: %v", err)
|
||||
}
|
||||
req.Header.Add("User-Agent", "configurator")
|
||||
for k, v := range headers {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not make request: %v", err)
|
||||
}
|
||||
b, err := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not read response body: %v", err)
|
||||
}
|
||||
return res, b, err
|
||||
}
|
||||
|
||||
// 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()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimRight(string(stdout), "\n")
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if condition(e) {
|
||||
f = append(f, e)
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue