Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/robfig/cron v1.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
33 changes: 26 additions & 7 deletions xmlvalidate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Validates an XML file against an XSD schema using `xmlint`.
## Requirements

This activity requires the `xmllint` command be installed on the system where
the activity worker is running. This can be installed, in Ubuntu, by entering
the activity worker is running. This can be installed in Ubuntu by entering
the following command:

```bash
Expand Down Expand Up @@ -52,16 +52,35 @@ ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
RetryPolicy: &temporal.RetryPolicy{MaximumAttempts: 1},
})

var re xmlvalidate.Result
var result xmlvalidate.Result
err := workflow.ExecuteActivity(
ctx,
xmlvalidate.Name,
&xmlvalidate.Params{
XMLFilePath: "/path/to/file.xml",
XSDFilePath: "/path/to/file.xsd",
XMLPath: "/path/to/file.xml",
XSDPath: "/path/to/file.xsd",
},
).Get(ctx, &re)
).Get(ctx, &result)
```

`err` may contain any non validation error. `re.Failures` contains the
`xmllint` validation output as `[]bytes`.
`err` may contain any non validation error. `result.Failures` encodes the
`xmllint` validation stderr output as `[]string`.

## Command

XMLValidate includes a command-line entry point that can be executed with
`go run`, e.g.:

```bash
go run ./xmlvalidate/cmd --xsd xmvalidate/testdata/person.xsd \
xmlvalidate/testdata/person_invalid.xml
```

If validation is successful `xmlvalidate` will write "OK" to stdout and exit
with status code: 0.

If a validation error is encountered, `xmlvalidate` will write the validation
error to stdout, and will exit with status code: 0.

If a non-validation error is encountered, `xmlvalidate` will write the error
message to stderr, and exit with status code: 1.
95 changes: 95 additions & 0 deletions xmlvalidate/cmd/main.go
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
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

@djjuhasz djjuhasz Oct 21, 2024

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:

  1. It avoids confusing the order of the XML file path and XSD file path, e.g. xmlvalidate person.xsd people.xml vs. xmlvalidate people.xml person.xsd
  2. It leaves open the option of adding a --dtd option to validate XML against a DTD file instead of an XSD

I think the first reason justifies typing the extra --xsd .

--help Display this help and exit

`
}

// To test: `go run ./cmd --xsd testdata/person.xsd testdata/person_valid.xml`
func main() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recent PRs changed the xmlvalidate activity but didn't reflect those changes in the docs (parameter names, result type, how to use the XMLLintValidator). It would be nice to include a note somewhere in there about this command too, if we really want to include it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I'll update the docs. 😊