Skip to content

Commit

Permalink
Implement dry run mode of do command
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushjn20 committed May 19, 2019
1 parent 466e51d commit 32cdbcc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
8 changes: 7 additions & 1 deletion cmd/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ func init() {
rootCmd.AddCommand(doCmd)

// Async Mode
doCmd.Flags().BoolP("async", "A", false, "Async mode")
doCmd.Flags().BoolP("async", "A", false, "Asynchronous mode")
if err := viper.BindPFlag("Async", doCmd.Flags().Lookup("async")); err != nil {
log.Fatal(err)
}

// Dry-run mode
doCmd.Flags().Bool("dry-run", false, "Dry-run of the command")
if err := viper.BindPFlag("Dry-run", doCmd.Flags().Lookup("dry-run")); err != nil {
log.Fatal(err)
}

}

var doCmd = &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func Init() {
// Modes
viper.SetDefault("Async", false)
viper.SetDefault("Verbose", false)
viper.SetDefault("Dry-run", false)

// Constants
viper.SetDefault("DockerAPIVersion", "1.39")
Expand Down
47 changes: 25 additions & 22 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,35 +143,38 @@ func (step Step) Exec() (*[]Result, error) {
}()

var results []Result
if multipleCommands {
for _, cmd := range step.Commands {
r, err := runCmd(ctx, cli, resp.ID, cmd)
if err != nil {
log.Fatal(err)
if dryRun := viper.GetBool("Dry-run"); !dryRun {
if multipleCommands {
for _, cmd := range step.Commands {
r, err := runCmd(ctx, cli, resp.ID, cmd)
if err != nil {
log.Fatal(err)
}
results = append(results, *r)
}
results = append(results, *r)
}
} else {
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err = <-errCh:
} else {
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err = <-errCh:
if err != nil {
log.Fatal(err)
}
case <-statusCh:
}

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
log.Fatal(err)
}
case <-statusCh:
}

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
log.Fatal(err)
results = []Result{*extractResult(out, step.Command)}
}

results = []Result{*extractResult(out, step.Command)}
return &results, nil
}
return &results, nil
return nil, nil
}

func runCmd(ctx context.Context, cli *client.Client, containerID string, command []string) (*Result, error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/dunner/dunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func process(configs *config.Configs, s *docker.Step, wg *sync.WaitGroup, args [
log.Fatal(err)
}

if results == nil {
return
}

for _, res := range *results {
log.Infof(
"Running task '%+v' on '%+v' Docker with command '%+v'",
Expand Down

0 comments on commit 32cdbcc

Please sign in to comment.