Skip to content

Commit a70e8e0

Browse files
authored
Allow to run local preview without animation (#384)
1 parent 02ae074 commit a70e8e0

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

internal/cmd/module/flags.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package module
22

3-
import "github.com/urfave/cli/v3"
3+
import (
4+
"os"
5+
6+
"github.com/mattn/go-isatty"
7+
"github.com/urfave/cli/v3"
8+
)
49

510
var flagModuleID = &cli.StringFlag{
611
Name: "id",
@@ -56,3 +61,9 @@ var flagWithGitDir = &cli.BoolFlag{
5661
Name: "with-git-dir",
5762
Usage: "[Optional] Add the .git to the archive, useful when you define custom behavior inside the repository",
5863
}
64+
65+
var flagNoAnimation = &cli.BoolFlag{
66+
Name: "no-animation",
67+
Usage: "[Optional] Disable animated output and print plain text status updates instead. Automatically enabled when the output is not a terminal.",
68+
Value: isatty.IsTerminal(os.Stdout.Fd()),
69+
}

internal/cmd/module/local_preview.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func localPreviewFunc(useHeaders bool) cli.ActionFunc {
140140
return err
141141
}
142142

143+
noAnimation := cliCmd.Bool(flagNoAnimation.Name)
144+
if noAnimation {
145+
return runPlainLocalPreview(ctx, moduleID, triggerMutation.VersionProposeLocalWorkspace)
146+
}
147+
143148
model := newModuleLocalPreviewModel(moduleID, triggerMutation.VersionProposeLocalWorkspace)
144149

145150
go func() {
@@ -187,6 +192,74 @@ func localPreviewFunc(useHeaders bool) cli.ActionFunc {
187192
}
188193
}
189194

195+
func runPlainLocalPreview(ctx context.Context, moduleID string, initialRuns []runQuery) error {
196+
runs := make([]runQuery, len(initialRuns))
197+
copy(runs, initialRuns)
198+
199+
printRun := func(run runQuery) {
200+
fmt.Printf("%s • %s • %s\n", run.State, run.Title, authenticated.Client().URL(
201+
"/module/%s/run/%s",
202+
moduleID,
203+
run.ID,
204+
))
205+
}
206+
207+
for _, run := range runs {
208+
printRun(run)
209+
}
210+
211+
ticker := time.NewTicker(time.Second * 5)
212+
defer ticker.Stop()
213+
214+
for range ticker.C {
215+
newRuns := make([]runQuery, len(runs))
216+
var g errgroup.Group
217+
for i := range newRuns {
218+
index := i
219+
g.Go(func() error {
220+
var getRun struct {
221+
Module struct {
222+
Run runQuery `graphql:"run(id: $run)"`
223+
} `graphql:"module(id: $module)"`
224+
}
225+
226+
if err := authenticated.Client().Query(ctx, &getRun, map[string]interface{}{
227+
"module": graphql.ID(moduleID),
228+
"run": graphql.ID(runs[index].ID),
229+
}); err != nil {
230+
return err
231+
}
232+
233+
newRuns[index] = getRun.Module.Run
234+
return nil
235+
})
236+
}
237+
if err := g.Wait(); err != nil {
238+
return fmt.Errorf("couldn't get runs: %w", err)
239+
}
240+
241+
for i, newRun := range newRuns {
242+
if newRun.State != runs[i].State {
243+
printRun(newRun)
244+
}
245+
}
246+
runs = newRuns
247+
248+
allFinished := true
249+
for _, run := range runs {
250+
if !run.Finished {
251+
allFinished = false
252+
break
253+
}
254+
}
255+
if allFinished {
256+
return nil
257+
}
258+
}
259+
260+
return nil
261+
}
262+
190263
type checkRunsFinishedMsg struct{}
191264

192265
type moduleLocalPreviewModel struct {

internal/cmd/module/module.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func Command() cmd.Command {
7373
flagDisregardGitignore,
7474
flagWithGitDir,
7575
flagTests,
76+
flagNoAnimation,
7677
},
7778
Action: localPreviewFunc(false),
7879
Before: authenticated.Ensure,
@@ -90,6 +91,7 @@ func Command() cmd.Command {
9091
flagDisregardGitignore,
9192
flagWithGitDir,
9293
flagTests,
94+
flagNoAnimation,
9395
},
9496
Action: localPreviewFunc(true),
9597
Before: authenticated.Ensure,

0 commit comments

Comments
 (0)