Skip to content

Commit

Permalink
feat: move binaries and cache to outdir if present (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Pragliola authored and Alessio Pragliola committed May 3, 2024
1 parent 043f99a commit 5a7825a
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 53 deletions.
7 changes: 4 additions & 3 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
return err
}

outDir := flags.Outdir

// Get home dir.
logrus.Debug("Getting Home Directory Path...")
outDir := flags.Outdir

homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -106,7 +107,7 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, flags.BinPath, flags.FuryctlPath, flags.VpnAutoConnect)

if flags.DistroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, flags.GitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol)
} else {
distrodl = distribution.NewDownloader(client, flags.GitProtocol)
}
Expand Down Expand Up @@ -142,7 +143,7 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
basePath := filepath.Join(outDir, ".furyctl", res.MinimalConf.Metadata.Name)

// Init second half of collaborators.
depsdl := dependencies.NewCachingDownloader(client, basePath, flags.BinPath, flags.GitProtocol)
depsdl := dependencies.NewCachingDownloader(client, outDir, basePath, flags.BinPath, flags.GitProtocol)

// Validate the furyctl.yaml file.
logrus.Info("Validating configuration file...")
Expand Down
19 changes: 18 additions & 1 deletion cmd/create/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
},
}

outDir, err := cmdutil.StringFlag(cmd, "outdir", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: outdir", ErrParsingFlag)
}

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
}

if outDir == "" {
outDir = homeDir
}

var distrodl *distribution.Downloader

// Init collaborators.
Expand All @@ -113,7 +130,7 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, "", "", false)

if distroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, typedGitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, typedGitProtocol)
} else {
distrodl = distribution.NewDownloader(client, typedGitProtocol)
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/delete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func NewClusterCmd(tracker *analytics.Tracker) *cobra.Command {
}

// Init paths.
logrus.Debug("Getting Home Directory Path...")

outDir := flags.Outdir

logrus.Debug("Getting Home Directory Path...")

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
Expand Down Expand Up @@ -98,7 +101,7 @@ func NewClusterCmd(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, flags.BinPath, flags.FuryctlPath, flags.VpnAutoConnect)

if flags.DistroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, flags.GitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol)
} else {
distrodl = distribution.NewDownloader(client, flags.GitProtocol)
}
Expand Down Expand Up @@ -135,7 +138,7 @@ func NewClusterCmd(tracker *analytics.Tracker) *cobra.Command {
basePath := filepath.Join(outDir, ".furyctl", res.MinimalConf.Metadata.Name)

// Init second half of collaborators.
depsdl := dependencies.NewCachingDownloader(client, basePath, flags.BinPath, flags.GitProtocol)
depsdl := dependencies.NewCachingDownloader(client, outDir, basePath, flags.BinPath, flags.GitProtocol)

// Validate the furyctl.yaml file.
logrus.Info("Validating configuration file...")
Expand Down
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewDiffCommand(tracker *analytics.Tracker) *cobra.Command {
client := netx.NewGoGetterClient()

if flags.DistroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, flags.GitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol)
} else {
distrodl = distribution.NewDownloader(client, flags.GitProtocol)
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/download/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ func NewDependenciesCmd(tracker *analytics.Tracker) *cobra.Command {
binPath := cmdutil.StringFlagOptional(cmd, "bin-path")

// Init paths.
logrus.Debug("Getting Home Directory Path...")

outDir, err := cmdutil.StringFlag(cmd, "outdir", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: outdir", ErrParsingFlag)
}

logrus.Debug("Getting Home Directory Path...")

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
Expand All @@ -89,7 +92,7 @@ func NewDependenciesCmd(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, binPath, furyctlPath, false)

if distroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, typedGitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, typedGitProtocol)
} else {
distrodl = distribution.NewDownloader(client, typedGitProtocol)
}
Expand Down Expand Up @@ -117,7 +120,7 @@ func NewDependenciesCmd(tracker *analytics.Tracker) *cobra.Command {

basePath := filepath.Join(outDir, ".furyctl", dres.MinimalConf.Metadata.Name)

depsdl := dependencies.NewCachingDownloader(client, basePath, binPath, typedGitProtocol)
depsdl := dependencies.NewCachingDownloader(client, outDir, basePath, binPath, typedGitProtocol)

logrus.Info("Downloading dependencies...")

Expand Down
21 changes: 18 additions & 3 deletions cmd/dump/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,31 @@ The generated folder will be created starting from a provided templates folder a
return fmt.Errorf("error: %w", err)
}

outDir := flags.Outdir

// Get home dir.
logrus.Debug("Getting Home Directory Path...")

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
}

if outDir == "" {
outDir = homeDir
}

var distrodl *distribution.Downloader

client := netx.NewGoGetterClient()
executor := execx.NewStdExecutor()
depsvl := dependencies.NewValidator(executor, "", absFuryctlPath, false)

if flags.DistroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, flags.GitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol)
} else {
distrodl = distribution.NewDownloader(client, flags.GitProtocol)
}
Expand Down Expand Up @@ -116,8 +133,6 @@ The generated folder will be created starting from a provided templates folder a
return fmt.Errorf("%s - %w", absFuryctlPath, err)
}

outDir := flags.Outdir

currentDir, err := os.Getwd()
if err != nil {
cmdEvent.AddErrorMessage(err)
Expand Down
20 changes: 19 additions & 1 deletion cmd/validate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package validate
import (
"errors"
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,19 +53,36 @@ func NewConfigCmd(tracker *analytics.Tracker) *cobra.Command {
return fmt.Errorf("%w: git-protocol", ErrParsingFlag)
}

outDir, err := cmdutil.StringFlag(cmd, "outdir", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: outdir", ErrParsingFlag)
}

typedGitProtocol, err := git.NewProtocol(gitProtocol)
if err != nil {
return fmt.Errorf("%w: %w", ErrParsingFlag, err)
}

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
}

if outDir == "" {
outDir = homeDir
}

var distrodl *distribution.Downloader

client := netx.NewGoGetterClient()
executor := execx.NewStdExecutor()
depsvl := dependencies.NewValidator(executor, "", furyctlPath, false)

if distroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, typedGitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, typedGitProtocol)
} else {
distrodl = distribution.NewDownloader(client, typedGitProtocol)
}
Expand Down
36 changes: 18 additions & 18 deletions cmd/validate/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,27 @@ func NewDependenciesCmd(tracker *analytics.Tracker) *cobra.Command {
return fmt.Errorf("%w: distro-location", ErrParsingFlag)
}

binPath := cobrax.Flag[string](cmd, "bin-path")
if binPath == "" {
// Init paths.
logrus.Debug("Getting Home Directory Path...")
outDir, err := cmdutil.StringFlag(cmd, "outdir", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: outdir", ErrParsingFlag)
}
// Init paths.
logrus.Debug("Getting Home Directory Path...")
outDir, err := cmdutil.StringFlag(cmd, "outdir", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: outdir", ErrParsingFlag)
}

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)
homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
}
return fmt.Errorf("error while getting user home directory: %w", err)
}

if outDir == "" {
outDir = homeDir
}
if outDir == "" {
outDir = homeDir
}

binPath := cobrax.Flag[string](cmd, "bin-path")
if binPath == "" {
binPath = filepath.Join(outDir, ".furyctl", "bin")
}

Expand All @@ -87,7 +87,7 @@ func NewDependenciesCmd(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, "", furyctlPath, false)

if distroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, typedGitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, typedGitProtocol)
} else {
distrodl = distribution.NewDownloader(client, typedGitProtocol)
}
Expand Down
10 changes: 8 additions & 2 deletions internal/dependencies/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ var (
ErrModuleNotFound = errors.New("module not found")
)

func NewCachingDownloader(client netx.Client, basePath, binPath string, gitProtocol git.Protocol) *Downloader {
return NewDownloader(netx.WithLocalCache(client, netx.GetCacheFolder()), basePath, binPath, gitProtocol)
func NewCachingDownloader(client netx.Client, outDir, basePath, binPath string, gitProtocol git.Protocol) *Downloader {
return NewDownloader(netx.WithLocalCache(
client,
filepath.Join(outDir, ".furyctl", "cache")),
basePath,
binPath,
gitProtocol,
)
}

func NewDownloader(client netx.Client, basePath, binPath string, gitProtocol git.Protocol) *Downloader {
Expand Down
4 changes: 2 additions & 2 deletions internal/distribution/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type DownloadResult struct {
DistroManifest config.KFD
}

func NewCachingDownloader(client netx.Client, gitProtocol git.Protocol) *Downloader {
return NewDownloader(netx.WithLocalCache(client, netx.GetCacheFolder()), gitProtocol)
func NewCachingDownloader(client netx.Client, outDir string, gitProtocol git.Protocol) *Downloader {
return NewDownloader(netx.WithLocalCache(client, filepath.Join(outDir, ".furyctl", "cache")), gitProtocol)
}

func NewDownloader(client netx.Client, gitProtocol git.Protocol) *Downloader {
Expand Down
9 changes: 0 additions & 9 deletions internal/x/net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ var (
URLPrefixRegexp = regexp.MustCompile(`^[A-z0-9]+::`)
)

func GetCacheFolder() string {
hd, err := os.UserHomeDir()
if err != nil {
hd = os.TempDir()
}

return filepath.Join(hd, ".furyctl", "cache")
}

type Client interface {
Download(src, dst string) error
}
Expand Down
2 changes: 1 addition & 1 deletion test/expensive/ekscluster/furyctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
}

CreateClusterTest = func(state *ContextState) {
dlRes := DownloadFuryDistribution(state.FuryctlYaml)
dlRes := DownloadFuryDistribution(state.TestDir, state.FuryctlYaml)

tfPlanPath := path.Join(
state.TestDir,
Expand Down
2 changes: 1 addition & 1 deletion test/expensive/kfddistribution/furyctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var (

CreateClusterTestFunc = func(state *distroContextState, phase string) func() {
return func() {
dlRes := DownloadFuryDistribution(state.FuryctlYaml)
dlRes := DownloadFuryDistribution(state.TestDir, state.FuryctlYaml)

kubectlPath := DownloadKubectl(dlRes.DistroManifest.Tools.Common.Kubectl.Version)

Expand Down
6 changes: 3 additions & 3 deletions test/expensive/onpremises/furyctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ var (
InjectNodesData("", "", "", ""),
)

dlRes := DownloadFuryDistribution(ctxState.FuryctlYaml)
dlRes := DownloadFuryDistribution(state.TestDir, ctxState.FuryctlYaml)

terraformBinPath := DownloadTerraform(dlRes.DistroManifest.Tools.Common.Terraform.Version)

Expand Down Expand Up @@ -448,7 +448,7 @@ var (

CreateClusterTestFunc = func(state *onPremContextState) func() {
return func() {
dlRes := DownloadFuryDistribution(state.FuryctlYaml)
dlRes := DownloadFuryDistribution(state.TestDir, state.FuryctlYaml)

kubectlPath := DownloadKubectl(dlRes.DistroManifest.Tools.Common.Kubectl.Version)

Expand Down Expand Up @@ -509,7 +509,7 @@ var (

CreateClusterPhaseDistributionTestFunc = func(state *onPremContextState) func() {
return func() {
dlRes := DownloadFuryDistribution(state.FuryctlYaml)
dlRes := DownloadFuryDistribution(state.TestDir, state.FuryctlYaml)

kubectlPath := DownloadKubectl(dlRes.DistroManifest.Tools.Common.Kubectl.Version)

Expand Down
4 changes: 2 additions & 2 deletions test/utils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func CompileFuryctl(outputPath string) func() {
}
}

func DownloadFuryDistribution(furyctlConfPath string) distribution.DownloadResult {
distrodl := distribution.NewCachingDownloader(netx.NewGoGetterClient(), git.ProtocolSSH)
func DownloadFuryDistribution(outDir, furyctlConfPath string) distribution.DownloadResult {
distrodl := distribution.NewCachingDownloader(netx.NewGoGetterClient(), outDir, git.ProtocolSSH)

return Must1(distrodl.Download("", furyctlConfPath))
}
Expand Down

0 comments on commit 5a7825a

Please sign in to comment.