Skip to content

Commit 2ef10a5

Browse files
committed
fix: plugin loading, added relative values reader
1 parent 422a095 commit 2ef10a5

File tree

9 files changed

+135
-47
lines changed

9 files changed

+135
-47
lines changed

cmd/apps.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cmd
2+
3+
import (
4+
"github.com/outblocks/outblocks-cli/pkg/config"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func (e *Executor) newAppsCmd() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "apps",
11+
Aliases: []string{"app", "application", "applications"},
12+
Short: "App management",
13+
Long: `Applications management - list, add or delete.`,
14+
Annotations: map[string]string{
15+
cmdGroupAnnotation: cmdGroupMain,
16+
},
17+
}
18+
19+
list := &cobra.Command{
20+
Use: "list",
21+
Short: "List apps",
22+
Long: `List configured applications.`,
23+
Annotations: map[string]string{
24+
cmdGroupAnnotation: cmdGroupMain,
25+
},
26+
SilenceUsage: true,
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
// TODO: app list
29+
return nil
30+
},
31+
}
32+
33+
del := &cobra.Command{
34+
Use: "delete",
35+
Aliases: []string{"del", "remove"},
36+
Short: "Delete an app",
37+
Long: `Delete an existing application config.`,
38+
Annotations: map[string]string{
39+
cmdGroupAnnotation: cmdGroupMain,
40+
cmdSkipLoadPluginsAnnotation: "1",
41+
},
42+
SilenceUsage: true,
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
if e.cfg == nil {
45+
return config.ErrProjectConfigNotFound
46+
}
47+
48+
// TODO: app deletion
49+
return nil
50+
},
51+
}
52+
53+
add := &cobra.Command{
54+
Use: "add",
55+
Short: "Add a new app",
56+
Long: `Add a new application (generates new config).`,
57+
Annotations: map[string]string{
58+
cmdGroupAnnotation: cmdGroupMain,
59+
},
60+
SilenceUsage: true,
61+
RunE: func(cmd *cobra.Command, args []string) error {
62+
// TODO: app add
63+
return nil
64+
},
65+
}
66+
67+
cmd.AddCommand(
68+
list,
69+
del,
70+
add,
71+
)
72+
73+
return cmd
74+
}

cmd/executor_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/outblocks/outblocks-cli/pkg/plugins"
1212
)
1313

14-
func (e *Executor) loadProjectConfig(ctx context.Context, vals map[string]interface{}, skipLoadPlugins bool) error {
15-
cfg, err := config.LoadProjectConfig(vals, &config.ProjectOptions{
14+
func (e *Executor) loadProjectConfig(ctx context.Context, cfgPath string, vals map[string]interface{}, skipLoadPlugins bool) error {
15+
cfg, err := config.LoadProjectConfig(cfgPath, vals, &config.ProjectOptions{
1616
Env: e.opts.env,
1717
})
1818
if err != nil {
@@ -29,7 +29,7 @@ func (e *Executor) loadProjectConfig(ctx context.Context, vals map[string]interf
2929

3030
e.loader = plugins.NewLoader(cfg.Path, e.PluginsCacheDir())
3131

32-
if skipLoadPlugins {
32+
if !skipLoadPlugins {
3333
if err := cfg.LoadPlugins(ctx, e.log, e.loader); err != nil {
3434
return err
3535
}

cmd/init.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ func (e *Executor) newInitCmd() *cobra.Command {
1313
Short: "Initialize new config",
1414
Long: `Initialize new Outblocks project config with opinionated defaults.`,
1515
Annotations: map[string]string{
16-
cmdGroupAnnotation: cmdGroupMain,
17-
cmdSkipLoadPluginsAnnotation: "1",
16+
cmdGroupAnnotation: cmdGroupMain,
17+
cmdSkipLoadConfigAnnotation: "1",
1818
},
1919
SilenceUsage: true,
2020
RunE: func(cmd *cobra.Command, args []string) error {
21-
return actions.NewInit(e.Log(), e.loader, e.PluginsCacheDir(), opts).Run(cmd.Context(), e.cfg)
21+
return actions.NewInit(e.Log(), e.PluginsCacheDir(), opts).Run(cmd.Context())
2222
},
2323
}
2424

2525
f := cmd.Flags()
26-
f.BoolVar(&opts.Overwrite, "overwrite", false, "do not ask if project file alrady exists")
26+
f.BoolVar(&opts.Overwrite, "overwrite", false, "do not ask if project file already exists")
2727
f.StringVar(&opts.Name, "name", "", "project name")
2828
f.StringVar(&opts.DeployPlugin, "deploy-plugin", "", "deploy plugin to use (e.g. gcp)")
2929
f.StringVar(&opts.RunPlugin, "run-plugin", "", "deploy plugin to use (e.g. docker)")

cmd/plugins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func (e *Executor) newPluginsCmd() *cobra.Command {
1111
Use: "plugins",
1212
Aliases: []string{"plugin"},
1313
Short: "Plugin management",
14-
Long: `Plugin management - list, update, add or remove.`,
14+
Long: `Plugin management - list, update, add or delete.`,
1515
Annotations: map[string]string{
1616
cmdGroupAnnotation: cmdGroupMain,
1717
},

cmd/root.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"os"
8+
"path/filepath"
79
"sort"
810
"strings"
911

12+
"github.com/outblocks/outblocks-cli/internal/fileutil"
1013
"github.com/outblocks/outblocks-cli/internal/version"
1114
"github.com/outblocks/outblocks-cli/pkg/cli/values"
1215
"github.com/outblocks/outblocks-cli/pkg/config"
@@ -18,6 +21,7 @@ import (
1821
)
1922

2023
const (
24+
cmdSkipLoadConfigAnnotation = "cmd_skip_load_config"
2125
cmdSkipLoadPluginsAnnotation = "cmd_skip_load_plugins"
2226
cmdGroupAnnotation = "cmd_group"
2327
cmdGroupDelimiter = "-"
@@ -263,14 +267,26 @@ func (e *Executor) newRoot() *cobra.Command {
263267
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
264268
e.opts.env = e.v.GetString("env")
265269

270+
skipLoadConfig := cmd.Annotations[cmdSkipLoadConfigAnnotation] == "1"
271+
if skipLoadConfig {
272+
return nil
273+
}
274+
266275
// Load values.
267276
for i, v := range e.opts.valueOpts.ValueFiles {
268277
e.opts.valueOpts.ValueFiles[i] = strings.ReplaceAll(v, "<env>", e.opts.env)
269278
}
270279

271280
defValuesYAML := strings.ReplaceAll(defaultValuesYAML, "<env>", e.opts.env)
272281

273-
v, err := e.opts.valueOpts.MergeValues(cmd.Context(), getter.All())
282+
pwd, err := os.Getwd()
283+
if err != nil {
284+
return fmt.Errorf("cannot find current directory: %w", err)
285+
}
286+
287+
cfgPath := fileutil.FindYAMLGoingUp(pwd, config.ProjectYAMLName)
288+
289+
v, err := e.opts.valueOpts.MergeValues(cmd.Context(), filepath.Dir(cfgPath), getter.All())
274290
if err != nil && (len(e.opts.valueOpts.ValueFiles) != 1 || e.opts.valueOpts.ValueFiles[0] != defValuesYAML) {
275291
return err
276292
}
@@ -279,7 +295,7 @@ func (e *Executor) newRoot() *cobra.Command {
279295
skipLoadPlugins := cmd.Annotations[cmdSkipLoadPluginsAnnotation] == "1"
280296

281297
// Load config file.
282-
if err := e.loadProjectConfig(cmd.Context(), vals, skipLoadPlugins); err != nil && !errors.Is(err, config.ErrProjectConfigNotFound) {
298+
if err := e.loadProjectConfig(cmd.Context(), cfgPath, vals, skipLoadPlugins); err != nil && !errors.Is(err, config.ErrProjectConfigNotFound) {
283299
return err
284300
}
285301

@@ -317,6 +333,7 @@ func (e *Executor) newRoot() *cobra.Command {
317333
e.newPluginsCmd(),
318334
e.newForceUnlockCmd(),
319335
e.newInitCmd(),
336+
e.newAppsCmd(),
320337
)
321338

322339
return cmd

pkg/actions/init.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/AlecAivazis/survey/v2/terminal"
1616
"github.com/Masterminds/sprig"
1717
"github.com/goccy/go-yaml"
18+
"github.com/outblocks/outblocks-cli/internal/fileutil"
1819
"github.com/outblocks/outblocks-cli/pkg/config"
1920
"github.com/outblocks/outblocks-cli/pkg/logger"
2021
"github.com/outblocks/outblocks-cli/pkg/plugins"
@@ -29,7 +30,6 @@ var (
2930

3031
type Init struct {
3132
log logger.Logger
32-
loader *plugins.Loader
3333
pluginCacheDir string
3434
opts *InitOptions
3535
}
@@ -48,10 +48,9 @@ type InitOptions struct {
4848
}
4949
}
5050

51-
func NewInit(log logger.Logger, loader *plugins.Loader, pluginCacheDir string, opts *InitOptions) *Init {
51+
func NewInit(log logger.Logger, pluginCacheDir string, opts *InitOptions) *Init {
5252
return &Init{
5353
log: log,
54-
loader: loader,
5554
pluginCacheDir: pluginCacheDir,
5655
opts: opts,
5756
}
@@ -63,17 +62,16 @@ func funcMap() template.FuncMap {
6362
}
6463
}
6564

66-
func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
65+
func (d *Init) Run(ctx context.Context) error {
6766
curDir, err := os.Getwd()
6867
if err != nil {
6968
return fmt.Errorf("getting current working dir error: %w", err)
7069
}
7170

72-
if cfg == nil {
73-
cfg = &config.Project{}
71+
cfg := &config.Project{}
72+
loader := plugins.NewLoader(curDir, d.pluginCacheDir)
7473

75-
d.loader = plugins.NewLoader(curDir, d.pluginCacheDir)
76-
} else if !d.opts.Overwrite {
74+
if !d.opts.Overwrite && fileutil.FindYAML(filepath.Join(curDir, config.ProjectYAMLName)) != "" {
7775
proceed := false
7876
prompt := &survey.Confirm{
7977
Message: "Project config already exists! Do you want to overwrite it?",
@@ -87,7 +85,7 @@ func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
8785
}
8886
}
8987

90-
cfg, err = d.prompt(ctx, cfg, curDir)
88+
cfg, err = d.prompt(ctx, cfg, loader, curDir)
9189
if errors.Is(err, errInitCanceled) {
9290
d.log.Println("Init canceled.")
9391
return nil
@@ -97,7 +95,7 @@ func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
9795
return err
9896
}
9997

100-
err = cfg.LoadPlugins(ctx, d.log, d.loader)
98+
err = cfg.LoadPlugins(ctx, d.log, loader)
10199
if err != nil {
102100
return err
103101
}
@@ -160,7 +158,7 @@ func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
160158
return err
161159
}
162160

163-
func (d *Init) prompt(ctx context.Context, cfg *config.Project, curDir string) (*config.Project, error) {
161+
func (d *Init) prompt(ctx context.Context, cfg *config.Project, loader *plugins.Loader, curDir string) (*config.Project, error) {
164162
var qs []*survey.Question
165163

166164
if d.opts.Name == "" {
@@ -228,12 +226,12 @@ func (d *Init) prompt(ctx context.Context, cfg *config.Project, curDir string) (
228226

229227
cfg.Name = answers.Name
230228

231-
_, latestDeployVersion, err := d.loader.MatchingVersion(ctx, answers.DeployPlugin, "", nil)
229+
_, latestDeployVersion, err := loader.MatchingVersion(ctx, answers.DeployPlugin, "", nil)
232230
if err != nil {
233231
return nil, fmt.Errorf("error retrieving latest version of plugin '%s': %w", answers.RunPlugin, err)
234232
}
235233

236-
_, latestRunVersion, err := d.loader.MatchingVersion(ctx, answers.RunPlugin, "", nil)
234+
_, latestRunVersion, err := loader.MatchingVersion(ctx, answers.RunPlugin, "", nil)
237235
if err != nil {
238236
return nil, fmt.Errorf("error retrieving latest version of plugin '%s': %w", answers.RunPlugin, err)
239237
}

pkg/cli/values/options.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package values
33

44
import (
55
"context"
6+
"errors"
67
"fmt"
78
"io/ioutil"
89
"net/url"
910
"os"
11+
"path/filepath"
1012
"strings"
1113

1214
"github.com/goccy/go-yaml"
@@ -19,16 +21,30 @@ type Options struct {
1921
Values []string
2022
}
2123

24+
func (opts *Options) readFile(ctx context.Context, rootPath, filePath string, p getter.Providers) ([]byte, error) {
25+
bytes, err := readFile(ctx, filePath, p)
26+
if err != nil {
27+
var perr *os.PathError
28+
if errors.As(err, &perr) && !filepath.IsAbs(filePath) && rootPath != "" {
29+
// Try different cfg root path.
30+
filePath = filepath.Join(rootPath, filePath)
31+
return ioutil.ReadFile(filePath)
32+
}
33+
}
34+
35+
return bytes, err
36+
}
37+
2238
// MergeValues merges values from files specified via -f/--values and directly
2339
// via --set marshaling them to YAML.
24-
func (opts *Options) MergeValues(ctx context.Context, p getter.Providers) (map[string]interface{}, error) {
40+
func (opts *Options) MergeValues(ctx context.Context, root string, p getter.Providers) (map[string]interface{}, error) {
2541
base := map[string]interface{}{}
2642

2743
// User specified a values files via -f/--values
2844
for _, filePath := range opts.ValueFiles {
2945
currentMap := map[string]interface{}{}
3046

31-
bytes, err := readFile(ctx, filePath, p)
47+
bytes, err := opts.readFile(ctx, root, filePath, p)
3248
if err != nil {
3349
return nil, err
3450
}

pkg/config/project.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io/ioutil"
7-
"os"
87
"path/filepath"
98
"strings"
109

@@ -61,34 +60,28 @@ type ProjectOptions struct {
6160
Env string
6261
}
6362

64-
func LoadProjectConfig(vars map[string]interface{}, opts *ProjectOptions) (*Project, error) {
65-
pwd, err := os.Getwd()
66-
if err != nil {
67-
return nil, fmt.Errorf("cannot find current directory: %w", err)
68-
}
69-
70-
f := fileutil.FindYAMLGoingUp(pwd, ProjectYAMLName)
71-
if f == "" {
63+
func LoadProjectConfig(cfgPath string, vars map[string]interface{}, opts *ProjectOptions) (*Project, error) {
64+
if cfgPath == "" {
7265
return nil, ErrProjectConfigNotFound
7366
}
7467

75-
data, err := ioutil.ReadFile(f)
68+
data, err := ioutil.ReadFile(cfgPath)
7669
if err != nil {
7770
return nil, fmt.Errorf("cannot read project yaml: %w", err)
7871
}
7972

8073
// Process lockfile.
8174
var lock *lockfile.Lockfile
8275

83-
lockPath := filepath.Join(filepath.Dir(f), LockfileName)
76+
lockPath := filepath.Join(filepath.Dir(cfgPath), LockfileName)
8477
if fileutil.FileExists(lockPath) {
8578
lock, err = lockfile.LoadLockfile(lockPath)
8679
if err != nil {
8780
return nil, err
8881
}
8982
}
9083

91-
p, err := LoadProjectConfigData(f, data, vars, opts, lock)
84+
p, err := LoadProjectConfigData(cfgPath, data, vars, opts, lock)
9285
if err != nil {
9386
return nil, err
9487
}

pkg/logger/logger.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,6 @@ func (l *Log) Errorf(format string, a ...interface{}) {
135135

136136
if l.logLevel == LogLevelDebug {
137137
_, fileName, line, _ := runtime.Caller(1)
138-
fmt.Println(fileName, line)
139-
_, fileName, line, _ = runtime.Caller(2)
140-
fmt.Println(fileName, line)
141-
_, fileName, line, _ = runtime.Caller(3)
142-
fmt.Println(fileName, line)
143-
_, fileName, line, _ = runtime.Caller(0)
144-
fmt.Println(fileName, line)
145-
_, fileName, line, _ = runtime.Caller(4)
146-
fmt.Println(fileName, line)
147-
148138
pterm.Println(pterm.FgGray.Sprint("└ " + fmt.Sprintf("(%s:%d)", fileName, line)))
149139
}
150140
}

0 commit comments

Comments
 (0)