Skip to content

Commit

Permalink
fix: halt on spec errors (#114)
Browse files Browse the repository at this point in the history
invalid specs no longer cause panics
  • Loading branch information
unRob authored Sep 25, 2023
1 parent 0c0eb35 commit e12052a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
2 changes: 2 additions & 0 deletions compa.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ See [﹅milpa help docs milpa﹅](/.milpa/docs/milpa/index.md) for more informat
err = lookup.AllSubCommands(!isDoctor)
if err != nil && !isDoctor {
logger.Fatalf("Could not find subcommands: %s", err)
} else if err != nil {
logger.Error(err)
}

if err := chinampa.Execute(cfg); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/unrob/milpa
go 1.20

require (
git.rob.mx/nidito/chinampa v0.1.2
git.rob.mx/nidito/chinampa v0.1.3
github.com/alecthomas/chroma/v2 v2.9.1
github.com/alessio/shellescape v1.4.2
github.com/bmatcuk/doublestar/v4 v4.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
git.rob.mx/nidito/chinampa v0.1.2 h1:jqQaeZWQKdwhuyqA90ODg1YQtPVV/oSlo6DvNUnJ7JM=
git.rob.mx/nidito/chinampa v0.1.2/go.mod h1:isI138ZQ3GN/ZcuRrNPJ48fYnPC+U642gUe5bonhwUo=
git.rob.mx/nidito/chinampa v0.1.3 h1:ZzOZLHZo5g0xKpWW0PM6MrX1dnvzYEQB2V7JZgwTstc=
git.rob.mx/nidito/chinampa v0.1.3/go.mod h1:isI138ZQ3GN/ZcuRrNPJ48fYnPC+U642gUe5bonhwUo=
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
Expand Down
26 changes: 24 additions & 2 deletions internal/actions/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"git.rob.mx/nidito/chinampa/pkg/tree"
"github.com/fatih/color"
"github.com/unrob/milpa/internal/bootstrap"
mcmd "github.com/unrob/milpa/internal/command"
_c "github.com/unrob/milpa/internal/constants"
)

Expand Down Expand Up @@ -72,10 +73,27 @@ var Doctor = &command.Command{
continue
}
docLog.Debugf("Validating %s", cmd.FullName())
report := cmd.Validate()

message := ""

hasFailures := false
report := map[string]int{}
if meta, ok := cmd.Meta.(mcmd.Meta); ok {
// fmt.Println("hasmeta")
parsingErrors := meta.ParsingErrors()
if len(parsingErrors) > 0 {
hasFailures = true

for _, err := range parsingErrors {
failures[cmd.FullName()]++
message += fail.Sprintf(" - %s\n", err)
}
} else {
report = cmd.Validate()
}
} else {
report = cmd.Validate()
}
for property, status := range report {
formatter := success
if status == 1 {
Expand Down Expand Up @@ -106,7 +124,11 @@ var Doctor = &command.Command{
if failedOverall {
failureReport := []string{}
for cmd, count := range failures {
failureReport = append(failureReport, fmt.Sprintf("%s - %d issues", cmd, count))
plural := ""
if count > 1 {
plural = "s"
}
failureReport = append(failureReport, fmt.Sprintf("%s - %d issue%s", cmd, count, plural))
}

return fmt.Errorf("your milpa could use some help with the following commands:\n%s", strings.Join(failureReport, "\n"))
Expand Down
14 changes: 14 additions & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func New(path string, repo string) (cmd *command.Command, err error) {
Config: spec,
}
meta.issues = append(meta.issues, err)
cmd.Meta = meta
cmd.HelpFunc = func(printLinks bool) string {
return `---
# ⚠️ Could not validate spec ⚠️
Looks like the spec for this command has errors that prevented parsing:
**` + fmt.Sprint(err) + `**
Run ﹅milpa itself doctor﹅ to diagnose your installed commands.
---`
}
return cmd, err
}

cmd.Meta = meta
Expand Down
4 changes: 4 additions & 0 deletions internal/command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ func metaForPath(path string, repo string) (meta Meta) {

return
}

func (meta *Meta) ParsingErrors() []error {
return meta.issues
}
13 changes: 8 additions & 5 deletions internal/lookup/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"git.rob.mx/nidito/chinampa"
ccmd "git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/chinampa/pkg/logger"
doublestar "github.com/bmatcuk/doublestar/v4"
"github.com/unrob/milpa/internal/bootstrap"
Expand Down Expand Up @@ -80,14 +81,16 @@ func AllSubCommands(returnOnError bool) error {
for _, path := range keys {
repo := files[path]
cmd, specErr := command.New(path, repo)
if specErr != nil {
if specErr == nil {
log.Debugf("Initialized %s", cmd.FullName())
chinampa.Register(cmd)
} else {
if returnOnError {
return specErr
cmd.Arguments = ccmd.Arguments{}
cmd.Options = ccmd.Options{}
}
chinampa.Register(cmd)
}

log.Debugf("Initialized %s", cmd.FullName())
chinampa.Register(cmd)
}

return err
Expand Down

0 comments on commit e12052a

Please sign in to comment.