-
Notifications
You must be signed in to change notification settings - Fork 0
Add an xmlvalidate command #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
|
|
||
| "github.com/spf13/pflag" | ||
|
|
||
| "github.com/artefactual-sdps/temporal-activities/xmlvalidate" | ||
| ) | ||
|
|
||
| func seeHelp() string { | ||
| return "See 'xmlvalidate --help' for usage." | ||
| } | ||
|
|
||
| func usage() string { | ||
| return ` | ||
| USAGE: xmlvalidate [options] XMLFILE | ||
|
|
||
| XMLFILE Path of the XML file to be validated | ||
|
|
||
| OPTIONS: | ||
| --xsd PATH Path of an XSD file to validate against | ||
| --help Display this help and exit | ||
|
|
||
| ` | ||
| } | ||
|
|
||
| // To test: `go run ./cmd --xsd testdata/person.xsd testdata/person_valid.xml` | ||
| func main() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should try to follow https://artefactual.dev/docs/programming-languages/go/main/ and add some tests for this command.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, okay. 👍 |
||
| // Setup signal handlers. | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| c := make(chan os.Signal, 1) | ||
| signal.Notify(c, os.Interrupt) | ||
| go func() { <-c; cancel() }() | ||
|
|
||
| // Execute program. | ||
| out, err := Run(ctx, os.Args) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| fmt.Fprintln(os.Stdout, out) | ||
| } | ||
|
|
||
| func Run(ctx context.Context, args []string) (string, error) { | ||
| var ( | ||
| help bool | ||
| xsd string | ||
| ) | ||
|
|
||
| p := pflag.NewFlagSet(args[0], pflag.ExitOnError) | ||
| p.BoolVarP(&help, "help", "h", false, "display help and exit") | ||
| p.StringVar(&xsd, "xsd", "", "XSD file `path`") | ||
|
|
||
| var out string | ||
| p.Usage = func() { | ||
| out = usage() | ||
| } | ||
|
|
||
| if err := p.Parse(args[1:]); err != nil { | ||
| if err == pflag.ErrHelp { | ||
| return out, nil | ||
| } | ||
| return "", err | ||
| } | ||
| args = p.Args() | ||
|
|
||
| if help { | ||
| return usage(), nil | ||
| } | ||
|
|
||
| if len(args) == 0 { | ||
| return "", fmt.Errorf("missing XMLFILE\n%s", seeHelp()) | ||
| } | ||
|
|
||
| if xsd == "" { | ||
| return "", fmt.Errorf("an --xsd file must be specified\n%s", seeHelp()) | ||
| } | ||
|
|
||
| v := xmlvalidate.NewXMLLintValidator() | ||
| out, err := v.Validate(ctx, args[0], xsd) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if out == "" { | ||
| out = "OK" | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recent PRs changed the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops! I'll update the docs. 😊 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's required, I'd make it an argument too.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it an option for two reasons:
xmlvalidate person.xsd people.xmlvs.xmlvalidate people.xml person.xsd--dtdoption to validate XML against a DTD file instead of an XSDI think the first reason justifies typing the extra
--xsd.