Minor changes to util functions

This commit is contained in:
David Allen 2024-09-20 16:56:15 -06:00
parent 601089672c
commit 751a2facdb
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 25 additions and 5 deletions

View file

@ -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)