Skip to content

Commit

Permalink
fix: --distro-patches not accepting relative paths + does not clear h…
Browse files Browse the repository at this point in the history
…is cache on error
  • Loading branch information
Alessio Pragliola committed May 3, 2024
1 parent 379b22c commit 590c97b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
12 changes: 10 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
logrus.Info("Dry run mode enabled, no changes will be applied")
}

absDistroPatchesLocation, err := filepath.Abs(flags.DistroPatchesLocation)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting absolute path of distro patches location: %w", err)
}

var distrodl *distribution.Downloader

// Init first half of collaborators.
Expand All @@ -112,9 +120,9 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, flags.BinPath, flags.FuryctlPath, flags.VpnAutoConnect)

if flags.DistroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol, flags.DistroPatchesLocation)
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol, absDistroPatchesLocation)
} else {
distrodl = distribution.NewDownloader(client, flags.GitProtocol, flags.DistroPatchesLocation)
distrodl = distribution.NewDownloader(client, flags.GitProtocol, absDistroPatchesLocation)
}

// Init packages.
Expand Down
12 changes: 8 additions & 4 deletions internal/distribution/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func (d *Downloader) applyCustomCompatibilityPatches(kfdManifest config.KFD, dst
return fmt.Errorf("error creating temp dir: %w", err)
}

if err := d.client.ClearItem(d.customDistroPatchesPath); err != nil {
return fmt.Errorf("%w '%s': %w", ErrCannotDownloadDistribution, d.customDistroPatchesPath, err)
}

if err := d.client.Download(d.customDistroPatchesPath, tmpDir); err != nil {
return fmt.Errorf("%w '%s': %w", ErrDownloadingFolder, d.customDistroPatchesPath, err)
}
Expand All @@ -234,20 +238,20 @@ func (d *Downloader) applyCustomCompatibilityPatches(kfdManifest config.KFD, dst
return nil
}

return fmt.Errorf("error getting custom distro patches info: %w", err)
return fmt.Errorf("error getting custom distribution patches directory info: %w", err)
}

if !info.IsDir() {
return fmt.Errorf("custom distro patches is not a directory: %w", err)
return fmt.Errorf("custom distribution patches location is not a directory: %w", err)
}

fsPatches, err := fs.Sub(os.DirFS(patchesPath), ".")
if err != nil {
return fmt.Errorf("error reading custom distro patches: %w", err)
return fmt.Errorf("error reading custom distribution patches directory: %w", err)
}

if err := iox.CopyRecursive(fsPatches, dst); err != nil {
return fmt.Errorf("error copying custom distro patches files: %w", err)
return fmt.Errorf("error copying custom distribution patches directory files: %w", err)
}

return nil
Expand Down
11 changes: 11 additions & 0 deletions internal/x/net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
type Client interface {
Download(src, dst string) error
Clear() error
ClearItem(src string) error
}

func WithLocalCache(c Client, dir string) Client {
Expand All @@ -51,6 +52,16 @@ func (d *LocalCacheClientDecorator) Clear() error {
return nil
}

func (d *LocalCacheClientDecorator) ClearItem(src string) error {
key := d.getKeyFromURL(src)

if err := os.RemoveAll(key); err != nil {
return fmt.Errorf("%w: %w", ErrCannotClearCache, err)
}

return nil
}

func (d *LocalCacheClientDecorator) Download(src, dst string) error {
csrc := URLPrefixRegexp.ReplaceAllString(src, "")

Expand Down
4 changes: 4 additions & 0 deletions internal/x/net/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (f *FakeClient) Clear() error {
return nil
}

func (f *FakeClient) ClearItem(src string) error {
return nil
}

func (f *FakeClient) Download(src, dst string) error {
switch src {
case distroHTTPSURL, distroSSHURL:
Expand Down
4 changes: 4 additions & 0 deletions internal/x/net/hashicorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (*GoGetterClient) Clear() error {
return nil
}

func (*GoGetterClient) ClearItem(src string) error {
return nil
}

func (g *GoGetterClient) Download(src, dst string) error {
protocols := []string{""}
if !g.URLHasForcedProtocol(src) {
Expand Down

0 comments on commit 590c97b

Please sign in to comment.