Skip to content

Commit

Permalink
chore: upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Dec 25, 2024
1 parent f83131e commit 06934d6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 84 deletions.
105 changes: 57 additions & 48 deletions src/config/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Aliae struct {

type FuncMap []StringFunc
type StringFunc struct {
Name string
F func([]string) (string, error)
Name []byte
F func(string) ([]byte, error)
}

func customUnmarshaler(a *Aliae, b []byte) error {
Expand All @@ -31,6 +31,8 @@ func customUnmarshaler(a *Aliae, b []byte) error {
return err
}

fmt.Println(string(data))

decoder := yaml.NewDecoder(bytes.NewBuffer(data))
if err = decoder.Decode(a); err != nil {
return err
Expand All @@ -41,46 +43,64 @@ func customUnmarshaler(a *Aliae, b []byte) error {

// includeUnmarshaler handles unmarshaling of !include and !include_dir tags
func includeUnmarshaler(b []byte) ([]byte, error) {
s := strings.Split(string(b), "\n")
newline := []byte("\n")

s := bytes.Split(b, newline)

includeFuncMap := FuncMap{
{
Name: "!include_dir",
F: getDirFiles,
Name: []byte("!include_dir"),
F: readDir,
},
{
Name: "!include",
F: getFile,
Name: []byte("!include"),
F: os.ReadFile,
},
}

for i, line := range s {
for _, f := range includeFuncMap {
if !strings.HasPrefix(line, f.Name) {
if !bytes.Contains(line, f.Name) {
continue
}

parts := strings.Fields(line)
if len(parts) < 2 {
parts := bytes.Fields(line)
if len(parts) < 3 {
return nil, fmt.Errorf("invalid %s directive: \n%s", f.Name, line)
}

data, err := f.F(parts[1:])
folder := string(bytes.Join(parts[2:][0:], []byte(" ")))
path, err := validatePath(folder)
if err != nil {
return nil, err
}

s[i] = data
data, err := f.F(path)
if err != nil {
return nil, err
}

splitted := bytes.Split(data, newline)
for i, line := range splitted {
splitted[i] = indent(line)
}

indented := bytes.Join(splitted, newline)

result := append(parts[0][0:], newline...)
result = append(result, indented...)

s[i] = result
break
}
}

returnData := []byte(strings.Join(s, "\n"))
if len(returnData) == len(b) {
return returnData, nil
data := bytes.Join(s, newline)
if len(data) == len(b) {
return data, nil
}

return includeUnmarshaler(returnData)
return includeUnmarshaler(data)
}

func trimQuotes(s string) string {
Expand All @@ -95,60 +115,49 @@ func trimQuotes(s string) string {
return s
}

func getFile(s []string) (string, error) {
// Allows for templating in the file path
file := shell.Template(trimQuotes(strings.Join(s, ""))).Parse().String()
func indent(data []byte) []byte {
newData := make([]byte, len(data)+2)
newData[0] = ' '
newData[1] = ' '

// check if filepath is relative
file, err := relativePath(file)
if err != nil {
return "", err
}
copy(newData[2:], data)

data, err := os.ReadFile(file)
if err != nil {
return "", err
}

return string(data), nil
return newData
}

func getDirFiles(d []string) (string, error) {
// Allows for templating in the directory path
dir := shell.Template(trimQuotes(strings.Join(d, ""))).Parse().String()

// check if filepath is relative
dir, err := relativePath(dir)
if err != nil {
return "", err
}

func readDir(dir string) ([]byte, error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", err
return []byte{}, err
}

var configData strings.Builder
var configData []byte

for _, file := range files {
for i, file := range files {
if !isYAMLExtension(file.Name()) {
continue
}

filePath := filepath.Join(dir, file.Name())
data, err := os.ReadFile(filePath)
if err != nil {
return "", err
continue
}

configData.WriteString("\n")
configData.Write(data)
configData = append(configData, data...)

if i != len(files)-1 {
configData = append(configData, []byte("\n")...)
}
}

return configData.String(), nil
return configData, nil
}

func relativePath(path string) (string, error) {
func validatePath(path string) (string, error) {
// Allows for templating in the file path
path = shell.Template(trimQuotes(path)).Parse().String()

if filepath.IsAbs(path) {
return path, nil
}
Expand Down
37 changes: 10 additions & 27 deletions src/config/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,23 @@ func TestTrimQuotes(t *testing.T) {
{"SingleQuotes", "'test'", "'test'"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := trimQuotes(tt.input)
assert.Equal(t, tt.expected, result)
})
for _, tc := range tests {
result := trimQuotes(tc.input)
assert.Equal(t, tc.expected, result)
}
}

func TestGetFile(t *testing.T) {
t.Run("ValidFile", func(t *testing.T) {
testDirPath := filepath.Join("test", "files", "test.yaml")
absPath, _ := filepath.Abs(testDirPath)
content, err := getFile([]string{absPath})
assert.NoError(t, err)
assert.Equal(t, "it exists", content)
})

t.Run("NonExistentFile", func(t *testing.T) {
_, err := getFile([]string{"path/to/nonexistent/file.txt"})
assert.Error(t, err)
})
}

func TestGetDirFiles(t *testing.T) {
func TestReadDir(t *testing.T) {
t.Run("ValidDir", func(t *testing.T) {
testDirPath := filepath.Join("test", "files")
absPath, _ := filepath.Abs(testDirPath)
content, err := getDirFiles([]string{absPath})
content, err := readDir(absPath)
assert.NoError(t, err)
assert.Equal(t, "\nit exists\nit exists2", content)
assert.Equal(t, []byte("it exists\nit exists2"), content)
})

t.Run("NonExistentDir", func(t *testing.T) {
_, err := getDirFiles([]string{"path/to/nonexistent/dir"})
_, err := readDir("path/to/nonexistent/dir")
assert.Error(t, err)
})
}
Expand All @@ -60,14 +43,14 @@ func TestRelativePath(t *testing.T) {
t.Run("RelativePath", func(t *testing.T) {
absPath, err := filepath.Abs("./test/files")
assert.NoError(t, err)
result, err := relativePath(absPath)
result, err := validatePath(absPath)
assert.NoError(t, err)
assert.Equal(t, absPath, result)
})

t.Run("HttpConfig", func(t *testing.T) {
t.Run("Http config", func(t *testing.T) {
configPathCache = "https://example.com/config.yaml"
_, err := relativePath("path/to/nonex istent/dir")
_, err := validatePath("path/to/nonex istent/dir")
assert.Error(t, err)
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/jandedobbeleer/aliae/src
go 1.22

require (
github.com/goccy/go-yaml v1.14.3
github.com/goccy/go-yaml v1.15.13
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.8.1
)
Expand All @@ -13,7 +13,7 @@ require (
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand All @@ -22,5 +22,5 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.10.0
golang.org/x/sys v0.27.0
golang.org/x/sys v0.28.0
)
12 changes: 6 additions & 6 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/goccy/go-yaml v1.14.3 h1:8tVD+aqqPLWisSEhM+6wWoiURWXCx6BwaTKS6ZeITgM=
github.com/goccy/go-yaml v1.14.3/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/goccy/go-yaml v1.15.13 h1:Xd87Yddmr2rC1SLLTm2MNDcTjeO/GYo0JGiww6gSTDg=
github.com/goccy/go-yaml v1.15.13/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -21,14 +21,14 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo=
github.com/tklauser/numcpus v0.9.0/go.mod h1:SN6Nq1O3VychhC1npsWostA+oW+VOQTxZrS604NSRyI=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit 06934d6

Please sign in to comment.