From b6da912b1ee74572c4eda6cfdba86f0359625df7 Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Thu, 5 Sep 2024 14:13:22 +0100 Subject: [PATCH] chore: bump gaf to migrate temp dir --- cliv2/go.mod | 2 +- cliv2/go.sum | 4 +-- cliv2/internal/cliv2/cliv2.go | 7 ++--- cliv2/internal/cliv2/cliv2_test.go | 9 ++++-- 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 | 3 +- 8 files changed, 17 insertions(+), 46 deletions(-) diff --git a/cliv2/go.mod b/cliv2/go.mod index 3c49f8dd44..8ba253c413 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -17,7 +17,7 @@ require ( github.com/snyk/cli-extension-sbom v0.0.0-20240820111700-68258cba52c7 github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7 github.com/snyk/error-catalog-golang-public v0.0.0-20240809094525-c48d19c27edb - github.com/snyk/go-application-framework v0.0.0-20240827093100-f92edcc104ec + 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 7453dd9e29..e63b36f1b1 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -747,8 +747,8 @@ github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7 h1:Zn5BcV76oFAb github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM= github.com/snyk/error-catalog-golang-public v0.0.0-20240809094525-c48d19c27edb h1:w9tJhpTFxWqAhLeraGsMExDjGK9x5Dwj1NRFwb+t+QE= github.com/snyk/error-catalog-golang-public v0.0.0-20240809094525-c48d19c27edb/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4= -github.com/snyk/go-application-framework v0.0.0-20240827093100-f92edcc104ec h1:hqrcfg/+5vz/svcws6B42J/5AurXpYCpeRimMVkgJtY= -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-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..c3abee7125 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 c.globalConfig.GetString(configuration.TEMP_DIR_PATH) } func (c *CLI) printVersion() { diff --git a/cliv2/internal/cliv2/cliv2_test.go b/cliv2/internal/cliv2/cliv2_test.go index b676a1ea2a..96a40bdaab 100644 --- a/cliv2/internal/cliv2/cliv2_test.go +++ b/cliv2/internal/cliv2/cliv2_test.go @@ -13,14 +13,15 @@ import ( "testing" "time" + "github.com/snyk/go-application-framework/pkg/app" "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" ) @@ -397,7 +398,8 @@ func Test_executeUnknownCommand(t *testing.T) { func Test_clearCache(t *testing.T) { cacheDir := getCacheDir(t) - config := configuration.NewInMemory() + engine := app.CreateAppEngine() + config := engine.GetConfiguration() config.Set(configuration.CACHE_PATH, cacheDir) // create instance under test @@ -432,7 +434,8 @@ func Test_clearCache(t *testing.T) { func Test_clearCacheBigCache(t *testing.T) { cacheDir := getCacheDir(t) - config := configuration.NewInMemory() + engine := app.CreateAppEngine() + config := engine.GetConfiguration() config.Set(configuration.CACHE_PATH, cacheDir) // create instance under test diff --git a/cliv2/internal/proxy/proxy.go b/cliv2/internal/proxy/proxy.go index dd96b6c8e2..9d96a49299 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 := config.GetString(configuration.TEMP_DIR_PATH) + 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..aeda99f4c3 100644 --- a/cliv2/pkg/basic_workflows/globalresources.go +++ b/cliv2/pkg/basic_workflows/globalresources.go @@ -10,6 +10,7 @@ import ( "github.com/rs/zerolog" "github.com/snyk/go-application-framework/pkg/configuration" "github.com/snyk/go-application-framework/pkg/workflow" + "github.com/spf13/pflag" "github.com/snyk/cli/cliv2/internal/cliv2" @@ -95,7 +96,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 := config.GetString(configuration.TEMP_DIR_PATH) err := os.RemoveAll(tmpDirectory) if err != nil { debugLogger.Print("Failed to delete temporary directory: ", tmpDirectory)