Skip to content

Commit 1fee7b4

Browse files
authored
Override better (#94)
1 parent 9bc18f1 commit 1fee7b4

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

internal/local_runner.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
9494
overrides[k] = v
9595
}
9696

97+
// Now, the override can either be one of two things (we are overloading the override map):
98+
// - docker image: In that case, change the manifest and remove from override map
99+
// - a path to an executable: In that case, we need to run it on the host machine
100+
// and use the override map <- We only check this case, and if it is not a path, we assume
101+
// it is a docker image. If it is not a docker image either, the error will be catched during the execution
102+
for k, v := range overrides {
103+
if _, err := os.Stat(v); err != nil {
104+
// this is a path to an executable, remove it from the overrides since we
105+
// assume it s a docker image and add it to manifest
106+
parts := strings.Split(v, ":")
107+
if len(parts) != 2 {
108+
return nil, fmt.Errorf("invalid override docker image %s, expected image:tag", v)
109+
}
110+
111+
srv := manifest.MustGetService(k)
112+
srv.image = parts[0]
113+
srv.tag = parts[1]
114+
115+
delete(overrides, k)
116+
continue
117+
}
118+
}
119+
97120
tasks := map[string]*task{}
98121
for _, svc := range manifest.services {
99122
tasks[svc.Name] = &task{
@@ -428,6 +451,27 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
428451
return argsResult, envs, nil
429452
}
430453

454+
func (d *LocalRunner) validateImageExists(image string) error {
455+
// check locally
456+
_, err := d.client.ImageInspect(context.Background(), image)
457+
if err == nil {
458+
return nil
459+
}
460+
if !client.IsErrNotFound(err) {
461+
return err
462+
}
463+
464+
// check remotely
465+
if _, err = d.client.DistributionInspect(context.Background(), image, ""); err == nil {
466+
return nil
467+
}
468+
if !client.IsErrNotFound(err) {
469+
return err
470+
}
471+
472+
return fmt.Errorf("image %s not found: %w", image)
473+
}
474+
431475
func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}, error) {
432476
// apply the template again on the arguments to figure out the connections
433477
// at this point all of them are valid, we just have to resolve them again. We assume for now
@@ -444,8 +488,14 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
444488
return nil, fmt.Errorf("failed to get absolute path for output folder: %w", err)
445489
}
446490

491+
// Validate that the image exists
492+
imageName := fmt.Sprintf("%s:%s", s.image, s.tag)
493+
if err := d.validateImageExists(imageName); err != nil {
494+
return nil, fmt.Errorf("failed to validate image %s: %w", imageName, err)
495+
}
496+
447497
service := map[string]interface{}{
448-
"image": fmt.Sprintf("%s:%s", s.image, s.tag),
498+
"image": imageName,
449499
"command": args,
450500
// Add volume mount for the output directory
451501
"volumes": []string{

main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ func runIt(recipe internal.Recipe) error {
166166

167167
log.Printf("Log level: %s\n", logLevel)
168168

169+
// parse the overrides
170+
overrides := map[string]string{}
171+
for _, val := range withOverrides {
172+
parts := strings.SplitN(val, "=", 2)
173+
if len(parts) != 2 {
174+
return fmt.Errorf("invalid override format: %s, expected service=val", val)
175+
}
176+
overrides[parts[0]] = parts[1]
177+
}
178+
169179
builder := recipe.Artifacts()
170180
builder.OutputDir(outputFlag)
171181
builder.GenesisDelay(genesisDelayFlag)
@@ -189,7 +199,14 @@ func runIt(recipe internal.Recipe) error {
189199
return nil
190200
}
191201

192-
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, nil, interactive)
202+
// validate that override is being applied to a service in the manifest
203+
for k := range overrides {
204+
if _, ok := svcManager.GetService(k); !ok {
205+
return fmt.Errorf("service '%s' in override not found in manifest", k)
206+
}
207+
}
208+
209+
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive)
193210
if err != nil {
194211
return fmt.Errorf("failed to create docker runner: %w", err)
195212
}

0 commit comments

Comments
 (0)