Skip to content

Commit

Permalink
add function to check if file/dir exists
Browse files Browse the repository at this point in the history
  • Loading branch information
eveld committed Oct 21, 2024
1 parent 1ecf44f commit 6f8b4ee
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ generate_mocks:

install_local:
go build -ldflags "-X main.version=${git_commit}" -gcflags=all="-N -l" -o bin/jumppad main.go
sudo mv /usr/bin/jumppad /usr/bin/jumppad-old || true
sudo cp bin/jumppad /usr/bin/jumppad
sudo mv /usr/bin/jumppad /usr/local/bin/jumppad-old || true
sudo cp bin/jumppad /usr/local/bin/jumppad

remove_local:
sudo mv /usr/bin/jumppad-old /usr/bin/jumppad || true
sudo mv /usr/local/bin/jumppad-old /usr/local/bin/jumppad || true
28 changes: 12 additions & 16 deletions examples/functions/container.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ resource "container" "consul" {
}

environment = {
file_dir = dir()
env = env("HOME")
k8s_config = k8s_config("dc1")
k8s_config_docker = k8s_config_docker("dc1")
home = home()
shipyard = shipyard()
file = file("./default.vars")
data = data("mine")
docker_ip = docker_ip()
docker_host = docker_host()
shipyard_ip = shipyard_ip()
cluster_api = cluster_api("nomad_cluster.dc1")
cluster_port = cluster_port("nomad_cluster.dc1")
var_len = len(variable.test_var)
os = system("os")
arch = system("arch")
file_dir = dir()
env = env("HOME")
home = home()
file = file("./default.vars")
data = data("mine")
data_with_permissions = data_with_permissions("mine", 755)
docker_ip = docker_ip()
docker_host = docker_host()
var_len = len(variable.test_var)
os = system("os")
arch = system("arch")
exists = exists("file")
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/infinytum/raymond/v2 v2.0.5
github.com/jumppad-labs/connector v0.4.0
github.com/jumppad-labs/gohup v0.4.0
github.com/jumppad-labs/hclconfig v0.25.0
github.com/jumppad-labs/hclconfig v0.25.1-0.20241021153408-472adc840789
github.com/jumppad-labs/plugin-sdk v0.1.1-0.20240306185853-c09f71f81b8a
github.com/kennygrant/sanitize v1.2.4
github.com/mattn/go-isatty v0.0.20
Expand Down Expand Up @@ -265,6 +265,6 @@ replace github.com/charmbracelet/log => github.com/jumppad-labs/log v0.0.0-20240

replace github.com/zclconf/go-cty => github.com/jumppad-labs/go-cty v0.0.0-20230804061424-9e985cb751f6

//replace github.com/jumppad-labs/hclconfig => ../hclconfig/
// replace github.com/jumppad-labs/hclconfig => ../hclconfig/

//replace github.com/jumppad-labs/gohup => ../gohup/
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,8 @@ github.com/jumppad-labs/go-cty v0.0.0-20230804061424-9e985cb751f6 h1:1ADItCWr5pr
github.com/jumppad-labs/go-cty v0.0.0-20230804061424-9e985cb751f6/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
github.com/jumppad-labs/gohup v0.4.0 h1:0OplHvnKnOLkqWm417sRHLSiJ4xGeb8LiSSAJ51QrYg=
github.com/jumppad-labs/gohup v0.4.0/go.mod h1:JYvZnemxJlWDyx8RbDNcCBLZSvIrYlYLnkQqR1BKFW4=
github.com/jumppad-labs/hclconfig v0.24.0 h1:SyMCl2rwLC5o32CCp50fsVbJxyFFCiYoMejvfh9RJKM=
github.com/jumppad-labs/hclconfig v0.24.0/go.mod h1:AOzW0btnKiqUKYVi3ioGzSPNCWsTzsJxKcqzVORccvk=
github.com/jumppad-labs/hclconfig v0.25.0 h1:CRIprG+i+Ol9s2izb6PRrt23kyGYZ1OC/XGFqdI1o/M=
github.com/jumppad-labs/hclconfig v0.25.0/go.mod h1:AOzW0btnKiqUKYVi3ioGzSPNCWsTzsJxKcqzVORccvk=
github.com/jumppad-labs/hclconfig v0.25.1-0.20241021153408-472adc840789 h1:iRef4QLFCuOEI/YsfZIw4tuKMytOrtg4/vSPIIdBGZQ=
github.com/jumppad-labs/hclconfig v0.25.1-0.20241021153408-472adc840789/go.mod h1:AOzW0btnKiqUKYVi3ioGzSPNCWsTzsJxKcqzVORccvk=
github.com/jumppad-labs/log v0.0.0-20240827082827-4404884e31a7 h1:tuoFYWXAqT5BheDlQNumY1DxvkW8bjG9JOzoxpFneZs=
github.com/jumppad-labs/log v0.0.0-20240827082827-4404884e31a7/go.mod h1:S9jhxE2C1+jv2PlLTAow3h+ZILzvXRhd6eBjFAUcfgI=
github.com/jumppad-labs/plugin-sdk v0.1.1-0.20240306185853-c09f71f81b8a h1:fqPvAO1o/6bUIm0fwEbXcUzUCng+GDlLmSMT/5oUJUE=
Expand Down
10 changes: 9 additions & 1 deletion pkg/config/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func customHCLFuncDockerHost() (string, error) {
}

func customHCLFuncDataFolderWithPermissions(name string, permissions int) (string, error) {
if permissions > 0 && permissions < 778 {
if permissions < 0 || permissions > 777 {
return "", fmt.Errorf("permissions must be a three digit number less than 777")
}

Expand All @@ -50,3 +50,11 @@ func customHCLFuncSystem(property string) (string, error) {
return "", fmt.Errorf("unknown system property %s", property)
}
}

func customHCLFuncExists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
return false, nil
}

return true, nil
}
24 changes: 24 additions & 0 deletions pkg/config/functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestExistsFalse(t *testing.T) {
exists, err := customHCLFuncExists("nonexistent")
require.NoError(t, err)
require.Equal(t, false, exists)
}

func TestExistsTrue(t *testing.T) {
file := filepath.Join(t.TempDir(), "testdata")
os.WriteFile(file, []byte("test"), 0644)

exists, err := customHCLFuncExists(file)
require.NoError(t, err)
require.Equal(t, true, exists)
}
1 change: 1 addition & 0 deletions pkg/config/zz_hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewParser(callback hclconfig.WalkCallback, variables map[string]string, var
p.RegisterFunction("data", customHCLFuncDataFolder)
p.RegisterFunction("data_with_permissions", customHCLFuncDataFolderWithPermissions)
p.RegisterFunction("system", customHCLFuncSystem)
p.RegisterFunction("exists", customHCLFuncExists)

return p
}

0 comments on commit 6f8b4ee

Please # to comment.