Skip to content

Commit

Permalink
initial implementation of resolution strictness with configurable sta…
Browse files Browse the repository at this point in the history
…tus codes
  • Loading branch information
emilwareus committed Jan 5, 2024
1 parent 91cc7d2 commit 5a4003b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
9 changes: 8 additions & 1 deletion cmd/debricked/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"errors"
"os"

"github.com/debricked/cli/internal/cmd/cmderror"
"github.com/debricked/cli/internal/cmd/root"
"github.com/debricked/cli/internal/wire"
)
Expand All @@ -13,6 +15,11 @@ var version string // Set at compile time

func main() {
if err := root.NewRootCmd(version, wire.GetCliContainer()).Execute(); err != nil {
os.Exit(1)
var cmdErr cmderror.CommandError
if errors.As(err, &cmdErr) {
os.Exit(cmdErr.Code)
} else {
os.Exit(1)
}
}
}
10 changes: 10 additions & 0 deletions internal/cmd/cmderror/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmderror

type CommandError struct {
Code int
Err error
}

func (e CommandError) Error() string {
return e.Err.Error()
}
22 changes: 11 additions & 11 deletions internal/cmd/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

var (
exclusions = file.Exclusions()
verbose bool
npmPreferred bool
regenerate int
strictness int
exclusions = file.Exclusions()
verbose bool
npmPreferred bool
regenerate int
resolutionStrictness int
)

const (
Expand Down Expand Up @@ -81,7 +81,7 @@ $ debricked resolve . `+exampleFlags)

cmd.Flags().BoolP(NpmPreferredFlag, "", npmPreferred, npmPreferredDoc)

cmd.Flags().IntVarP(&strictness, ResolutionStrictFlag, "s", file.StrictAll, `Allows you to configure exit code 1 or 0 depending on if the resolution was successful or not.
cmd.Flags().IntVarP(&resolutionStrictness, ResolutionStrictFlag, "s", file.StrictAll, `Allows you to configure exit code 1 or 0 depending on if the resolution was successful or not.
Strictness Level | Meaning
---------------- | -------
0 (default) | Always exit with code 0, even if any or all files failed to resolve
Expand All @@ -102,13 +102,13 @@ func RunE(resolver resolution.IResolver) func(_ *cobra.Command, args []string) e
args = append(args, ".")
}
options := resolution.DebrickedOptions{
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Regenerate: viper.GetInt(RegenerateFlag),
NpmPreferred: viper.GetBool(NpmPreferredFlag),
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Regenerate: viper.GetInt(RegenerateFlag),
NpmPreferred: viper.GetBool(NpmPreferredFlag),
Resolutionstrictness: viper.GetInt(ResolutionStrictFlag),
}
_, err := resolver.Resolve(args, options)

return err
}
}
12 changes: 12 additions & 0 deletions internal/resolution/resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/debricked/cli/internal/resolution/job"
type IResolution interface {
Jobs() []job.IJob
HasErr() bool
GetJobErrorCount() int
}

type Resolution struct {
Expand All @@ -28,3 +29,14 @@ func (r Resolution) HasErr() bool {

return false
}

func (r Resolution) GetJobErrorCount() int {
count := 0
for _, j := range r.Jobs() {
if j.Errors().HasError() {
count++
}
}

return count
}
54 changes: 48 additions & 6 deletions internal/resolution/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package resolution

import (
"errors"
"fmt"
"os"
"path"
"regexp"

"github.com/debricked/cli/internal/cmd/cmderror"
"github.com/debricked/cli/internal/file"
resolutionFile "github.com/debricked/cli/internal/resolution/file"
"github.com/debricked/cli/internal/resolution/job"
Expand All @@ -32,11 +34,12 @@ type Resolver struct {
type IOptions interface{}

type DebrickedOptions struct {
Path string
Exclusions []string
Verbose bool
Regenerate int
NpmPreferred bool
Path string
Exclusions []string
Verbose bool
Regenerate int
NpmPreferred bool
Resolutionstrictness int
}

func NewResolver(
Expand All @@ -58,6 +61,35 @@ func (r Resolver) setNpmPreferred(npmPreferred bool) {
r.batchFactory.SetNpmPreferred(npmPreferred)
}

func (r Resolver) GetExitCode(resolution IResolution, options DebrickedOptions) int {
errorCount := resolution.GetJobErrorCount()
jobCount := len(resolution.Jobs())

switch options.Resolutionstrictness {
case 0:
return 0
case 1:
if errorCount == jobCount {
return 1
}
return 0
case 2:
if errorCount > 0 {
return 1
}
return 0
case 3:
if errorCount == 0 {
return 0
} else if errorCount == jobCount {
return 1
}
return 3
default:
panic(fmt.Sprintf("Invalid strictness level: %d", options.Resolutionstrictness))
}
}

func (r Resolver) Resolve(paths []string, options IOptions) (IResolution, error) {
dOptions, ok := options.(DebrickedOptions)
if !ok {
Expand Down Expand Up @@ -86,7 +118,17 @@ func (r Resolver) Resolve(paths []string, options IOptions) (IResolution, error)

if resolution.HasErr() {
jobErrList := tui.NewJobsErrorList(os.Stdout, resolution.Jobs())
err = jobErrList.Render(dOptions.Verbose)
renderErr := jobErrList.Render(dOptions.Verbose)
if renderErr != nil {
return resolution, renderErr
}
code := r.GetExitCode(resolution, dOptions)
if code != 0 {
err = cmderror.CommandError{
Code: code,
Err: fmt.Errorf("resolution failed"),
}
}
}

return resolution, err
Expand Down

0 comments on commit 5a4003b

Please sign in to comment.