Skip to content

Commit bd03d0d

Browse files
committed
feat: refactor based on sourcery feedback
1 parent 2e82981 commit bd03d0d

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

cmd/cli/commands/launch.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func newLaunchCmd() *cobra.Command {
106106
}
107107

108108
if ca, ok := containerApps[app]; ok {
109-
return launchContainerApp(cmd, ca, ep.container, image, port, detach, dryRun)
109+
return launchContainerApp(cmd, ca, ep.container, image, port, detach, appArgs, dryRun)
110110
}
111111
if cli, ok := hostApps[app]; ok {
112112
return launchHostApp(cmd, app, ep.host, cli, appArgs, dryRun)
@@ -125,6 +125,11 @@ func newLaunchCmd() *cobra.Command {
125125
// resolveBaseEndpoints resolves the base URLs (without path) for both
126126
// container and host client locations.
127127
func resolveBaseEndpoints(runner *standaloneRunner) (engineEndpoints, error) {
128+
const (
129+
localhost = "127.0.0.1"
130+
hostDockerInternal = "host.docker.internal"
131+
)
132+
128133
kind := modelRunner.EngineKind()
129134
switch kind {
130135
case types.ModelRunnerEngineKindDesktop:
@@ -135,8 +140,8 @@ func resolveBaseEndpoints(runner *standaloneRunner) (engineEndpoints, error) {
135140
case types.ModelRunnerEngineKindMobyManual:
136141
ep := strings.TrimRight(modelRunner.URL(""), "/")
137142
containerEP := strings.NewReplacer(
138-
"localhost", "host.docker.internal",
139-
"127.0.0.1", "host.docker.internal",
143+
"localhost", hostDockerInternal,
144+
localhost, hostDockerInternal,
140145
).Replace(ep)
141146
return engineEndpoints{container: containerEP, host: ep}, nil
142147
case types.ModelRunnerEngineKindCloud, types.ModelRunnerEngineKindMoby:
@@ -147,12 +152,14 @@ func resolveBaseEndpoints(runner *standaloneRunner) (engineEndpoints, error) {
147152
port := fmt.Sprintf("%d", runner.gatewayPort)
148153
return engineEndpoints{
149154
container: "http://" + net.JoinHostPort(runner.gatewayIP, port),
150-
host: "http://" + net.JoinHostPort("127.0.0.1", port),
155+
host: "http://" + net.JoinHostPort(localhost, port),
151156
}, nil
152157
}
153158
if runner.hostPort != 0 {
159+
hostPort := fmt.Sprintf("%d", runner.hostPort)
154160
return engineEndpoints{
155-
host: "http://" + net.JoinHostPort("127.0.0.1", fmt.Sprintf("%d", runner.hostPort)),
161+
container: "http://" + net.JoinHostPort(hostDockerInternal, hostPort),
162+
host: "http://" + net.JoinHostPort(localhost, hostPort),
156163
}, nil
157164
}
158165
return engineEndpoints{}, errors.New("unable to determine standalone runner endpoint")
@@ -162,7 +169,7 @@ func resolveBaseEndpoints(runner *standaloneRunner) (engineEndpoints, error) {
162169
}
163170

164171
// launchContainerApp launches a container-based app via "docker run".
165-
func launchContainerApp(cmd *cobra.Command, ca containerApp, baseURL string, imageOverride string, portOverride int, detach, dryRun bool) error {
172+
func launchContainerApp(cmd *cobra.Command, ca containerApp, baseURL string, imageOverride string, portOverride int, detach bool, appArgs []string, dryRun bool) error {
166173
img := imageOverride
167174
if img == "" {
168175
img = ca.defaultImage
@@ -187,6 +194,7 @@ func launchContainerApp(cmd *cobra.Command, ca containerApp, baseURL string, ima
187194
dockerArgs = append(dockerArgs, "-e", e)
188195
}
189196
dockerArgs = append(dockerArgs, img)
197+
dockerArgs = append(dockerArgs, appArgs...)
190198

191199
if dryRun {
192200
cmd.Printf("Would run: docker %s\n", strings.Join(dockerArgs, " "))

cmd/cli/commands/launch_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestResolveBaseEndpointsHostPortFallback(t *testing.T) {
150150
runner := &standaloneRunner{hostPort: 12434}
151151
ep, err := resolveBaseEndpoints(runner)
152152
require.NoError(t, err)
153-
require.Equal(t, "", ep.container)
153+
require.Equal(t, "http://host.docker.internal:12434", ep.container)
154154
require.Equal(t, "http://127.0.0.1:12434", ep.host)
155155
}
156156

@@ -159,7 +159,7 @@ func TestLaunchContainerAppDryRun(t *testing.T) {
159159
buf := new(bytes.Buffer)
160160
cmd := newTestCmd(buf)
161161

162-
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, false, true)
162+
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, false, nil, true)
163163
require.NoError(t, err)
164164

165165
output := buf.String()
@@ -177,7 +177,7 @@ func TestLaunchContainerAppOverrides(t *testing.T) {
177177
buf := new(bytes.Buffer)
178178
cmd := newTestCmd(buf)
179179

180-
err := launchContainerApp(cmd, ca, testBaseURL, overrideImage, overridePort, false, true)
180+
err := launchContainerApp(cmd, ca, testBaseURL, overrideImage, overridePort, false, nil, true)
181181
require.NoError(t, err)
182182

183183
output := buf.String()
@@ -191,7 +191,7 @@ func TestLaunchContainerAppDetach(t *testing.T) {
191191
buf := new(bytes.Buffer)
192192
cmd := newTestCmd(buf)
193193

194-
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, true, true)
194+
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, true, nil, true)
195195
require.NoError(t, err)
196196

197197
output := buf.String()
@@ -206,7 +206,7 @@ func TestLaunchContainerAppUsesEnvFn(t *testing.T) {
206206
buf := new(bytes.Buffer)
207207
cmd := newTestCmd(buf)
208208

209-
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, false, true)
209+
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, false, nil, true)
210210
require.NoError(t, err)
211211

212212
output := buf.String()
@@ -219,7 +219,7 @@ func TestLaunchContainerAppNilEnvFn(t *testing.T) {
219219
buf := new(bytes.Buffer)
220220
cmd := newTestCmd(buf)
221221

222-
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, false, true)
222+
err := launchContainerApp(cmd, ca, testBaseURL, "", 0, false, nil, true)
223223
require.Error(t, err)
224224
require.Contains(t, err.Error(), "container app requires envFn to be set")
225225
}

0 commit comments

Comments
 (0)