Skip to content

Commit 65bfa33

Browse files
committed
feat: add download to dev mode
1 parent 0a77f0d commit 65bfa33

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

pkg/cmd/dev.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ type BuildModel struct {
2626
build *stainless.BuildObject
2727
branch string
2828
diagnostics []stainless.BuildDiagnosticListResponse
29-
view string
29+
downloads map[stainless.Target]struct {
30+
status string
31+
path string
32+
}
33+
view string
3034

3135
cc *apiCommandContext
3236
ctx context.Context
@@ -38,6 +42,7 @@ type tickMsg time.Time
3842
type fetchBuildMsg *stainless.BuildObject
3943
type fetchDiagnosticsMsg []stainless.BuildDiagnosticListResponse
4044
type errorMsg error
45+
type downloadMsg stainless.Target
4146
type triggerNewBuildMsg struct{}
4247

4348
func NewBuildModel(cc *apiCommandContext, ctx context.Context, branch string, fn func() (*stainless.BuildObject, error)) BuildModel {
@@ -77,6 +82,11 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7782
cmds = append(cmds, tea.Quit)
7883
}
7984

85+
case downloadMsg:
86+
download := m.downloads[stainless.Target(msg)]
87+
download.status = "completed"
88+
m.downloads[stainless.Target(msg)] = download
89+
8090
case tickMsg:
8191
if m.build != nil {
8292
cmds = append(cmds, m.fetchBuildStatus())
@@ -89,6 +99,19 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8999
case fetchBuildMsg:
90100
if m.build == nil {
91101
m.build = msg
102+
m.downloads = make(map[stainless.Target]struct {
103+
status string
104+
path string
105+
})
106+
for targetName, targetConfig := range m.cc.workspaceConfig.Targets {
107+
m.downloads[stainless.Target(targetName)] = struct {
108+
status string
109+
path string
110+
}{
111+
status: "not started",
112+
path: targetConfig.OutputPath,
113+
}
114+
}
92115
cmds = append(cmds, m.updateView("header"))
93116
}
94117

@@ -97,6 +120,25 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
97120
m.isCompleted = true
98121
cmds = append(cmds, m.fetchDiagnostics())
99122
}
123+
languages := getBuildLanguages(m.build)
124+
for _, target := range languages {
125+
buildTarget := getBuildTarget(m.build, target)
126+
if buildTarget == nil {
127+
continue
128+
}
129+
commitUnion := getStepUnion(buildTarget, "commit")
130+
if commitUnion == nil {
131+
continue
132+
}
133+
status, _, _ := extractStepInfo(commitUnion)
134+
if status == "completed" {
135+
if download, ok := m.downloads[target]; ok && download.status == "not started" {
136+
download.status = "started"
137+
cmds = append(cmds, m.downloadTarget(target))
138+
m.downloads[target] = download
139+
}
140+
}
141+
}
100142

101143
case fetchDiagnosticsMsg:
102144
if m.diagnostics == nil {
@@ -111,6 +153,32 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
111153
return m, tea.Sequence(cmds...)
112154
}
113155

156+
func (m BuildModel) downloadTarget(target stainless.Target) tea.Cmd {
157+
return func() tea.Msg {
158+
if m.build == nil {
159+
return errorMsg(fmt.Errorf("no current build to download target from"))
160+
}
161+
params := stainless.BuildTargetOutputGetParams{
162+
BuildID: m.build.ID,
163+
Target: stainless.BuildTargetOutputGetParamsTarget(target),
164+
Type: "source",
165+
Output: "git",
166+
}
167+
outputRes, err := m.cc.client.Builds.TargetOutputs.Get(
168+
context.TODO(),
169+
params,
170+
)
171+
if err != nil {
172+
return errorMsg(err)
173+
}
174+
err = pullOutput(outputRes.Output, outputRes.URL, outputRes.Ref, m.downloads[target].path, &Group{silent: true})
175+
if err != nil {
176+
return errorMsg(err)
177+
}
178+
return downloadMsg(target)
179+
}
180+
}
181+
114182
func (m BuildModel) fetchBuildStatus() tea.Cmd {
115183
return func() tea.Msg {
116184
if m.build == nil {

pkg/cmd/dev_view.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var parts = []struct {
136136
languages := getBuildLanguages(m.build)
137137
// Target rows with colors
138138
for _, target := range languages {
139-
pipeline := ViewBuildPipeline(m.build, target)
139+
pipeline := ViewBuildPipeline(m.build, target, m.downloads)
140140
langStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("15")).Bold(true)
141141
s.WriteString(fmt.Sprintf("%s %s\n", langStyle.Render(fmt.Sprintf("%-13s", string(target))), pipeline))
142142
}
@@ -169,7 +169,10 @@ var parts = []struct {
169169
},
170170
}
171171

172-
func ViewBuildPipeline(build *stainless.BuildObject, target stainless.Target) string {
172+
func ViewBuildPipeline(build *stainless.BuildObject, target stainless.Target, downloads map[stainless.Target]struct {
173+
status string
174+
path string
175+
}) string {
173176
buildTarget := getBuildTarget(build, target)
174177
if buildTarget == nil {
175178
return ""
@@ -191,6 +194,18 @@ func ViewBuildPipeline(build *stainless.BuildObject, target stainless.Target) st
191194
pipeline.WriteString(symbol + " " + Hyperlink(url, step))
192195
}
193196

197+
if download, ok := downloads[target]; ok {
198+
if download.status == "not started" {
199+
// do nothing
200+
} else if download.status == "started" {
201+
pipeline.WriteString(" → " + "downloading")
202+
} else if download.status == "completed" {
203+
pipeline.WriteString(" → " + "downloaded")
204+
} else {
205+
pipeline.WriteString(" → " + download.status)
206+
}
207+
}
208+
194209
return pipeline.String()
195210
}
196211

pkg/cmd/print.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
type Group struct {
1717
prefix string
1818
indent int
19+
silent bool
1920
}
2021

2122
func Header(format string, args ...any) Group {
@@ -96,32 +97,44 @@ func SSuccess(indent int, format string, args ...any) string {
9697
}
9798

9899
func (g Group) Info(format string, args ...any) Group {
99-
fmt.Fprint(os.Stderr, SInfo(g.indent, format, args...))
100+
if !g.silent {
101+
fmt.Fprint(os.Stderr, SInfo(g.indent, format, args...))
102+
}
100103
return Group{prefix: "i", indent: g.indent + 1}
101104
}
102105

103106
func (g Group) Property(key, msg string) Group {
104-
fmt.Fprint(os.Stderr, SProperty(g.indent, key, msg))
107+
if !g.silent {
108+
fmt.Fprint(os.Stderr, SProperty(g.indent, key, msg))
109+
}
105110
return Group{prefix: "✱", indent: g.indent + 1}
106111
}
107112

108113
func (g Group) Progress(format string, args ...any) Group {
109-
fmt.Fprint(os.Stderr, SProgress(g.indent, format, args...))
114+
if !g.silent {
115+
fmt.Fprint(os.Stderr, SProgress(g.indent, format, args...))
116+
}
110117
return Group{prefix: "✱", indent: g.indent + 1}
111118
}
112119

113120
func (g Group) Error(format string, args ...any) Group {
114-
fmt.Fprint(os.Stderr, SError(g.indent, format, args...))
121+
if !g.silent {
122+
fmt.Fprint(os.Stderr, SError(g.indent, format, args...))
123+
}
115124
return Group{prefix: "✗", indent: g.indent + 1}
116125
}
117126

118127
func (g Group) Warn(format string, args ...any) Group {
119-
fmt.Fprint(os.Stderr, SWarn(g.indent, format, args...))
128+
if !g.silent {
129+
fmt.Fprint(os.Stderr, SWarn(g.indent, format, args...))
130+
}
120131
return Group{prefix: "!", indent: g.indent + 1}
121132
}
122133

123134
func (g Group) Success(format string, args ...any) Group {
124-
fmt.Fprint(os.Stderr, SSuccess(g.indent, format, args...))
135+
if !g.silent {
136+
fmt.Fprint(os.Stderr, SSuccess(g.indent, format, args...))
137+
}
125138
return Group{prefix: "✓", indent: g.indent + 1}
126139
}
127140

0 commit comments

Comments
 (0)