Skip to content

Commit

Permalink
fix binaries path
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Mar 1, 2024
1 parent 302cc9e commit 57ea7b3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
6 changes: 5 additions & 1 deletion ignite/internal/tools/gen-mig-diffs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func NewRootCmd() *cobra.Command {
if err != nil {
return err
}
defer sFrom.Cleanup()

if err := sFrom.Run(cmd.Context()); err != nil {
return err
}
Expand All @@ -106,6 +108,8 @@ func NewRootCmd() *cobra.Command {
if err != nil {
return err
}
defer sTo.Cleanup()

if err := sTo.Run(cmd.Context()); err != nil {
return err
}
Expand All @@ -124,7 +128,7 @@ func NewRootCmd() *cobra.Command {
if err = diff.SaveDiffs(diffs, output); err != nil {
return errors.Wrap(err, "failed to save diff map")
}
session.Println("Migration diffs generated successfully at", output)
session.Printf("Migration diffs generated successfully at %s\n", output)

return nil
},
Expand Down
67 changes: 64 additions & 3 deletions ignite/internal/tools/gen-mig-diffs/pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repo
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sort"
Expand All @@ -27,6 +28,7 @@ type (
Generator struct {
From, To *semver.Version
source string
binPath string
repo *git.Repository
session *cliui.Session
cleanup bool
Expand All @@ -37,6 +39,7 @@ type (
source string
output string
repoURL string
binPath string
cleanup bool
}
// Options configures the generator.
Expand All @@ -48,6 +51,7 @@ func newOptions() options {
tmpDir := os.TempDir()
return options{
source: "",
binPath: filepath.Join(tmpDir, "bin"),
output: filepath.Join(tmpDir, "migration-source"),
repoURL: defaultRepoURL,
}
Expand Down Expand Up @@ -76,6 +80,13 @@ func WithRepoOutput(output string) Options {
}
}

// WithBinPath set the binary path to build the source.
func WithBinPath(binPath string) Options {
return func(o *options) {
o.binPath = binPath
}
}

// WithCleanup cleanup folders after use.
func WithCleanup() Options {
return func(o *options) {
Expand Down Expand Up @@ -146,12 +157,18 @@ func New(from, to *semver.Version, session *cliui.Session, options ...Options) (
return nil, err
}

binPath, err := filepath.Abs(opts.binPath)
if err != nil {
return nil, err
}

return &Generator{
From: from,
To: to,
source: source,
repo: repo,
session: session,
binPath: binPath,
}, nil
}

Expand Down Expand Up @@ -274,10 +291,15 @@ func (g *Generator) buildIgniteCli(ctx context.Context, ver *semver.Version) (st
return "", errors.Wrap(err, "failed to build ignite cli using make build")
}

binPath := filepath.Join(g.source, defaultBinaryPath)
// Copy the built binary to the binary path.
genBinaryPath := filepath.Join(g.source, defaultBinaryPath)
binPath := filepath.Join(g.binPath, ver.Original(), "ignite")
if err := copyFile(genBinaryPath, binPath); err != nil {
return "", err
}

g.session.StopSpinner()
g.session.EventBus().SendInfo(fmt.Sprintf("Built ignite cli for %s", ver.Original()))
g.session.EventBus().SendInfo(fmt.Sprintf("Built ignite cli for %s at %s", ver.Original(), binPath))

return binPath, nil
}
Expand All @@ -288,7 +310,6 @@ func (g *Generator) checkoutToTag(tag string) error {
if err != nil {
return err
}

// Reset and clean the git directory before the checkout to avoid conflicts.
if err := wt.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
return errors.Wrapf(err, "failed to reset %s", g.source)
Expand All @@ -301,3 +322,43 @@ func (g *Generator) checkoutToTag(tag string) error {
}
return nil
}

// copyFile copy a file to a destination directory. Creates the directory if not exist.
func copyFile(srcPath, dstPath string) error {
dstDir := filepath.Dir(dstPath)
if err := os.RemoveAll(dstDir); err != nil {
return err
}
if err := os.MkdirAll(dstDir, os.ModePerm); err != nil {
return err
}

src, err := os.Open(srcPath)
if err != nil {
return errors.Wrap(err, "failed to open source file")
}
defer src.Close()

dst, err := os.Create(dstPath)
if err != nil {
return errors.Wrap(err, "failed to create destination file")
}
defer dst.Close()

_, err = io.Copy(dst, src)
if err != nil {
return errors.Wrap(err, "failed to copy data: %s")
}
// Sync to ensure data is flushed to disk.
err = dst.Sync()
if err != nil {
return errors.Wrap(err, "failed to sync destination file")
}

// Set executable permissions on the destination file.
err = os.Chmod(dstPath, 0o755)
if err != nil {
return errors.Wrap(err, "failed to set executable permissions")
}
return err
}
46 changes: 3 additions & 43 deletions ignite/internal/tools/gen-mig-diffs/pkg/scaffold/cache.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package scaffold

import (
"io"
"os"
"path/filepath"
"sync"

"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/xos"
)

// cache represents a cache for executed scaffold command.
Expand All @@ -30,7 +30,7 @@ func (c *cache) save(name, path string) error {
defer c.mu.Unlock()

dstPath := filepath.Join(c.cachePath, name)
if err := copyFiles(path, dstPath); err != nil {
if err := xos.CopyFolder(path, dstPath); err != nil {
return err
}

Expand Down Expand Up @@ -70,48 +70,8 @@ func (c *cache) get(name, dstPath string) error {
if err != nil {
return err
}
if err := copyFiles(cachePath, dstPath); err != nil {
if err := xos.CopyFolder(cachePath, dstPath); err != nil {
return errors.Wrapf(err, "error to copy cache from %s to %s", cachePath, dstPath)
}
return nil
}

// copyFiles copy all files from the source path to the destination path.
func copyFiles(srcPath, dstPath string) error {
srcInfo, err := os.Stat(srcPath)
switch {
case os.IsNotExist(err):
return errors.Wrapf(err, "cache %s not exist in the path", srcPath)
case err != nil:
return err
case !srcInfo.IsDir():
return errors.Wrapf(err, "cache %s is not a directory", srcPath)
}

// Walk through the original path and copy all content to the cache path.
return filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(srcPath, path)
if err != nil {
return err
}
dstPath := filepath.Join(dstPath, relPath)
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}
srcFile, err := os.Open(path)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(dstPath)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
})
}
8 changes: 8 additions & 0 deletions ignite/internal/tools/gen-mig-diffs/pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ func (s *Scaffold) Run(ctx context.Context) error {
return nil
}

// Cleanup cleanup all temporary directories.
func (s *Scaffold) Cleanup() error {
if err := os.RemoveAll(s.cachePath); err != nil {
return err
}
return os.RemoveAll(s.Output)
}

func (s *Scaffold) runCommand(ctx context.Context, name string, command Command) error {
path := filepath.Join(s.Output, name)
if command.Prerequisite != "" {
Expand Down

0 comments on commit 57ea7b3

Please sign in to comment.