Skip to content

Commit 4412b0b

Browse files
committed
refactor FindPackageRoot and handle error on FindPackageRootFrom calls
1 parent ecfeb9b commit 4412b0b

File tree

15 files changed

+80
-105
lines changed

15 files changed

+80
-105
lines changed

cmd/benchmark.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ func pipelineCommandAction(cmd *cobra.Command, args []string) error {
137137
return cobraext.FlagParsingError(err, cobraext.BenchNumTopProcsFlagName)
138138
}
139139

140-
packageRootPath, found, err := packages.FindPackageRoot()
141-
if !found {
142-
return errors.New("package root not found")
143-
}
140+
packageRootPath, err := packages.FindPackageRoot()
144141
if err != nil {
145142
return fmt.Errorf("locating package root failed: %w", err)
146143
}
@@ -292,12 +289,8 @@ func rallyCommandAction(cmd *cobra.Command, args []string) error {
292289
}
293290

294291
var packageRootPath string
295-
var found bool
296292
if len(packageName) == 0 {
297-
packageRootPath, found, err = packages.FindPackageRoot()
298-
if !found {
299-
return errors.New("package root not found")
300-
}
293+
packageRootPath, err = packages.FindPackageRoot()
301294
if err != nil {
302295
return fmt.Errorf("locating package root failed: %w", err)
303296
}
@@ -472,10 +465,7 @@ func streamCommandAction(cmd *cobra.Command, args []string) error {
472465
return cobraext.FlagParsingError(err, cobraext.BenchStreamTimestampFieldFlagName)
473466
}
474467

475-
packageRootPath, found, err := packages.FindPackageRoot()
476-
if !found {
477-
return errors.New("package root not found")
478-
}
468+
packageRootPath, err := packages.FindPackageRoot()
479469
if err != nil {
480470
return fmt.Errorf("locating package root failed: %w", err)
481471
}
@@ -585,10 +575,7 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
585575
return cobraext.FlagParsingError(err, cobraext.BenchReindexToMetricstoreFlagName)
586576
}
587577

588-
packageRootPath, found, err := packages.FindPackageRoot()
589-
if !found {
590-
return errors.New("package root not found")
591-
}
578+
packageRootPath, err := packages.FindPackageRoot()
592579
if err != nil {
593580
return fmt.Errorf("locating package root failed: %w", err)
594581
}

cmd/create_data_stream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ type newDataStreamAnswers struct {
3737
func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
3838
cmd.Println("Create a new data stream")
3939

40-
packageRoot, found, err := packages.FindPackageRoot()
40+
packageRoot, err := packages.FindPackageRoot()
4141
if err != nil {
42+
if errors.Is(err, packages.ErrPackageRootNotFound) {
43+
return errors.New("package root not found, you can only create new data stream in the package context")
44+
}
4245
return fmt.Errorf("locating package root failed: %w", err)
4346
}
44-
if !found {
45-
return errors.New("package root not found, you can only create new data stream in the package context")
46-
}
4747

4848
manifest, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
4949
if err != nil {

cmd/format.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"errors"
98
"fmt"
109

1110
"github.com/spf13/cobra"
@@ -35,13 +34,10 @@ func setupFormatCommand() *cobraext.Command {
3534
func formatCommandAction(cmd *cobra.Command, args []string) error {
3635
cmd.Println("Format the package")
3736

38-
packageRoot, found, err := packages.FindPackageRoot()
37+
packageRoot, err := packages.FindPackageRoot()
3938
if err != nil {
4039
return fmt.Errorf("locating package root failed: %w", err)
4140
}
42-
if !found {
43-
return errors.New("package root not found")
44-
}
4541

4642
ff, err := cmd.Flags().GetBool(cobraext.FailFastFlagName)
4743
if err != nil {

cmd/install.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"errors"
98
"fmt"
109

1110
"github.com/spf13/cobra"
@@ -72,12 +71,8 @@ func installCommandAction(cmd *cobra.Command, _ []string) error {
7271
}
7372

7473
if zipPathFile == "" && packageRootPath == "" {
75-
var found bool
7674
var err error
77-
packageRootPath, found, err = packages.FindPackageRoot()
78-
if !found {
79-
return errors.New("package root not found")
80-
}
75+
packageRootPath, err = packages.FindPackageRoot()
8176
if err != nil {
8277
return fmt.Errorf("locating package root failed: %w", err)
8378
}

cmd/lint.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"errors"
98
"fmt"
109

1110
"github.com/spf13/cobra"
@@ -74,10 +73,7 @@ func lintCommandAction(cmd *cobra.Command, args []string) error {
7473
}
7574

7675
func validateSourceCommandAction(cmd *cobra.Command, args []string) error {
77-
packageRootPath, found, err := packages.FindPackageRoot()
78-
if !found {
79-
return errors.New("package root not found")
80-
}
76+
packageRootPath, err := packages.FindPackageRoot()
8177
if err != nil {
8278
return fmt.Errorf("locating package root failed: %w", err)
8379
}

cmd/service.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"errors"
98
"fmt"
109
"path/filepath"
1110

@@ -47,13 +46,10 @@ func setupServiceCommand() *cobraext.Command {
4746
func upCommandAction(cmd *cobra.Command, args []string) error {
4847
cmd.Println("Boot up the service stack")
4948

50-
packageRoot, found, err := packages.FindPackageRoot()
49+
packageRoot, err := packages.FindPackageRoot()
5150
if err != nil {
5251
return fmt.Errorf("locating package root failed: %w", err)
5352
}
54-
if !found {
55-
return errors.New("package root not found")
56-
}
5753

5854
var dataStreamPath string
5955
dataStreamFlag, _ := cmd.Flags().GetString(cobraext.DataStreamFlagName)

cmd/status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ func getPackageStatus(packageName string, options registry.SearchOptions) (*stat
163163
if packageName != "" {
164164
return status.RemotePackage(packageName, options)
165165
}
166-
packageRootPath, found, err := packages.FindPackageRoot()
167-
if !found {
168-
return nil, errors.New("no package specified and package root not found")
169-
}
166+
packageRootPath, err := packages.FindPackageRoot()
170167
if err != nil {
168+
if errors.Is(err, packages.ErrPackageRootNotFound) {
169+
return nil, errors.New("no package specified and package root not found")
170+
}
171171
return nil, fmt.Errorf("locating package root failed: %w", err)
172172
}
173173
return status.LocalPackage(packageRootPath, options)

cmd/testrunner.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ func testRunnerAssetCommandAction(cmd *cobra.Command, args []string) error {
146146
return cobraext.FlagParsingError(fmt.Errorf("coverage format not available: %s", testCoverageFormat), cobraext.TestCoverageFormatFlagName)
147147
}
148148

149-
packageRootPath, found, err := packages.FindPackageRoot()
150-
if !found {
151-
return errors.New("package root not found")
152-
}
149+
packageRootPath, err := packages.FindPackageRoot()
153150
if err != nil {
154151
return fmt.Errorf("locating package root failed: %w", err)
155152
}
@@ -242,10 +239,7 @@ func testRunnerStaticCommandAction(cmd *cobra.Command, args []string) error {
242239
return cobraext.FlagParsingError(fmt.Errorf("coverage format not available: %s", testCoverageFormat), cobraext.TestCoverageFormatFlagName)
243240
}
244241

245-
packageRootPath, found, err := packages.FindPackageRoot()
246-
if !found {
247-
return errors.New("package root not found")
248-
}
242+
packageRootPath, err := packages.FindPackageRoot()
249243
if err != nil {
250244
return fmt.Errorf("locating package root failed: %w", err)
251245
}
@@ -349,10 +343,7 @@ func testRunnerPipelineCommandAction(cmd *cobra.Command, args []string) error {
349343
return cobraext.FlagParsingError(err, cobraext.DeferCleanupFlagName)
350344
}
351345

352-
packageRootPath, found, err := packages.FindPackageRoot()
353-
if !found {
354-
return errors.New("package root not found")
355-
}
346+
packageRootPath, err := packages.FindPackageRoot()
356347
if err != nil {
357348
return fmt.Errorf("locating package root failed: %w", err)
358349
}
@@ -493,10 +484,7 @@ func testRunnerSystemCommandAction(cmd *cobra.Command, args []string) error {
493484
return cobraext.FlagParsingError(err, cobraext.VariantFlagName)
494485
}
495486

496-
packageRootPath, found, err := packages.FindPackageRoot()
497-
if !found {
498-
return errors.New("package root not found")
499-
}
487+
packageRootPath, err := packages.FindPackageRoot()
500488
if err != nil {
501489
return fmt.Errorf("locating package root failed: %w", err)
502490
}
@@ -664,10 +652,7 @@ func testRunnerPolicyCommandAction(cmd *cobra.Command, args []string) error {
664652
return cobraext.FlagParsingError(fmt.Errorf("coverage format not available: %s", testCoverageFormat), cobraext.TestCoverageFormatFlagName)
665653
}
666654

667-
packageRootPath, found, err := packages.FindPackageRoot()
668-
if !found {
669-
return errors.New("package root not found")
670-
}
655+
packageRootPath, err := packages.FindPackageRoot()
671656
if err != nil {
672657
return fmt.Errorf("locating package root failed: %w", err)
673658
}

cmd/uninstall.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"errors"
98
"fmt"
109

1110
"github.com/spf13/cobra"
@@ -35,10 +34,7 @@ func setupUninstallCommand() *cobraext.Command {
3534
}
3635

3736
func uninstallCommandAction(cmd *cobra.Command, args []string) error {
38-
packageRootPath, found, err := packages.FindPackageRoot()
39-
if !found {
40-
return errors.New("package root not found")
41-
}
37+
packageRootPath, err := packages.FindPackageRoot()
4238
if err != nil {
4339
return fmt.Errorf("locating package root failed: %w", err)
4440
}

internal/fields/validate.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ func WithOTELValidation(otelValidation bool) ValidatorOption {
257257
}
258258

259259
type packageRootFinder interface {
260-
FindPackageRoot() (string, bool, error)
260+
FindPackageRoot() (string, error)
261261
}
262262

263263
type packageRoot struct{}
264264

265-
func (p packageRoot) FindPackageRoot() (string, bool, error) {
265+
func (p packageRoot) FindPackageRoot() (string, error) {
266266
return packages.FindPackageRoot()
267267
}
268268

@@ -288,13 +288,14 @@ func createValidatorForDirectoryAndPackageRoot(fieldsParentDir string, finder pa
288288

289289
var fdm *DependencyManager
290290
if !v.disabledDependencyManagement {
291-
packageRoot, found, err := finder.FindPackageRoot()
291+
packageRoot, err := finder.FindPackageRoot()
292292
if err != nil {
293+
if errors.Is(err, packages.ErrPackageRootNotFound) {
294+
return nil, errors.New("package root not found and dependency management is enabled")
295+
}
293296
return nil, fmt.Errorf("can't find package root: %w", err)
294297
}
295-
if !found {
296-
return nil, errors.New("package root not found and dependency management is enabled")
297-
}
298+
298299
fdm, v.Schema, err = initDependencyManagement(packageRoot, v.specVersion, v.enabledImportAllECSSchema)
299300
if err != nil {
300301
return nil, fmt.Errorf("failed to initialize dependency management: %w", err)

0 commit comments

Comments
 (0)