Skip to content

Commit

Permalink
Implemented post commands (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h authored Nov 24, 2023
1 parent e8ef261 commit 7cadd65
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 70 deletions.
5 changes: 5 additions & 0 deletions Giltfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ repositories:
dstFile: /tmp/library/neutron_router.py
- src: tests
dstDir: /tmp/tests
commands:
- cmd: touch
args: post-command-1
- cmd: touch
args: post-command-2
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ This project is a port of [Gilt][], it is not 100% compatible with the python
version, and aims to correct poor decisions made in the python version of
Gilt.

This version of Gilt does not provide built in branches, tags, and post
commands unlike our python friend. However, those features will be added
soon enough.
This version of Gilt does not handle branches and tags unlike our python
friend. However, those features will be added soon.

## Installation

Expand Down Expand Up @@ -63,6 +62,8 @@ repositories:
dstFile: library/neutron_router.py
- src: tests
dstDir: tests
commands:
- cmd: ansible-playbook -i, playbook.yml
```
### Env Vars
Expand Down
27 changes: 25 additions & 2 deletions cmd/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,29 @@ import (
giltpath "github.com/retr0h/go-gilt/internal/path"
)

// getGiltDir create the GiltDir if it doesn't exist.
func getGiltDir() (string, error) {
dir, err := giltpath.ExpandUser(appConfig.GiltDir)
if err != nil {
return "", err
}

if err := appFs.MkdirAll(dir, 0o755); err != nil {
return "", err
}

return dir, nil
}

// withLock is a convenience function to create a lock, execute a function while
// holding that lock, and then release the lock on completion.
func withLock(fn func() error) error {
expandedLockDir, err := giltpath.ExpandUser(appConfig.GiltDir)
lockDir, err := getGiltDir()
if err != nil {
return err
}
lockFile := filepath.Join(expandedLockDir, "gilt.lock")

lockFile := filepath.Join(lockDir, "gilt.lock")
logger.Info(
"acquiring lock",
slog.String("lockfile", lockFile),
Expand Down Expand Up @@ -70,11 +84,20 @@ func logRepositoriesGroup() []any {
)
sourceGroups = append(sourceGroups, group)
}
var cmdGroups []any
for i, s := range repo.Commands {
group := slog.Group(strconv.Itoa(i),
slog.String("Cmd", s.Cmd),
)
cmdGroups = append(cmdGroups, group)
}

group := slog.Group(strconv.Itoa(i),
slog.String("Git", repo.Git),
slog.String("Version", repo.Version),
slog.String("DstDir", repo.DstDir),
slog.Group("Sources", sourceGroups...),
slog.Group("Commands", cmdGroups...),
)
logGroups = append(logGroups, group)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
repos internal.RepositoriesManager
logger *slog.Logger
appConfig config.Repositories
appFs afero.Fs
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -129,7 +130,7 @@ func initConfig() {
os.Exit(1)
}

appFs := afero.NewOsFs()
appFs = afero.NewOsFs()

repoManager := repository.NewCopy(
appFs,
Expand Down Expand Up @@ -157,6 +158,7 @@ func initConfig() {
g,
logger,
),
execManager,
logger,
)
}
14 changes: 11 additions & 3 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Repositories struct {
Repositories []Repository `mapstruture:"repositories"`
}

// Sources mapping of files and/or directories needing copied.
type Sources struct {
// Source mapping of files and/or directories needing copied.
type Source struct {
// Src source file or directory to copy.
Src string `mapstructure:"src"`
// DstFile destination of file copy.
Expand All @@ -42,6 +42,12 @@ type Sources struct {
DstDir string `mapstructure:"dstDir"`
}

// Command command to execute.
type Command struct {
Cmd string `mapstructure:"cmd"`
Args []string `mapstructure:"args"`
}

// Repository contains the repository's details for cloning.
type Repository struct {
// Git url of Git repository to clone.
Expand All @@ -51,5 +57,7 @@ type Repository struct {
// DstDir destination directory to copy clone to.
DstDir string `mapstructure:"dstDir"`
// Sources containing files and/or directories to copy.
Sources []Sources `mapstructure:"sources"`
Sources []Source `mapstructure:"sources"`
// Commands commands to execute on Repository.
Commands []Command `mapstructure:"commands"`
}
2 changes: 1 addition & 1 deletion internal/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ package internal

// ExecManager manager responsible for exec operations.
type ExecManager interface {
RunCmd(name string, args ...string) error
RunCmd(name string, args []string) error
}
2 changes: 1 addition & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func New(
// Yeah, yeah, yeah, I know I cheated by using Exec in this package.
func (e *Exec) RunCmd(
name string,
args ...string,
args []string,
) error {
cmd := exec.Command(name, args...)

Expand Down
13 changes: 4 additions & 9 deletions internal/exec/exec_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/exec/exec_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (suite *ExecManagerPublicTestSuite) NewTestExecManager(
func (suite *ExecManagerPublicTestSuite) TestRunCmdOk() {
em := suite.NewTestExecManager(false)

err := em.RunCmd("ls")
err := em.RunCmd("ls", []string{})
assert.NoError(suite.T(), err)
}

Expand All @@ -59,14 +59,14 @@ func (suite *ExecManagerPublicTestSuite) TestRunCmdWithDebug() {

em := suite.NewTestExecManager(true)

err := em.RunCmd("echo", "-n", "foo")
err := em.RunCmd("echo", []string{"-n", "foo"})
assert.NoError(suite.T(), err)
}

func (suite *ExecManagerPublicTestSuite) TestRunCmdReturnsError() {
em := suite.NewTestExecManager(false)

err := em.RunCmd("invalid", "foo")
err := em.RunCmd("invalid", []string{"foo"})
assert.Error(suite.T(), err)
assert.Contains(suite.T(), err.Error(), "not found")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ func (g *Git) Clone(
cloneDir string,
) error {
// return g.execManager.Clone(gitURL, cloneDir)
return g.execManager.RunCmd("git", "clone", gitURL, cloneDir)
return g.execManager.RunCmd("git", []string{"clone", gitURL, cloneDir})
}

// Reset to the given git version.
func (g *Git) Reset(
cloneDir string,
gitVersion string,
) error {
return g.execManager.RunCmd("git", "-C", cloneDir, "reset", "--hard", gitVersion)
return g.execManager.RunCmd("git", []string{"-C", cloneDir, "reset", "--hard", gitVersion})
}

// CheckoutIndex checkout Repository.Git to Repository.DstDir.
Expand Down Expand Up @@ -91,5 +91,5 @@ func (g *Git) CheckoutIndex(
dst + string(os.PathSeparator),
}

return g.execManager.RunCmd("git", cmdArgs...)
return g.execManager.RunCmd("git", cmdArgs)
}
7 changes: 5 additions & 2 deletions internal/git/git_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (suite *GitManagerPublicTestSuite) SetupTest() {
}

func (suite *GitManagerPublicTestSuite) TestCloneOk() {
suite.mockExec.EXPECT().RunCmd("git", "clone", suite.gitURL, suite.cloneDir).Return(nil)
suite.mockExec.EXPECT().
RunCmd("git", []string{"clone", suite.gitURL, suite.cloneDir}).
Return(nil)

err := suite.gm.Clone(suite.gitURL, suite.cloneDir)
assert.NoError(suite.T(), err)
Expand All @@ -88,7 +90,8 @@ func (suite *GitManagerPublicTestSuite) TestCloneReturnsError() {
}

func (suite *GitManagerPublicTestSuite) TestResetOk() {
suite.mockExec.EXPECT().RunCmd("git", "-C", suite.cloneDir, "reset", "--hard", suite.gitVersion)
suite.mockExec.EXPECT().
RunCmd("git", []string{"-C", suite.cloneDir, "reset", "--hard", suite.gitVersion})

err := suite.gm.Reset(suite.cloneDir, suite.gitVersion)
assert.NoError(suite.T(), err)
Expand Down
31 changes: 23 additions & 8 deletions internal/repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ func New(
appFs afero.Fs,
c config.Repositories,
repoManager internal.RepositoryManager,
execManager internal.ExecManager,
logger *slog.Logger,
) *Repositories {
return &Repositories{
appFs: appFs,
config: c,
repoManager: repoManager,
execManager: execManager,
logger: logger,
}
}
Expand All @@ -69,26 +71,26 @@ func (r *Repositories) getCloneHash(
return fmt.Sprintf("%s-%s", replacedGitURL, c.Version)
}

// getGiltDir create the GiltDir if it doesn't exist.
func (r *Repositories) getGiltDir() (string, error) {
expandedGiltDir, err := giltpath.ExpandUser(r.config.GiltDir)
// getCacheDir create the cacheDir if it doesn't exist.
func (r *Repositories) getCacheDir() (string, error) {
giltDir, err := giltpath.ExpandUser(r.config.GiltDir)
if err != nil {
return "", err
}

cacheGiltDir := filepath.Join(expandedGiltDir, "cache")
if _, err := r.appFs.Stat(cacheGiltDir); os.IsNotExist(err) {
if err := r.appFs.Mkdir(cacheGiltDir, 0o755); err != nil {
cacheDir := filepath.Join(giltDir, "cache")
if _, err := r.appFs.Stat(cacheDir); os.IsNotExist(err) {
if err := r.appFs.Mkdir(cacheDir, 0o755); err != nil {
return "", err
}
}

return cacheGiltDir, nil
return cacheDir, nil
}

// Overlay clone and extract the Repository items.
func (r *Repositories) Overlay() error {
cacheDir, err := r.getGiltDir()
cacheDir, err := r.getCacheDir()
if err != nil {
r.logger.Error(
"error expanding dir",
Expand Down Expand Up @@ -126,6 +128,19 @@ func (r *Repositories) Overlay() error {
return err
}
}

// run post commands
if len(c.Commands) > 0 {
for _, command := range c.Commands {
r.logger.Info(
"executing commmand",
slog.String("cmd", command.Cmd),
)
if err := r.execManager.RunCmd(command.Cmd, command.Args); err != nil {
return err
}
}
}
}

return nil
Expand Down
Loading

0 comments on commit 7cadd65

Please sign in to comment.