diff --git a/docker/Dockerfile.linux.arm64 b/docker/Dockerfile.linux.arm64 index 7978c3c..4f52c13 100644 --- a/docker/Dockerfile.linux.arm64 +++ b/docker/Dockerfile.linux.arm64 @@ -2,6 +2,8 @@ FROM alpine:latest as base WORKDIR /app -COPY ./drone-helm /app/ +ADD ./drone-helm /bin/ -CMD ["/app/drone-helm"] \ No newline at end of file +RUN chmod +x /bin/drone-helm + +ENTRYPOINT ["/bin/drone-helm"] \ No newline at end of file diff --git a/go.mod b/go.mod index 57eee20..987afd8 100644 --- a/go.mod +++ b/go.mod @@ -86,6 +86,7 @@ require ( github.com/opencontainers/image-spec v1.1.0-rc5 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.16.0 // indirect github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.44.0 // indirect @@ -97,6 +98,8 @@ require ( github.com/spf13/cast v1.5.0 // indirect github.com/spf13/cobra v1.8.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xeipuuv/gojsonschema v1.2.0 // indirect diff --git a/go.sum b/go.sum index 6d987ee..e074dd2 100644 --- a/go.sum +++ b/go.sum @@ -239,6 +239,7 @@ github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -279,6 +280,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -287,6 +289,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/main.go b/main.go index 8d8e27e..98bc284 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,9 @@ func main() { chartPath := os.Getenv("PLUGIN_CHART_PATH") namespace := os.Getenv("PLUGIN_REGISTRY_NAMESPACE") - if (registryUrl == "") || (username == "") || (token == "") || (namespace == "") || (chartPath == "") { - fmt.Println("Missing required environment variables") + err := verifyEnvVars() + if err != nil { + fmt.Println(err.Error()) os.Exit(1) } @@ -82,3 +83,17 @@ func main() { fmt.Printf("Successfully pushed chart to %s", ociURL) } + +func verifyEnvVars() error { + registryUrl := os.Getenv("PLUGIN_REGISTRY_URL") + username := os.Getenv("PLUGIN_REGISTRY_USERNAME") + token := os.Getenv("PLUGIN_REGISTRY_PASSWORD") + chartPath := os.Getenv("PLUGIN_CHART_PATH") + namespace := os.Getenv("PLUGIN_REGISTRY_NAMESPACE") + + if (registryUrl == "") || (username == "") || (token == "") || (namespace == "") || (chartPath == "") { + return fmt.Errorf("required environment variables not set") + } + + return nil +} diff --git a/main_test.go b/main_test.go index d076a02..115e8c5 100644 --- a/main_test.go +++ b/main_test.go @@ -5,12 +5,63 @@ import ( "testing" ) -func TestEnvironmentVariables(t *testing.T) { - os.Setenv("PLUGIN_REGISTRY_URL", "") - // Set other required env variables to non-empty values +func TestMain_EnvVarsNotSet(t *testing.T) { + // Save current environment variables + originalRegistryUrl := os.Getenv("PLUGIN_REGISTRY_URL") + originalUsername := os.Getenv("PLUGIN_REGISTRY_USERNAME") + originalToken := os.Getenv("PLUGIN_REGISTRY_PASSWORD") + originalChartPath := os.Getenv("PLUGIN_CHART_PATH") + originalNamespace := os.Getenv("PLUGIN_REGISTRY_NAMESPACE") - if os.Getenv("PLUGIN_REGISTRY_URL") == "" { - t.Error("Expected an error due to missing PLUGIN_REGISTRY_URL") + // Clear environment variables + os.Setenv("PLUGIN_REGISTRY_URL", "") + os.Setenv("PLUGIN_REGISTRY_USERNAME", "") + os.Setenv("PLUGIN_REGISTRY_PASSWORD", "") + os.Setenv("PLUGIN_CHART_PATH", "") + os.Setenv("PLUGIN_REGISTRY_NAMESPACE", "") + + defer func() { + // Restore original environment variables + os.Setenv("PLUGIN_REGISTRY_URL", originalRegistryUrl) + os.Setenv("PLUGIN_REGISTRY_USERNAME", originalUsername) + os.Setenv("PLUGIN_REGISTRY_PASSWORD", originalToken) + os.Setenv("PLUGIN_CHART_PATH", originalChartPath) + os.Setenv("PLUGIN_REGISTRY_NAMESPACE", originalNamespace) + }() + + err := verifyEnvVars() + if err == nil { + t.Error("Expected error, but got nil") + } + +} + +func TestMain_EnvVarsSet(t *testing.T) { + // Save current environment variables + originalRegistryUrl := os.Getenv("PLUGIN_REGISTRY_URL") + originalUsername := os.Getenv("PLUGIN_REGISTRY_USERNAME") + originalToken := os.Getenv("PLUGIN_REGISTRY_PASSWORD") + originalChartPath := os.Getenv("PLUGIN_CHART_PATH") + originalNamespace := os.Getenv("PLUGIN_REGISTRY_NAMESPACE") + + // Set environment variables + os.Setenv("PLUGIN_REGISTRY_URL", "https://registry.example.com") + os.Setenv("PLUGIN_REGISTRY_USERNAME", "username") + os.Setenv("PLUGIN_REGISTRY_PASSWORD", "token") + os.Setenv("PLUGIN_CHART_PATH", "charts") + os.Setenv("PLUGIN_REGISTRY_NAMESPACE", "namespace") + + defer func() { + // Restore original environment variables + os.Setenv("PLUGIN_REGISTRY_URL", originalRegistryUrl) + os.Setenv("PLUGIN_REGISTRY_USERNAME", originalUsername) + os.Setenv("PLUGIN_REGISTRY_PASSWORD", originalToken) + os.Setenv("PLUGIN_CHART_PATH", originalChartPath) + os.Setenv("PLUGIN_REGISTRY_NAMESPACE", originalNamespace) + }() + + err := verifyEnvVars() + if err != nil { + t.Errorf("Expected nil, but got %v", err) } - // Repeat for other variables }