Skip to content

Commit cd8812d

Browse files
authored
Merge pull request #2083 from Adirio/export-config-machinery
🌱 Export config machinery
2 parents 73cda2b + f7ad796 commit cd8812d

22 files changed

+661
-374
lines changed

pkg/cli/api.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24-
"sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
24+
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
2525
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2626
)
2727

@@ -76,18 +76,15 @@ func (c CLI) bindCreateAPI(ctx plugin.Context, cmd *cobra.Command) {
7676
return
7777
}
7878

79-
cfg, err := config.LoadInitialized(c.fs)
80-
if err != nil {
81-
cmdErr(cmd, err)
82-
return
83-
}
84-
8579
subcommand := createAPIPlugin.GetCreateAPISubcommand()
86-
subcommand.InjectConfig(cfg.Config)
8780
subcommand.BindFlags(cmd.Flags())
8881
subcommand.UpdateContext(&ctx)
8982
cmd.Long = ctx.Description
9083
cmd.Example = ctx.Examples
91-
cmd.RunE = runECmdFunc(c.fs, cfg, subcommand,
92-
fmt.Sprintf("failed to create API with %q", plugin.KeyFor(createAPIPlugin)))
84+
85+
cfg := yamlstore.New(c.fs)
86+
msg := fmt.Sprintf("failed to create API with %q", plugin.KeyFor(createAPIPlugin))
87+
cmd.PreRunE = preRunECmdFunc(subcommand, cfg, msg)
88+
cmd.RunE = runECmdFunc(c.fs, subcommand, msg)
89+
cmd.PostRunE = postRunECmdFunc(cfg, msg)
9390
}

pkg/cli/cli.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cli
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223
"strings"
@@ -25,8 +26,8 @@ import (
2526
"github.com/spf13/cobra"
2627
"github.com/spf13/pflag"
2728

28-
internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
2929
"sigs.k8s.io/kubebuilder/v3/pkg/config"
30+
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
3031
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
3132
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
3233
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -196,16 +197,17 @@ func (c *CLI) getInfoFromFlags() (string, []string, error) {
196197
// getInfoFromConfigFile obtains the project version and plugin keys from the project config file.
197198
func (c CLI) getInfoFromConfigFile() (config.Version, []string, error) {
198199
// Read the project configuration file
199-
projectConfig, err := internalconfig.Read(c.fs)
200+
cfg := yamlstore.New(c.fs)
201+
err := cfg.Load()
200202
switch {
201203
case err == nil:
202-
case os.IsNotExist(err):
204+
case errors.Is(err, os.ErrNotExist):
203205
return config.Version{}, nil, nil
204206
default:
205207
return config.Version{}, nil, err
206208
}
207209

208-
return getInfoFromConfig(projectConfig)
210+
return getInfoFromConfig(cfg.Config())
209211
}
210212

211213
// getInfoFromConfig obtains the project version and plugin keys from the project config.

pkg/cli/cmd_helpers.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ package cli
1818

1919
import (
2020
"fmt"
21+
"os"
2122

2223
"github.com/spf13/cobra"
2324

24-
"sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/config/store"
2526
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2627
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2728
)
@@ -46,18 +47,39 @@ func errCmdFunc(err error) func(*cobra.Command, []string) error {
4647
}
4748
}
4849

49-
// runECmdFunc returns a cobra RunE function that runs subcommand and saves the
50-
// config, which may have been modified by subcommand.
51-
func runECmdFunc(
52-
fs machinery.Filesystem,
53-
c *config.Config,
54-
subcommand plugin.Subcommand,
55-
msg string,
56-
) func(*cobra.Command, []string) error {
50+
// preRunECmdFunc returns a cobra PreRunE function that loads the configuration file
51+
// and injects it into the subcommand
52+
func preRunECmdFunc(subcmd plugin.Subcommand, cfg store.Store, msg string) func(*cobra.Command, []string) error {
5753
return func(*cobra.Command, []string) error {
58-
if err := subcommand.Run(fs); err != nil {
54+
err := cfg.Load()
55+
if os.IsNotExist(err) {
56+
return fmt.Errorf("%s: unable to find configuration file, project must be initialized", msg)
57+
} else if err != nil {
58+
return fmt.Errorf("%s: unable to load configuration file: %w", msg, err)
59+
}
60+
61+
subcmd.InjectConfig(cfg.Config())
62+
return nil
63+
}
64+
}
65+
66+
// runECmdFunc returns a cobra RunE function that runs subcommand
67+
func runECmdFunc(fs machinery.Filesystem, subcmd plugin.Subcommand, msg string) func(*cobra.Command, []string) error {
68+
return func(*cobra.Command, []string) error {
69+
if err := subcmd.Run(fs); err != nil {
5970
return fmt.Errorf("%s: %v", msg, err)
6071
}
61-
return c.Save()
72+
return nil
73+
}
74+
}
75+
76+
// postRunECmdFunc returns a cobra PostRunE function that saves the configuration file
77+
func postRunECmdFunc(cfg store.Store, msg string) func(*cobra.Command, []string) error {
78+
return func(*cobra.Command, []string) error {
79+
err := cfg.Save()
80+
if err != nil {
81+
return fmt.Errorf("%s: unable to save configuration file: %w", msg, err)
82+
}
83+
return nil
6284
}
6385
}

pkg/cli/edit.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24-
"sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
24+
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
2525
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2626
)
2727

@@ -76,18 +76,15 @@ func (c CLI) bindEdit(ctx plugin.Context, cmd *cobra.Command) {
7676
return
7777
}
7878

79-
cfg, err := config.LoadInitialized(c.fs)
80-
if err != nil {
81-
cmdErr(cmd, err)
82-
return
83-
}
84-
8579
subcommand := editPlugin.GetEditSubcommand()
86-
subcommand.InjectConfig(cfg.Config)
8780
subcommand.BindFlags(cmd.Flags())
8881
subcommand.UpdateContext(&ctx)
8982
cmd.Long = ctx.Description
9083
cmd.Example = ctx.Examples
91-
cmd.RunE = runECmdFunc(c.fs, cfg, subcommand,
92-
fmt.Sprintf("failed to edit project with %q", plugin.KeyFor(editPlugin)))
84+
85+
cfg := yamlstore.New(c.fs)
86+
msg := fmt.Sprintf("failed to edit project with %q", plugin.KeyFor(editPlugin))
87+
cmd.PreRunE = preRunECmdFunc(subcommand, cfg, msg)
88+
cmd.RunE = runECmdFunc(c.fs, subcommand, msg)
89+
cmd.PostRunE = postRunECmdFunc(cfg, msg)
9390
}

pkg/cli/init.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ limitations under the License.
1717
package cli
1818

1919
import (
20+
"errors"
2021
"fmt"
21-
"log"
2222
"os"
2323
"sort"
2424
"strconv"
2525
"strings"
2626

2727
"github.com/spf13/cobra"
2828

29-
internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
3029
"sigs.k8s.io/kubebuilder/v3/pkg/config"
30+
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
3131
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
3232
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
3333
)
@@ -136,28 +136,28 @@ func (c CLI) bindInit(ctx plugin.Context, cmd *cobra.Command) {
136136
return
137137
}
138138

139-
cfg, err := internalconfig.New(c.fs, c.projectVersion, internalconfig.DefaultPath)
140-
if err != nil {
141-
cmdErr(cmd, fmt.Errorf("unable to initialize the project configuration: %w", err))
142-
return
143-
}
144-
145139
subcommand := initPlugin.GetInitSubcommand()
146-
subcommand.InjectConfig(cfg.Config)
147140
subcommand.BindFlags(cmd.Flags())
148141
subcommand.UpdateContext(&ctx)
149142
cmd.Long = ctx.Description
150143
cmd.Example = ctx.Examples
151-
cmd.RunE = func(*cobra.Command, []string) error {
152-
// Check if a config is initialized in the command runner so the check
153-
// doesn't erroneously fail other commands used in initialized projects.
154-
_, err := internalconfig.Read(c.fs)
155-
if err == nil || os.IsExist(err) {
156-
log.Fatal("config already initialized")
144+
145+
cfg := yamlstore.New(c.fs)
146+
msg := fmt.Sprintf("failed to initialize project with %q", plugin.KeyFor(initPlugin))
147+
cmd.PreRunE = func(*cobra.Command, []string) error {
148+
// Check if a config is initialized.
149+
if err := cfg.Load(); err == nil || !errors.Is(err, os.ErrNotExist) {
150+
return fmt.Errorf("%s: already initialized", msg)
157151
}
158-
if err := subcommand.Run(c.fs); err != nil {
159-
return fmt.Errorf("failed to initialize project with %q: %v", plugin.KeyFor(initPlugin), err)
152+
153+
err := cfg.New(c.projectVersion)
154+
if err != nil {
155+
return fmt.Errorf("%s: error initializing project configuration: %w", msg, err)
160156
}
161-
return cfg.Save()
157+
158+
subcommand.InjectConfig(cfg.Config())
159+
return nil
162160
}
161+
cmd.RunE = runECmdFunc(c.fs, subcommand, msg)
162+
cmd.PostRunE = postRunECmdFunc(cfg, msg)
163163
}

0 commit comments

Comments
 (0)