|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
5 | | - runtimevar "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime" |
6 | | - "github.com/loft-sh/devspace/pkg/devspace/imageselector" |
7 | 6 | "io" |
8 | 7 | "os" |
| 8 | + "os/exec" |
9 | 9 | "strings" |
10 | 10 | "sync" |
11 | 11 | "time" |
12 | 12 |
|
| 13 | + runtimevar "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime" |
| 14 | + "github.com/loft-sh/devspace/pkg/devspace/imageselector" |
| 15 | + |
13 | 16 | "github.com/loft-sh/devspace/pkg/devspace/config" |
14 | 17 | "github.com/loft-sh/devspace/pkg/devspace/config/legacy" |
15 | 18 | "github.com/loft-sh/devspace/pkg/devspace/dependency/types" |
@@ -600,30 +603,54 @@ func (cmd *DevCmd) startOutput(configInterface config.Config, dependencies []typ |
600 | 603 | return 0, pluginErr |
601 | 604 | } |
602 | 605 |
|
603 | | - selectorOptions := targetselector.NewDefaultOptions().ApplyCmdParameter("", "", cmd.Namespace, "") |
604 | | - if config.Dev.Terminal != nil { |
605 | | - selectorOptions = selectorOptions.ApplyConfigParameter(config.Dev.Terminal.LabelSelector, config.Dev.Terminal.Namespace, config.Dev.Terminal.ContainerName, "") |
606 | | - } |
| 606 | + // if config.Dev.Terminal is defined && config.Images is empty |
| 607 | + // config.Dev.Terminal.ImageSelector is empty && |
| 608 | + // config.Dev.Terminal.LabelSelector is also empty && |
| 609 | + // config.Dev.Terminal.Command is defined then |
| 610 | + // run the command locally instead on in container |
| 611 | + if config.Images == nil && config.Dev.Terminal.ImageSelector == "" && |
| 612 | + config.Dev.Terminal.LabelSelector == nil && |
| 613 | + config.Dev.Terminal.Command != nil && |
| 614 | + len(config.Dev.Terminal.Command) > 0 { |
| 615 | + command := config.Dev.Terminal.Command[0] |
| 616 | + c := exec.Command(command, config.Dev.Terminal.Command[1:]...) |
| 617 | + var stdout bytes.Buffer |
| 618 | + c.Stdout = &stdout |
| 619 | + if err := c.Run(); err != nil { |
| 620 | + if exitError, ok := err.(*exec.ExitError); ok { |
| 621 | + cmd.log.Failf("Command '%s' returned an error: %s", command, err) |
| 622 | + return exitError.ExitCode(), err |
| 623 | + } |
| 624 | + } |
| 625 | + // if command succeeds |
| 626 | + cmd.log.Infof("Command '%s' returned: %s", command, stdout.String()) |
| 627 | + return 0, nil |
| 628 | + } else { |
| 629 | + selectorOptions := targetselector.NewDefaultOptions().ApplyCmdParameter("", "", cmd.Namespace, "") |
| 630 | + if config.Dev.Terminal != nil { |
| 631 | + selectorOptions = selectorOptions.ApplyConfigParameter(config.Dev.Terminal.LabelSelector, config.Dev.Terminal.Namespace, config.Dev.Terminal.ContainerName, "") |
| 632 | + } |
607 | 633 |
|
608 | | - var imageSelectors []imageselector.ImageSelector |
609 | | - if config.Dev.Terminal != nil && config.Dev.Terminal.ImageSelector != "" { |
610 | | - imageSelector, err := runtimevar.NewRuntimeResolver(true).FillRuntimeVariablesAsImageSelector(config.Dev.Terminal.ImageSelector, configInterface, dependencies) |
611 | | - if err != nil { |
612 | | - return 0, err |
| 634 | + var imageSelectors []imageselector.ImageSelector |
| 635 | + if config.Dev.Terminal != nil && config.Dev.Terminal.ImageSelector != "" { |
| 636 | + imageSelector, err := runtimevar.NewRuntimeResolver(true).FillRuntimeVariablesAsImageSelector(config.Dev.Terminal.ImageSelector, configInterface, dependencies) |
| 637 | + if err != nil { |
| 638 | + return 0, err |
| 639 | + } |
| 640 | + |
| 641 | + imageSelectors = append(imageSelectors, *imageSelector) |
613 | 642 | } |
614 | 643 |
|
615 | | - imageSelectors = append(imageSelectors, *imageSelector) |
616 | | - } |
| 644 | + cmd.log.Info("Terminal: Waiting for containers to start...") |
| 645 | + selectorOptions.ImageSelector = imageSelectors |
| 646 | + stdout, stderr, stdin := defaultStdStreams(cmd.Stdout, cmd.Stderr, cmd.Stdin) |
| 647 | + code, err := servicesClient.StartTerminal(selectorOptions, args, cmd.WorkingDirectory, exitChan, true, cmd.TerminalReconnect, stdout, stderr, stdin) |
| 648 | + if services.IsUnexpectedExitCode(code) { |
| 649 | + cmd.log.Warnf("Command terminated with exit code %d", code) |
| 650 | + } |
617 | 651 |
|
618 | | - cmd.log.Info("Terminal: Waiting for containers to start...") |
619 | | - selectorOptions.ImageSelector = imageSelectors |
620 | | - stdout, stderr, stdin := defaultStdStreams(cmd.Stdout, cmd.Stderr, cmd.Stdin) |
621 | | - code, err := servicesClient.StartTerminal(selectorOptions, args, cmd.WorkingDirectory, exitChan, true, cmd.TerminalReconnect, stdout, stderr, stdin) |
622 | | - if services.IsUnexpectedExitCode(code) { |
623 | | - cmd.log.Warnf("Command terminated with exit code %d", code) |
| 652 | + return code, err |
624 | 653 | } |
625 | | - |
626 | | - return code, err |
627 | 654 | } else if config.Dev.Logs == nil || config.Dev.Logs.Disabled == nil || !*config.Dev.Logs.Disabled { |
628 | 655 | pluginErr := hook.ExecuteHooks(client, configInterface, dependencies, nil, cmd.log, "devCommand:before:streamLogs") |
629 | 656 | if pluginErr != nil { |
@@ -761,23 +788,25 @@ func (cmd *DevCmd) loadConfig(configOptions *loader.ConfigOptions) (config.Confi |
761 | 788 | imageNames = append(imageNames, k) |
762 | 789 | } |
763 | 790 |
|
764 | | - // if only one image exists, use it, otherwise show image picker |
765 | | - imageName := "" |
766 | | - if len(imageNames) == 1 { |
767 | | - imageName = imageNames[0] |
768 | | - } else { |
769 | | - imageName, err = cmd.log.Question(&survey.QuestionOptions{ |
770 | | - Question: "Which image do you want to open a terminal to?", |
771 | | - Options: imageNames, |
772 | | - }) |
773 | | - if err != nil { |
774 | | - return nil, err |
| 791 | + if len(imageNames) > 0 { |
| 792 | + // if only one image exists, use it, otherwise show image picker |
| 793 | + imageName := "" |
| 794 | + if len(imageNames) == 1 { |
| 795 | + imageName = imageNames[0] |
| 796 | + } else { |
| 797 | + imageName, err = cmd.log.Question(&survey.QuestionOptions{ |
| 798 | + Question: "Which image do you want to open a terminal to?", |
| 799 | + Options: imageNames, |
| 800 | + }) |
| 801 | + if err != nil { |
| 802 | + return nil, err |
| 803 | + } |
| 804 | + } |
| 805 | + c.Dev.Terminal = &latest.Terminal{ |
| 806 | + ImageSelector: fmt.Sprintf("${runtime.images.%s.image}:${runtime.images.%s.tag}", imageName, imageName), |
775 | 807 | } |
776 | 808 | } |
777 | 809 |
|
778 | | - c.Dev.Terminal = &latest.Terminal{ |
779 | | - ImageSelector: fmt.Sprintf("${runtime.images.%s.image}:${runtime.images.%s.tag}", imageName, imageName), |
780 | | - } |
781 | 810 | } else { |
782 | 811 | c.Dev.Terminal.Disabled = false |
783 | 812 | } |
|
0 commit comments