Skip to content

Commit

Permalink
feat: add --no-commands switch
Browse files Browse the repository at this point in the history
Fixes: Issue #205
  • Loading branch information
nisimond authored and retr0h committed May 26, 2024
1 parent 3d62646 commit 160355a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Enable or disable debug mode")
rootCmd.PersistentFlags().BoolP("parallel", "p", true, "Fetch clones in parallel")
rootCmd.PersistentFlags().Bool("no-commands", false, "Skip post-commands when overlaying")
rootCmd.PersistentFlags().
StringP("gilt-dir", "c", "~/.gilt/clone", "Path to Gilt's clone dir")
rootCmd.PersistentFlags().
StringP("gilt-file", "f", "Giltfile.yaml", "Path to config file")

_ = viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
_ = viper.BindPFlag("parallel", rootCmd.PersistentFlags().Lookup("parallel"))
_ = viper.BindPFlag("skipCommands", rootCmd.PersistentFlags().Lookup("no-commands"))
_ = viper.BindPFlag("giltFile", rootCmd.PersistentFlags().Lookup("gilt-file"))
_ = viper.BindPFlag("giltDir", rootCmd.PersistentFlags().Lookup("gilt-dir"))
_ = viper.BindPFlag("repositories", rootCmd.PersistentFlags().Lookup("repositories"))
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ should have an 80 character line wrap limit (enforced by Prettier).

### Writing tests

When making a changes, consider whether new tests are required. These tests
should ensure that the functionality you are adding will continue to work in the
When making changes, consider whether new tests are required. These tests should
ensure that the functionality you are adding will continue to work in the
future. Existing tests may also need updating if you have changed Gilt's
behavior.

Expand Down
8 changes: 8 additions & 0 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ Display the git commands being executed.
gilt --debug overlay
```

### Skipping post-commands

Overlay files only, but run no other commands.

```bash
gilt overlay --no-commands
```

## Package

### Overlay Repository
Expand Down
4 changes: 4 additions & 0 deletions internal/repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (r *Repositories) Overlay() error {
}

// run post commands
if r.config.SkipCommands {
r.logger.Info("skipping running post-commands")
continue
}
if err := r.runCommands(c); err != nil {
return err
}
Expand Down
31 changes: 30 additions & 1 deletion internal/repositories/repositories_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type RepositoriesPublicTestSuite struct {
gitURL string
gitVersion string
repoConfigDstDir []config.Repository
SkipCommands bool
logger *slog.Logger
}

Expand All @@ -62,6 +63,7 @@ func (suite *RepositoriesPublicTestSuite) NewTestRepositoriesManager(
reposConfig := config.Repositories{
Debug: false,
Parallel: true,
SkipCommands: suite.SkipCommands,
GiltFile: "Giltfile.yaml",
GiltDir: suite.giltDir,
Repositories: repoConfig,
Expand Down Expand Up @@ -93,7 +95,7 @@ func (suite *RepositoriesPublicTestSuite) SetupTest() {
DstDir: suite.dstDir,
},
}

suite.SkipCommands = false
suite.logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
}

Expand Down Expand Up @@ -300,6 +302,33 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCommandErro
assert.Error(suite.T(), err)
}

func (suite *RepositoriesPublicTestSuite) TestOverlaySkipsCommands() {
suite.SkipCommands = true
repoConfig := []config.Repository{
{
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
Commands: []config.Command{
{
Cmd: "touch",
Args: []string{"/tmp/foo"},
},
},
},
}

repos := suite.NewTestRepositoriesManager(repoConfig)

suite.mockRepo.EXPECT().Clone(gomock.Any(), gomock.Any()).Return("", nil)
suite.mockRepo.EXPECT().Worktree(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
// Explicitly check that RunCmd is never called
suite.mockExec.EXPECT().RunCmd(gomock.Any(), gomock.Any()).Times(0)

err := repos.Overlay()
assert.NoError(suite.T(), err)
}

// In order for `go test` to run this suite, we need to create
// a normal test function and pass our suite to suite.Run.
func TestRepositoriesPublicTestSuite(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Repositories struct {
Debug bool `mapstruture:"debug"`
// Parallel enable or disable concurrent clone fetches.
Parallel bool ` mapstructure:"parallel"`
// SkipCommands run post-commands as part of the overlay process
SkipCommands bool
// GiltFile path to Gilt's config file option set from CLI.
GiltFile string ` mapstructure:"giltFile" validate:"required"`
// GiltDir path to Gilt's clone dir option set from CLI.
Expand Down

0 comments on commit 160355a

Please sign in to comment.