Skip to content

Commit

Permalink
feat(cli): add --fallback-image flag which forces an image instead of…
Browse files Browse the repository at this point in the history
… trying to detect the language
  • Loading branch information
pascalbreuninger committed Apr 25, 2024
1 parent 2ee823e commit 255ccf8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
5 changes: 3 additions & 2 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command {
upCmd.Flags().StringVar(&cmd.Machine, "machine", "", "The machine to use for this workspace. The machine needs to exist beforehand or the command will fail. If the workspace already exists, this option has no effect")
upCmd.Flags().StringVar(&cmd.IDE, "ide", "", "The IDE to open the workspace in. If empty will use vscode locally or in browser")
upCmd.Flags().BoolVar(&cmd.OpenIDE, "open-ide", true, "If this is false and an IDE is configured, DevPod will only install the IDE server backend, but not open it")
upCmd.Flags().BoolVar(&cmd.ForceCredentials, "force-credentials", false, "If true will always use local credentials")
_ = upCmd.Flags().MarkHidden("force-credentials")
upCmd.Flags().StringVar(&cmd.GitBranch, "git-branch", "", "The git branch to use")
upCmd.Flags().StringVar(&cmd.GitCommit, "git-commit", "", "The git commit SHA to use")
upCmd.Flags().StringVar(&cmd.FallbackImage, "fallback-image", "", "The fallback image to use if no devcontainer configuration has been detected")

upCmd.Flags().BoolVar(&cmd.DisableDaemon, "disable-daemon", false, "If enabled, will not install a daemon into the target machine to track activity")
upCmd.Flags().StringVar(&cmd.Source, "source", "", "Optional source for the workspace. E.g. git:https://github.com/my-org/my-repo")
upCmd.Flags().BoolVar(&cmd.Proxy, "proxy", false, "If true will forward agent requests to stdio")
upCmd.Flags().BoolVar(&cmd.ForceCredentials, "force-credentials", false, "If true will always use local credentials")
_ = upCmd.Flags().MarkHidden("force-credentials")

// testing
upCmd.Flags().StringVar(&cmd.DaemonInterval, "daemon-interval", "", "TESTING ONLY")
Expand Down
37 changes: 27 additions & 10 deletions pkg/devcontainer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,26 @@ func (r *runner) Up(ctx context.Context, options UpOptions) (*config.Result, err
return nil, err
}
} else {
r.Log.Warn("dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties, defaulting to auto-detection")
if options.FallbackImage != "" {
r.Log.Warn("dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties, using fallback image " + options.FallbackImage)

lang, err := language.DetectLanguage(r.LocalWorkspaceFolder)
if err != nil {
return nil, fmt.Errorf("could not detect project language and dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
}
substitutedConfig.Config.ImageContainer = config.ImageContainer{
Image: options.FallbackImage,
}
} else {
r.Log.Warn("dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties, defaulting to auto-detection")

lang, err := language.DetectLanguage(r.LocalWorkspaceFolder)
if err != nil {
return nil, fmt.Errorf("could not detect project language and dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
}

if language.MapConfig[lang] == nil {
return nil, fmt.Errorf("could not detect project language and dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
if language.MapConfig[lang] == nil {
return nil, fmt.Errorf("could not detect project language and dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
}
substitutedConfig.Config.ImageContainer = language.MapConfig[lang].ImageContainer
}

substitutedConfig.Config.ImageContainer = language.MapConfig[lang].ImageContainer
result, err = r.runSingleContainer(ctx, substitutedConfig, substitutionContext, options)
if err != nil {
return nil, err
Expand Down Expand Up @@ -200,8 +208,17 @@ func (r *runner) prepare(
return nil, nil, errors.Wrap(err, "parsing devcontainer.json")
} else if rawParsedConfig == nil {
r.Log.Infof("Couldn't find a devcontainer.json")
r.Log.Infof("Try detecting project programming language...")
defaultConfig := language.DefaultConfig(r.LocalWorkspaceFolder, r.Log)
defaultConfig := &config.DevContainerConfig{}
if options.FallbackImage != "" {
r.Log.Infof("Using fallback image %s", options.FallbackImage)
defaultConfig.ImageContainer = config.ImageContainer{
Image: options.FallbackImage,
}
} else {
r.Log.Infof("Try detecting project programming language...")
defaultConfig = language.DefaultConfig(r.LocalWorkspaceFolder, r.Log)
}

defaultConfig.Origin = path.Join(filepath.ToSlash(r.LocalWorkspaceFolder), ".devcontainer.json")
err = config.SaveDevContainerJSON(defaultConfig)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type CLIOptions struct {
ForceCredentials bool `json:"forceCredentials,omitempty"`
GitBranch string `json:"gitBranch,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
FallbackImage string `json:"fallbackImage,omitempty"`

// build options
Repository string `json:"repository,omitempty"`
Expand Down

0 comments on commit 255ccf8

Please sign in to comment.