Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"github.com/loft-sh/devspace/pkg/util/factory"
"github.com/spf13/cobra"
)

// NewBuildCmd creates a new devspace build command
func NewBuildCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
func NewBuildCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
cmd := &RunPipelineCmd{
GlobalFlags: globalFlags,
Pipeline: "build",
Expand All @@ -23,10 +24,14 @@ func NewBuildCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Comma
Builds all defined images and pushes them
#######################################################`,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd, args, f, "build", "buildCommand")
return cmd.Run(cobraCmd, args, f, "buildCommand")
},
}

cmd.AddFlags(buildCmd)
var pipeline *latest.Pipeline
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
pipeline = rawConfig.Config.Pipelines["build"]
}
cmd.AddPipelineFlags(f, buildCmd, pipeline)
return buildCmd
}
11 changes: 8 additions & 3 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"github.com/loft-sh/devspace/pkg/util/factory"
"github.com/spf13/cobra"
)

// NewDeployCmd creates a new deploy command
func NewDeployCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
func NewDeployCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
cmd := &RunPipelineCmd{
GlobalFlags: globalFlags,
SkipPushLocalKubernetes: true,
Expand All @@ -29,10 +30,14 @@ devspace deploy --kube-context=deploy-context
#######################################################`,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd, args, f, "deploy", "deployCommand")
return cmd.Run(cobraCmd, args, f, "deployCommand")
},
}

cmd.AddFlags(deployCmd)
var pipeline *latest.Pipeline
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
pipeline = rawConfig.Config.Pipelines["deploy"]
}
cmd.AddPipelineFlags(f, deployCmd, pipeline)
return deployCmd
}
11 changes: 8 additions & 3 deletions cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"github.com/loft-sh/devspace/pkg/util/factory"
"github.com/spf13/cobra"
)

// NewDevCmd creates a new devspace dev command
func NewDevCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
func NewDevCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
cmd := &RunPipelineCmd{
GlobalFlags: globalFlags,
SkipPushLocalKubernetes: true,
Expand All @@ -24,10 +25,14 @@ Starts your project in development mode
#######################################################`,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd, args, f, "dev", "devCommand")
return cmd.Run(cobraCmd, args, f, "devCommand")
},
}

cmd.AddFlags(devCmd)
var pipeline *latest.Pipeline
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
pipeline = rawConfig.Config.Pipelines["dev"]
}
cmd.AddPipelineFlags(f, devCmd, pipeline)
return devCmd
}
70 changes: 0 additions & 70 deletions cmd/overwrite_command.go

This file was deleted.

11 changes: 8 additions & 3 deletions cmd/purge.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"github.com/loft-sh/devspace/pkg/util/factory"

"github.com/loft-sh/devspace/cmd/flags"
"github.com/spf13/cobra"
)

// NewPurgeCmd creates a new purge command
func NewPurgeCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
func NewPurgeCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
cmd := &RunPipelineCmd{
GlobalFlags: globalFlags,
Pipeline: "purge",
Expand All @@ -27,10 +28,14 @@ devspace purge
#######################################################`,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd, args, f, "purge", "purgeCommand")
return cmd.Run(cobraCmd, args, f, "purgeCommand")
},
}

cmd.AddFlags(purgeCmd)
var pipeline *latest.Pipeline
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
pipeline = rawConfig.Config.Pipelines["purge"]
}
cmd.AddPipelineFlags(f, purgeCmd, pipeline)
return purgeCmd
}
11 changes: 8 additions & 3 deletions cmd/render.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"os"

"github.com/loft-sh/devspace/cmd/flags"
Expand All @@ -9,7 +10,7 @@ import (
)

// NewRenderCmd creates a new devspace render command
func NewRenderCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
func NewRenderCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
cmd := &RunPipelineCmd{
GlobalFlags: globalFlags,
SkipPushLocalKubernetes: true,
Expand All @@ -31,10 +32,14 @@ deployment.
#######################################################`,
RunE: func(cobraCmd *cobra.Command, args []string) error {
f.GetLog().Warnf("This command is deprecated, please use 'devspace deploy --render' instead")
return cmd.Run(cobraCmd, args, f, "render", "renderCommand")
return cmd.Run(cobraCmd, args, f, "renderCommand")
},
}

cmd.AddFlags(renderCmd)
var pipeline *latest.Pipeline
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
pipeline = rawConfig.Config.Pipelines["deploy"]
}
cmd.AddPipelineFlags(f, renderCmd, pipeline)
return renderCmd
}
30 changes: 9 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Additional run commands:
rootCmd.AddCommand(NewInitCmd(f))
rootCmd.AddCommand(NewRestartCmd(f, globalFlags))
rootCmd.AddCommand(NewSyncCmd(f, globalFlags))
rootCmd.AddCommand(NewRenderCmd(f, globalFlags))
rootCmd.AddCommand(NewRenderCmd(f, globalFlags, rawConfig))
rootCmd.AddCommand(NewUpgradeCmd())
rootCmd.AddCommand(NewEnterCmd(f, globalFlags))
rootCmd.AddCommand(NewAnalyzeCmd(f, globalFlags))
Expand All @@ -256,33 +256,21 @@ Additional run commands:
rootCmd.AddCommand(NewRunCmd(f, globalFlags, rawConfig))
rootCmd.AddCommand(NewAttachCmd(f, globalFlags))
rootCmd.AddCommand(NewPrintCmd(f, globalFlags))
rootCmd.AddCommand(NewRunPipelineCmd(f, globalFlags))
rootCmd.AddCommand(NewRunPipelineCmd(f, globalFlags, rawConfig))
rootCmd.AddCommand(NewCompletionCmd())

// check overwrite commands
rootCmd.AddCommand(replaceCommand("dev", rawConfig, f, globalFlags, NewDevCmd))
rootCmd.AddCommand(replaceCommand("deploy", rawConfig, f, globalFlags, NewDeployCmd))
rootCmd.AddCommand(replaceCommand("build", rawConfig, f, globalFlags, NewBuildCmd))
rootCmd.AddCommand(replaceCommand("purge", rawConfig, f, globalFlags, NewPurgeCmd))
rootCmd.AddCommand(NewDevCmd(f, globalFlags, rawConfig))
rootCmd.AddCommand(NewDeployCmd(f, globalFlags, rawConfig))
rootCmd.AddCommand(NewBuildCmd(f, globalFlags, rawConfig))
rootCmd.AddCommand(NewPurgeCmd(f, globalFlags, rawConfig))

// Add plugin commands
plugin.AddPluginCommands(rootCmd, plugins, "")
variable.AddPredefinedVars(plugins)
return rootCmd
}

func replaceCommand(command string, rawConfig *RawConfig, f factory.Factory, globalFlags *flags.GlobalFlags, fallback func(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command) *cobra.Command {
if rawConfig != nil && rawConfig.CommandsConfig != nil && rawConfig.Resolver != nil && rawConfig.CommandsConfig.Commands != nil {
// get command
overwriteCommand, ok := rawConfig.CommandsConfig.Commands[command]
if ok && !overwriteCommand.DisableReplace {
return NewOverwriteCmd(f, globalFlags, overwriteCommand, rawConfig.Resolver.ResolvedVariables())
}
}

return fallback(f, globalFlags)
}

func disableKlog() {
flagSet := &flag.FlagSet{}
klog.InitFlags(flagSet)
Expand Down Expand Up @@ -338,7 +326,7 @@ type RawConfig struct {
RawConfig map[string]interface{}
Resolver variable.Resolver

CommandsConfig *latest.Config
Config *latest.Config

resolvedMutex sync.Mutex
resolved map[string]string
Expand All @@ -357,8 +345,8 @@ func (r *RawConfig) Parse(
r.Resolver = resolver

// try parsing commands
latestConfig, beforeConversion, err := loader.NewCommandsParser().Parse(ctx, originalRawConfig, rawConfig, resolver, log)
r.CommandsConfig = latestConfig
latestConfig, beforeConversion, err := loader.NewCommandsPipelinesParser().Parse(ctx, originalRawConfig, rawConfig, resolver, log)
r.Config = latestConfig
return latestConfig, beforeConversion, err
}

Expand Down
63 changes: 60 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ devspace --dependency my-dependency run any-command --any-command-flag
return cmd.RunRun(f, args)
}

if rawConfig != nil && rawConfig.CommandsConfig != nil {
for _, cmd := range rawConfig.CommandsConfig.Commands {
runCmd.AddCommand(NewOverwriteCmd(f, globalFlags, cmd, rawConfig.Resolver.ResolvedVariables()))
if rawConfig != nil && rawConfig.Config != nil {
for _, cmd := range rawConfig.Config.Commands {
runCmd.AddCommand(NewSpecificRunCommand(f, globalFlags, cmd, rawConfig.Resolver.ResolvedVariables()))
}
}
runCmd.Flags().StringVar(&cmd.Dependency, "dependency", "", "Run a command from a specific dependency")
Expand Down Expand Up @@ -308,3 +308,60 @@ func ExecuteCommand(ctx context.Context, cmd *latest.CommandConfig, variables ma
shellArgs = append(shellArgs, args...)
return command.CommandWithEnv(ctx, dir, stdout, stderr, stdin, extraEnv, shellCommand, shellArgs...)
}

// RunCommandCmd holds the cmd flags of an run command
type RunCommandCmd struct {
*flags.GlobalFlags

Command *latest.CommandConfig
Variables map[string]interface{}

Stdout io.Writer
Stderr io.Writer
}

// NewSpecificRunCommand creates a new run command
func NewSpecificRunCommand(f factory.Factory, globalFlags *flags.GlobalFlags, command *latest.CommandConfig, variables map[string]interface{}) *cobra.Command {
cmd := &RunCommandCmd{
GlobalFlags: globalFlags,
Command: command,
Variables: variables,
Stdout: os.Stdout,
Stderr: os.Stderr,
}

description := command.Description
longDescription := command.Description
if description == "" {
description = "Runs command: " + command.Name
longDescription = description
}
if len(description) > 64 {
if len(description) > 64 {
description = description[:61] + "..."
}
}

runCmd := &cobra.Command{
Use: command.Name,
Short: description,
Long: longDescription,
Args: cobra.ArbitraryArgs,
RunE: func(cobraCmd *cobra.Command, _ []string) error {
args, err := ParseArgs(cobraCmd, cmd.GlobalFlags, f.GetLog())
if err != nil {
return err
}

plugin.SetPluginCommand(cobraCmd, args)
return cmd.Run(f, args)
},
}
runCmd.DisableFlagParsing = true
return runCmd
}

func (cmd *RunCommandCmd) Run(f factory.Factory, args []string) error {
devCtx := devspacecontext.NewContext(context.Background(), cmd.Variables, f.GetLog())
return executeCommandWithAfter(devCtx.Context(), cmd.Command, args, cmd.Variables, devCtx.WorkingDir(), cmd.Stdout, cmd.Stderr, os.Stdin, devCtx.Log())
}
Loading