Skip to content

Commit c2b5090

Browse files
authored
Merge pull request #1802 from FabianKramm/master
feat: new --dependency flag for devspace print
2 parents 6e7de0e + a8e273e commit c2b5090

File tree

4 files changed

+76
-59
lines changed

4 files changed

+76
-59
lines changed

cmd/print.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/loft-sh/devspace/pkg/devspace/hook"
6-
"io"
7-
"os"
8-
"path/filepath"
9-
105
"github.com/loft-sh/devspace/pkg/devspace/config"
116
"github.com/loft-sh/devspace/pkg/devspace/dependency"
7+
"github.com/loft-sh/devspace/pkg/devspace/hook"
128
"github.com/loft-sh/devspace/pkg/devspace/plugin"
9+
"io"
10+
"os"
1311

1412
"github.com/loft-sh/devspace/cmd/flags"
15-
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
1613
"github.com/loft-sh/devspace/pkg/util/factory"
1714
logger "github.com/loft-sh/devspace/pkg/util/log"
1815
"github.com/loft-sh/devspace/pkg/util/message"
@@ -28,6 +25,8 @@ type PrintCmd struct {
2825

2926
Out io.Writer
3027
SkipInfo bool
28+
29+
Dependency string
3130
}
3231

3332
// NewPrintCmd creates a new devspace print command
@@ -54,6 +53,7 @@ profile after all patching and variable substitution
5453
}
5554

5655
printCmd.Flags().BoolVar(&cmd.SkipInfo, "skip-info", false, "When enabled, only prints the configuration without additional information")
56+
printCmd.Flags().StringVar(&cmd.Dependency, "dependency", "", "The dependency to print the config from. Use dot to access nested dependencies (e.g. dep1.dep2)")
5757

5858
return printCmd
5959
}
@@ -98,13 +98,22 @@ func (cmd *PrintCmd) Run(f factory.Factory) error {
9898
return err
9999
}
100100

101+
if cmd.Dependency != "" {
102+
dep := dependency.GetDependencyByPath(dependencies, cmd.Dependency)
103+
if dep == nil {
104+
return fmt.Errorf("couldn't find dependency %s: make sure it gets loaded correctly", cmd.Dependency)
105+
}
106+
107+
loadedConfig = dep.Config()
108+
}
109+
101110
bsConfig, err := yaml.Marshal(loadedConfig.Config())
102111
if err != nil {
103112
return err
104113
}
105114

106115
if !cmd.SkipInfo {
107-
err = printExtraInfo(cmd.ConfigPath, loadedConfig, log)
116+
err = printExtraInfo(loadedConfig, log)
108117
if err != nil {
109118
return err
110119
}
@@ -122,12 +131,7 @@ func (cmd *PrintCmd) Run(f factory.Factory) error {
122131
return nil
123132
}
124133

125-
func printExtraInfo(configPath string, config config.Config, log logger.Logger) error {
126-
absPath, err := filepath.Abs(loader.ConfigPath(configPath))
127-
if err != nil {
128-
return err
129-
}
130-
134+
func printExtraInfo(config config.Config, log logger.Logger) error {
131135
log.WriteString("\n-------------------\n\nVars:\n")
132136

133137
headerColumnNames := []string{"Name", "Value"}
@@ -146,7 +150,6 @@ func printExtraInfo(configPath string, config config.Config, log logger.Logger)
146150
log.Info("No vars found")
147151
}
148152

149-
log.WriteString("\n-------------------\n\nLoaded path: " + absPath + "\n\n-------------------\n\n")
150-
153+
log.WriteString("\n-------------------\n\nLoaded path: " + config.Path() + "\n\n-------------------\n\n")
151154
return nil
152155
}

cmd/run.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ func (cmd *RunCmd) RunRun(f factory.Factory, args []string) error {
143143

144144
// check if dependency command
145145
commandSplitted := strings.Split(args[0], ".")
146-
if len(commandSplitted) == 2 {
147-
cmd.Dependency = commandSplitted[0]
148-
args[0] = commandSplitted[1]
146+
if len(commandSplitted) > 1 {
147+
cmd.Dependency = strings.Join(commandSplitted[:len(commandSplitted)-1], ".")
148+
args[0] = commandSplitted[len(commandSplitted)-1]
149149
}
150150

151151
// Execute plugin hook
@@ -161,11 +161,19 @@ func (cmd *RunCmd) RunRun(f factory.Factory, args []string) error {
161161
return err
162162
}
163163

164-
return f.NewDependencyManager(config, nil, configOptions, f.GetLog()).Command(dependency.CommandOptions{
165-
Dependency: cmd.Dependency,
166-
Command: args[0],
167-
Args: args[1:],
164+
dependencies, err := f.NewDependencyManager(config, nil, configOptions, f.GetLog()).ResolveAll(dependency.ResolveOptions{
165+
Silent: true,
168166
})
167+
if err != nil {
168+
return err
169+
}
170+
171+
dep := dependency.GetDependencyByPath(dependencies, cmd.Dependency)
172+
if dep == nil {
173+
return fmt.Errorf("couldn't find dependency %s", cmd.Dependency)
174+
}
175+
176+
return dep.Command(args[0], args[1:])
169177
}
170178

171179
// load generated

pkg/devspace/dependency/dependency.go

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ type Manager interface {
5151

5252
// RenderAll renders all dependencies
5353
RenderAll(options RenderOptions) ([]types.Dependency, error)
54-
55-
// Command executes a dependency command
56-
Command(options CommandOptions) error
5754
}
5855

5956
type manager struct {
@@ -114,40 +111,6 @@ func (m *manager) ResolveAll(options ResolveOptions) ([]types.Dependency, error)
114111
return dependencies, nil
115112
}
116113

117-
// CommandOptions has all options for executing a command from a dependency
118-
type CommandOptions struct {
119-
Dependency string
120-
Command string
121-
Args []string
122-
UpdateDependencies bool
123-
Verbose bool
124-
}
125-
126-
// Command will execute a dependency command
127-
func (m *manager) Command(options CommandOptions) error {
128-
found := false
129-
_, err := m.handleDependencies(nil, []string{options.Dependency}, false, options.UpdateDependencies, true, options.Verbose, "Command", func(dependency *Dependency, log log.Logger) error {
130-
// Switch current working directory
131-
currentWorkingDirectory, err := dependency.prepare(true)
132-
if err != nil {
133-
return err
134-
} else if currentWorkingDirectory == "" {
135-
return nil
136-
}
137-
138-
// Change back to original working directory
139-
defer func() { _ = os.Chdir(currentWorkingDirectory) }()
140-
141-
found = true
142-
return ExecuteCommand(dependency.localConfig.Config().Commands, options.Command, options.Args, os.Stdout, os.Stderr)
143-
})
144-
if !found {
145-
return fmt.Errorf("couldn't find dependency %s", options.Dependency)
146-
}
147-
148-
return err
149-
}
150-
151114
// ExecuteCommand executes a given command from the available commands
152115
func ExecuteCommand(commands []*latest.CommandConfig, cmd string, args []string, stdout io.Writer, stderr io.Writer) error {
153116
err := command.ExecuteCommand(commands, cmd, args, stdout, stderr)
@@ -389,6 +352,32 @@ func (m *manager) handleDependencies(skipDependencies, filterDependencies []stri
389352
return retDependencies, nil
390353
}
391354

355+
func GetDependencyByPath(dependencies []types.Dependency, path string) types.Dependency {
356+
splitted := strings.Split(path, ".")
357+
358+
var retDependency types.Dependency
359+
searchDependencies := dependencies
360+
for _, segment := range splitted {
361+
var nextDependency types.Dependency
362+
for _, dependency := range searchDependencies {
363+
if dependency.Name() == segment {
364+
nextDependency = dependency
365+
break
366+
}
367+
}
368+
369+
// not found, exit here
370+
if nextDependency == nil {
371+
return nil
372+
}
373+
374+
searchDependencies = nextDependency.Children()
375+
retDependency = nextDependency
376+
}
377+
378+
return retDependency
379+
}
380+
392381
// Dependency holds the dependency config and has an id
393382
type Dependency struct {
394383
id string
@@ -429,6 +418,20 @@ func (d *Dependency) Root() bool { return d.root }
429418

430419
func (d *Dependency) BuiltImages() map[string]string { return d.builtImages }
431420

421+
func (d *Dependency) Command(command string, args []string) error {
422+
// Switch current working directory
423+
currentWorkingDirectory, err := d.prepare(true)
424+
if err != nil {
425+
return err
426+
} else if currentWorkingDirectory == "" {
427+
return nil
428+
}
429+
430+
// Change back to original working directory
431+
defer func() { _ = os.Chdir(currentWorkingDirectory) }()
432+
return ExecuteCommand(d.localConfig.Config().Commands, command, args, os.Stdout, os.Stderr)
433+
}
434+
432435
// Build builds and pushes all defined images
433436
func (d *Dependency) Build(forceDependencies bool, buildOptions *build.Options, log log.Logger) error {
434437
// Switch current working directory

pkg/devspace/dependency/types/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ type Dependency interface {
4040

4141
// StartPortForwarding starts the dependency port-forwarding
4242
StartPortForwarding(client kubectl.Client, interrupt chan error, logger log.Logger) error
43+
44+
// Command executes a command from the dependency config
45+
Command(command string, args []string) error
4346
}

0 commit comments

Comments
 (0)