Skip to content

Commit 9839f92

Browse files
authored
Use the app name instead of path
1 parent 30d4f4d commit 9839f92

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

internal/orchestrator/provision.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log/slog"
77
"os"
8+
"regexp"
89

910
"github.com/arduino/arduino-app-cli/pkg/parser"
1011

@@ -15,6 +16,8 @@ import (
1516
"gopkg.in/yaml.v3"
1617
)
1718

19+
var containerNameInvalidRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
20+
1821
func ProvisionApp(ctx context.Context, docker *dockerClient.Client, app parser.App) error {
1922
if err := pullBasePythonContainer(ctx, pythonImage); err != nil {
2023
return fmt.Errorf("provisioning failed to pull base image: %w", err)
@@ -27,7 +30,7 @@ func ProvisionApp(ctx context.Context, docker *dockerClient.Client, app parser.A
2730
}, &container.HostConfig{
2831
Binds: []string{app.FullPath.String() + ":/app"},
2932
AutoRemove: true,
30-
}, nil, nil, app.Name)
33+
}, nil, nil, generateContainerName(app.Name))
3134
if err != nil {
3235
return fmt.Errorf("provisiong failed to create container: %w", err)
3336
}
@@ -52,6 +55,17 @@ func ProvisionApp(ctx context.Context, docker *dockerClient.Client, app parser.A
5255
return generateMainComposeFile(ctx, app, pythonImage)
5356
}
5457

58+
// Converts an arbitrary string to one that satisfies the container name requirement: [a-zA-Z0-9][a-zA-Z0-9_.-]
59+
// See the Docker Engine code here: https://github.com/moby/moby/blob/master/daemon/names/names.go#L6
60+
func generateContainerName(appName string) string {
61+
result := containerNameInvalidRegex.ReplaceAllString(appName, "")
62+
63+
if len(result) < 1 {
64+
result = "c"
65+
}
66+
return result
67+
}
68+
5569
func pullBasePythonContainer(ctx context.Context, pythonImage string) error {
5670
process, err := paths.NewProcess(nil, "docker", "pull", pythonImage)
5771
if err != nil {

pkg/parser/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func Load(appPath string) (App, error) {
3232
}
3333

3434
app := App{
35-
Name: path.Base(),
3635
FullPath: path,
3736
Descriptor: Descriptor{},
3837
}
@@ -43,6 +42,7 @@ func Load(appPath string) (App, error) {
4342
return App{}, fmt.Errorf("error loading app descriptor file: %w", err)
4443
}
4544
app.Descriptor = desc
45+
app.Name = desc.DisplayName
4646
} else {
4747
return App{}, errors.New("descriptor app.yaml file missing from app")
4848
}

0 commit comments

Comments
 (0)