From 8d4d7356d4abaa2dda2f1888fb8fef858ba40b61 Mon Sep 17 00:00:00 2001 From: Sandor Trombitas Date: Tue, 10 Sep 2024 08:35:14 +0300 Subject: [PATCH] chore: refactor dir utils to gaf --- cliv2/go.mod | 2 +- cliv2/go.sum | 2 ++ cliv2/internal/cliv2/cliv2.go | 7 ++--- cliv2/internal/cliv2/cliv2_test.go | 2 +- cliv2/internal/proxy/proxy.go | 4 +-- cliv2/internal/proxy/proxy_test.go | 3 +- cliv2/internal/utils/directories.go | 31 -------------------- cliv2/pkg/basic_workflows/globalresources.go | 4 ++- 8 files changed, 13 insertions(+), 42 deletions(-) diff --git a/cliv2/go.mod b/cliv2/go.mod index 0c973e0900..42c24a0582 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -17,7 +17,7 @@ require ( github.com/snyk/cli-extension-sbom v0.0.0-20240812130014-3f4e892f15ec github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 github.com/snyk/error-catalog-golang-public v0.0.0-20240809094525-c48d19c27edb - github.com/snyk/go-application-framework v0.0.0-20240905121411-2f34d0529335 + github.com/snyk/go-application-framework v0.0.0-20240909124730-e4ed39418971 github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 github.com/snyk/snyk-iac-capture v0.6.5 github.com/snyk/snyk-ls v0.0.0-20240903085751-fa9fa4069fc2 diff --git a/cliv2/go.sum b/cliv2/go.sum index 366a62eba2..be5b61cc2e 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -751,6 +751,8 @@ github.com/snyk/go-application-framework v0.0.0-20240827093100-f92edcc104ec h1:h github.com/snyk/go-application-framework v0.0.0-20240827093100-f92edcc104ec/go.mod h1:zgYTVG71nX7zTb3ELeRlnwE/uKQxeOyQmAHtg6bC4uU= github.com/snyk/go-application-framework v0.0.0-20240905121411-2f34d0529335 h1:c0qHt7564dZnU53T8fDHipLG1JJnsxAPpDHK9EqBcm0= github.com/snyk/go-application-framework v0.0.0-20240905121411-2f34d0529335/go.mod h1:zgYTVG71nX7zTb3ELeRlnwE/uKQxeOyQmAHtg6bC4uU= +github.com/snyk/go-application-framework v0.0.0-20240909124730-e4ed39418971 h1:9FvSwv7zWY/cM20LBwoTpVg4awBbO28KGfnhWi1I4s4= +github.com/snyk/go-application-framework v0.0.0-20240909124730-e4ed39418971/go.mod h1:zgYTVG71nX7zTb3ELeRlnwE/uKQxeOyQmAHtg6bC4uU= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg= github.com/snyk/policy-engine v0.30.11 h1:wUy5LMar2vccMbNM62MSBRdjAQAhAbIm7aNXXO+g2tk= diff --git a/cliv2/internal/cliv2/cliv2.go b/cliv2/internal/cliv2/cliv2.go index c802d1063a..fed5f0e616 100644 --- a/cliv2/internal/cliv2/cliv2.go +++ b/cliv2/internal/cliv2/cliv2.go @@ -31,7 +31,6 @@ import ( "github.com/snyk/cli/cliv2/internal/embedded" "github.com/snyk/cli/cliv2/internal/embedded/cliv1" "github.com/snyk/cli/cliv2/internal/proxy" - local_utils "github.com/snyk/cli/cliv2/internal/utils" ) type Handler int @@ -93,7 +92,7 @@ func (c *CLI) Init() (err error) { if len(c.CacheDirectory) > 0 { // ensure the specified base cache directory exists, this needs to be done even before acquiring the lock if _, err = os.Stat(c.CacheDirectory); os.IsNotExist(err) { - err = os.MkdirAll(c.CacheDirectory, local_utils.CACHEDIR_PERMISSION) + err = os.MkdirAll(c.CacheDirectory, utils.DIR_PERMISSION) if err != nil { return fmt.Errorf("Cache directory path is invalid: %w", err) } @@ -117,7 +116,7 @@ func (c *CLI) Init() (err error) { c.DebugLogger.Printf("Init-Lock acquired: %v (%s)\n", fileLock.Locked(), lockFileName) // create required cache and temp directories - err = local_utils.CreateAllDirectories(c.CacheDirectory, GetFullVersion()) + err = utils.CreateAllDirectories(c.CacheDirectory, GetFullVersion()) if err != nil { return err } @@ -218,7 +217,7 @@ func (c *CLI) GetBinaryLocation() string { } func (c *CLI) GetTempDir() string { - return local_utils.GetTemporaryDirectory(c.CacheDirectory, cliv1.CLIV1Version()) + return utils.GetTemporaryDirectory(c.CacheDirectory, cliv1.CLIV1Version()) } func (c *CLI) printVersion() { diff --git a/cliv2/internal/cliv2/cliv2_test.go b/cliv2/internal/cliv2/cliv2_test.go index b676a1ea2a..e5eac0b0fe 100644 --- a/cliv2/internal/cliv2/cliv2_test.go +++ b/cliv2/internal/cliv2/cliv2_test.go @@ -14,13 +14,13 @@ import ( "time" "github.com/snyk/go-application-framework/pkg/configuration" + "github.com/snyk/go-application-framework/pkg/utils" cli_errors "github.com/snyk/cli/cliv2/internal/errors" "github.com/snyk/cli/cliv2/internal/cliv2" "github.com/snyk/cli/cliv2/internal/constants" "github.com/snyk/cli/cliv2/internal/proxy" - "github.com/snyk/cli/cliv2/internal/utils" "github.com/stretchr/testify/assert" ) diff --git a/cliv2/internal/proxy/proxy.go b/cliv2/internal/proxy/proxy.go index dd96b6c8e2..97998b89ef 100644 --- a/cliv2/internal/proxy/proxy.go +++ b/cliv2/internal/proxy/proxy.go @@ -71,8 +71,8 @@ func InitCA(config configuration.Configuration, cliVersion string, logger *zerol return nil, err } - tmpDirectory := utils.GetTemporaryDirectory(cacheDirectory, cliVersion) - err = utils.CreateAllDirectories(cacheDirectory, cliVersion) + tmpDirectory := pkg_utils.GetTemporaryDirectory(cacheDirectory, cliVersion) + err = pkg_utils.CreateAllDirectories(cacheDirectory, cliVersion) if err != nil { return nil, err } diff --git a/cliv2/internal/proxy/proxy_test.go b/cliv2/internal/proxy/proxy_test.go index 3f070dadb4..ad70d79f31 100644 --- a/cliv2/internal/proxy/proxy_test.go +++ b/cliv2/internal/proxy/proxy_test.go @@ -21,7 +21,6 @@ import ( "github.com/snyk/cli/cliv2/internal/constants" "github.com/snyk/cli/cliv2/internal/proxy" - "github.com/snyk/cli/cliv2/internal/utils" "github.com/snyk/cli/cliv2/pkg/basic_workflows" ) @@ -71,7 +70,7 @@ func helper_getHttpClient(gateway *proxy.WrapperProxy, useProxyAuth bool) (*http func setup(t *testing.T, baseCache string, version string) configuration.Configuration { t.Helper() - err := utils.CreateAllDirectories(baseCache, version) + err := gafUtils.CreateAllDirectories(baseCache, version) assert.Nil(t, err) config := configuration.NewInMemory() config.Set(configuration.CACHE_PATH, baseCache) diff --git a/cliv2/internal/utils/directories.go b/cliv2/internal/utils/directories.go index bc6a5a598c..97ef16555c 100644 --- a/cliv2/internal/utils/directories.go +++ b/cliv2/internal/utils/directories.go @@ -1,40 +1,9 @@ package utils import ( - "fmt" - "os" "path" - - "github.com/pkg/errors" ) -const CACHEDIR_PERMISSION = 0755 - -// The directory structure used to cache things into -// - Base cache directory (user definable, default depends on OS, exmple: /Users/username/Library/Caches/snyk/) -// |- Version cache directory (example: /Users/username/Library/Caches/snyk/snyk-cli/1.1075.0/) -// |- Temp directory (example: /Users/username/Library/Caches/snyk/snyk-cli/1.1075.0/tmp/) - -func GetTemporaryDirectory(baseCacheDirectory string, versionNumber string) string { - pid := os.Getpid() - return path.Join(GetVersionCacheDirectory(baseCacheDirectory, versionNumber), "tmp", fmt.Sprintf("pid%d", pid)) -} - func GetVersionCacheDirectory(baseCacheDirectory string, versionNumber string) string { return path.Join(baseCacheDirectory, versionNumber) } - -func CreateAllDirectories(baseCacheDirectory string, versionNumber string) error { - directoryList := []string{ - GetTemporaryDirectory(baseCacheDirectory, versionNumber), - } - - for _, dir := range directoryList { - err := os.MkdirAll(dir, CACHEDIR_PERMISSION) - if err != nil { - return errors.Wrap(err, "failed to create all directories.") - } - } - - return nil -} diff --git a/cliv2/pkg/basic_workflows/globalresources.go b/cliv2/pkg/basic_workflows/globalresources.go index 54418572ea..33278f6c97 100644 --- a/cliv2/pkg/basic_workflows/globalresources.go +++ b/cliv2/pkg/basic_workflows/globalresources.go @@ -9,7 +9,9 @@ import ( "github.com/rs/zerolog" "github.com/snyk/go-application-framework/pkg/configuration" + gaf_utils "github.com/snyk/go-application-framework/pkg/utils" "github.com/snyk/go-application-framework/pkg/workflow" + "github.com/spf13/pflag" "github.com/snyk/cli/cliv2/internal/cliv2" @@ -95,7 +97,7 @@ func GetGlobalCertAuthority(config configuration.Configuration, debugLogger *zer } func CleanupGlobalTempDirectory(config configuration.Configuration, debugLogger *zerolog.Logger) { - tmpDirectory := utils.GetTemporaryDirectory(config.GetString(configuration.CACHE_PATH), cliv2.GetFullVersion()) + tmpDirectory := gaf_utils.GetTemporaryDirectory(config.GetString(configuration.CACHE_PATH), cliv2.GetFullVersion()) err := os.RemoveAll(tmpDirectory) if err != nil { debugLogger.Print("Failed to delete temporary directory: ", tmpDirectory)