Skip to content

Commit

Permalink
Merge pull request #5470 from snyk/feat/CLI-509_configurableTmpDir
Browse files Browse the repository at this point in the history
chore(GAF): bump gaf version
  • Loading branch information
PeterSchafer authored Sep 12, 2024
2 parents 42ea0c2 + f0f2d6e commit 0c0d1a1
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 3 additions & 4 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 6 additions & 3 deletions cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cliv2/internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions cliv2/internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
31 changes: 0 additions & 31 deletions cliv2/internal/utils/directories.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion cliv2/pkg/basic_workflows/globalresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0c0d1a1

Please sign in to comment.