@@ -17,17 +17,17 @@ limitations under the License.
17
17
package cli
18
18
19
19
import (
20
+ "errors"
20
21
"fmt"
21
- "log"
22
22
"os"
23
23
"sort"
24
24
"strconv"
25
25
"strings"
26
26
27
27
"github.com/spf13/cobra"
28
28
29
- internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
30
29
"sigs.k8s.io/kubebuilder/v3/pkg/config"
30
+ yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
31
31
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
32
32
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
33
33
)
@@ -136,28 +136,28 @@ func (c CLI) bindInit(ctx plugin.Context, cmd *cobra.Command) {
136
136
return
137
137
}
138
138
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
-
145
139
subcommand := initPlugin .GetInitSubcommand ()
146
- subcommand .InjectConfig (cfg .Config )
147
140
subcommand .BindFlags (cmd .Flags ())
148
141
subcommand .UpdateContext (& ctx )
149
142
cmd .Long = ctx .Description
150
143
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 )
157
151
}
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 )
160
156
}
161
- return cfg .Save ()
157
+
158
+ subcommand .InjectConfig (cfg .Config ())
159
+ return nil
162
160
}
161
+ cmd .RunE = runECmdFunc (c .fs , subcommand , msg )
162
+ cmd .PostRunE = postRunECmdFunc (cfg , msg )
163
163
}
0 commit comments