-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --help, --version, and improved errors
- Loading branch information
Showing
17 changed files
with
562 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
package pipesore | ||
|
||
type ast struct { | ||
functions []function | ||
filters []filter | ||
} | ||
|
||
func newAST() *ast { | ||
return &ast{ | ||
functions: []function{}, | ||
filters: []filter{}, | ||
} | ||
} | ||
|
||
type function struct { | ||
type filter struct { | ||
name string | ||
arguments []any | ||
position | ||
} | ||
|
||
func (f function) isNot() bool { | ||
func (f filter) isNot() bool { | ||
return f.name[0:1] == "!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pipesore | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/dyson/pipesore/pkg/pipeline" | ||
) | ||
|
||
func Run(version, commit, date string) (int, error) { | ||
seeHelp := fmt.Sprintf("See '%s --help'", filepath.Base(os.Args[0])) | ||
|
||
if len(os.Args) != 2 { | ||
return 1, fmt.Errorf("error: define a single pipeline or option.\n%s.", seeHelp) | ||
} | ||
|
||
input := os.Args[1] | ||
|
||
switch input { | ||
case "-h", "--help": | ||
printHelp() | ||
return 0, nil | ||
case "-v", "--version": | ||
fmt.Printf("pipesore version %s, commit %s, date %s\n", version, commit, date) | ||
return 0, nil | ||
case "": | ||
return 1, fmt.Errorf("error: no pipeline defined.\n%s.", seeHelp) | ||
} | ||
|
||
err := execute(input, os.Stdin, os.Stdout) | ||
if err != nil { | ||
var syntaxError *syntaxError | ||
if errors.As(err, &syntaxError) { | ||
return 1, newFormattedError(err, input, syntaxError.position, seeHelp) | ||
} | ||
|
||
var filterNameError *filterNameError | ||
if errors.As(err, &filterNameError) { | ||
if filterNameError.suggestion != "" { | ||
definition := pipeline.Filters[filterNameError.suggestion].Definition | ||
seeHelp = fmt.Sprintf("Did you mean '%s'?\n%s", definition, seeHelp) | ||
} | ||
|
||
return 1, newFormattedError(err, input, filterNameError.position, seeHelp) | ||
} | ||
|
||
var filterArgumentError *filterArgumentError | ||
if errors.As(err, &filterArgumentError) { | ||
help := fmt.Sprintf("%s. %s", pipeline.Filters[filterArgumentError.name].Definition, seeHelp) | ||
|
||
return 1, newFormattedError(err, input, filterArgumentError.position, help) | ||
} | ||
|
||
return 1, fmt.Errorf("%w.\n%s.", err, seeHelp) | ||
} | ||
|
||
return 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package pipesore | ||
|
||
import "fmt" | ||
|
||
type syntaxError struct { | ||
err error | ||
position | ||
} | ||
|
||
func newSyntaxError(err error, position position) *syntaxError { | ||
return &syntaxError{ | ||
err: err, | ||
position: position, | ||
} | ||
} | ||
|
||
func (pe *syntaxError) Error() string { | ||
return pe.err.Error() | ||
} | ||
|
||
type filterNameError struct { | ||
err error | ||
position | ||
name string | ||
suggestion string | ||
} | ||
|
||
func newFilterNameError(err error, position position, name, suggestion string) *filterNameError { | ||
return &filterNameError{ | ||
err: err, | ||
position: position, | ||
name: name, | ||
suggestion: suggestion, | ||
} | ||
} | ||
|
||
func (fne *filterNameError) Error() string { | ||
return fne.err.Error() | ||
} | ||
|
||
type filterArgumentError struct { | ||
err error | ||
name string | ||
position | ||
} | ||
|
||
func newFilterArgumentError(err error, position position, name string) *filterArgumentError { | ||
return &filterArgumentError{ | ||
err: err, | ||
position: position, | ||
name: name, | ||
} | ||
} | ||
|
||
func (fne *filterArgumentError) Error() string { | ||
return fne.err.Error() | ||
} | ||
|
||
func newFormattedError(err error, input string, position position, help string) error { | ||
red := "\x1b[31m" | ||
undercurl := "\x1b[4:3m" | ||
reset := "\x1b[0m" | ||
|
||
var inputBefore, inputAfter string | ||
|
||
start := position.start | ||
end := position.end | ||
|
||
// handle EOF | ||
if len(input) == start { | ||
input += " " | ||
} | ||
|
||
if start > 0 { | ||
inputBefore = input[:start] | ||
} | ||
|
||
inputError := input[start:end] | ||
|
||
if len(input) > end { | ||
inputAfter = input[end:] | ||
} | ||
|
||
if help != "" { | ||
help = "\n" + help | ||
} | ||
|
||
return fmt.Errorf( | ||
"%w:\n\t%s%s%s%s%s%s%s.", | ||
err, | ||
inputBefore, | ||
red, | ||
undercurl, | ||
inputError, | ||
reset, | ||
inputAfter, | ||
help, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.