Skip to content

Commit

Permalink
chore: refactor dir utils to gaf
Browse files Browse the repository at this point in the history
  • Loading branch information
sandor-trombitas committed Sep 10, 2024
1 parent efd1619 commit 8d4d735
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 42 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-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
Expand Down
2 changes: 2 additions & 0 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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 utils.GetTemporaryDirectory(c.CacheDirectory, cliv1.CLIV1Version())
}

func (c *CLI) printVersion() {
Expand Down
2 changes: 1 addition & 1 deletion cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
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 := pkg_utils.GetTemporaryDirectory(cacheDirectory, cliVersion)
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
}
4 changes: 3 additions & 1 deletion cliv2/pkg/basic_workflows/globalresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8d4d735

Please sign in to comment.