Skip to content
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

Cleanup and simplify the service config YML #1

Merged
merged 3 commits into from
Apr 25, 2024
Merged
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
42 changes: 20 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,18 @@ type Content struct {
}

type Cmd struct {
Command string `yaml:"cmd,omitempty"`
PreArgs []string `yaml:"pre-args,omitempty"`
PostArgs []string `yaml:"post-args,omitempty"`
Command string `yaml:"cmd,omitempty"`
Args []string `yaml:"args,omitempty"`
}

type Config struct {
Label string `yaml:"label"`
Method string `yaml:"destination-http-method"`
FileHeader string `yaml:"file-header"`
ArgHeader string `yaml:"arg-header"`
ForwardAuth bool `yaml:"forward-auth"`
AllowedFormats []string `yaml:"allowed-formats"`
Mimetypes map[string]Cmd `yaml:"mimetypes"`
Label string `yaml:"label"`
Method string `yaml:"destination-http-method"`
FileHeader string `yaml:"file-header"`
ArgHeader string `yaml:"arg-header"`
ForwardAuth bool `yaml:"forward-auth"`
AllowedMimeTypes []string `yaml:"allowed-mimetypes"`
CmdByMimeType map[string]Cmd `yaml:"cmd-by-mimetype"`
}

var (
Expand Down Expand Up @@ -218,27 +217,26 @@ func ReadConfig(yp string) (*Config, error) {
func buildExecCommand(mimetype, addtlArgs string, c *Config) (*exec.Cmd, error) {
var cmdConfig Cmd
var exists bool
slog.Info("Allowed formats", "formats", c.AllowedFormats)
if isAllowedMIMEType(mimetype, c.AllowedFormats) {
cmdConfig, exists = c.Mimetypes[mimetype]
slog.Info("Allowed formats", "formats", c.AllowedMimeTypes)
if isAllowedMIMEType(mimetype, c.AllowedMimeTypes) {
cmdConfig, exists = c.CmdByMimeType[mimetype]
if !exists || (len(cmdConfig.Command) == 0) {
// Fallback to default if specific MIME type not configured or if command is empty
cmdConfig = c.Mimetypes["default"]
cmdConfig = c.CmdByMimeType["default"]
}
} else {
return nil, fmt.Errorf("undefined mimetype: %s", mimetype)
}

args := []string{}
if len(cmdConfig.PreArgs) > 0 {
args = append(args, cmdConfig.PreArgs...)
}
if addtlArgs != "" {
args = append(args, addtlArgs)
}
if len(cmdConfig.PostArgs) > 0 {
args = append(args, cmdConfig.PostArgs...)
for _, a := range cmdConfig.Args {
if a == "%s" && addtlArgs != "" {
args = append(args, addtlArgs)
} else {
args = append(args, a)
}
}

cmd := exec.Command(cmdConfig.Command, args...)

return cmd, nil
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ destination-http-method: "PUT"
file-header: Apix-Ldp-Resource
arg-header: X-Islandora-Args
forward-auth: false
allowed-formats: [
allowed-mimetypes: [
"text/plain"
]
mimetypes:
cmd-by-mimetype:
default:
cmd: "cat"
`)
Expand Down Expand Up @@ -139,10 +139,10 @@ destination-http-method: "%s"
file-header: Apix-Ldp-Resource
arg-header: X-Islandora-Args
forward-auth: false
allowed-formats: [
allowed-mimetypes: [
"text/plain"
]
mimetypes:
cmd-by-mimetype:
default:
cmd: "cat"
`, method))
Expand Down
10 changes: 6 additions & 4 deletions scyllaridae.complex-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ destination-http-method: "PUT"
file-header: Apix-Ldp-Resource
arg-header: X-Islandora-Args
forward-auth: false
allowed-formats: [
allowed-mimetypes: [
"application/pdf",
"image/*"
]
mimetypes:
cmd-by-mimetype:
application/pdf:
cmd: "pdftotext"
post-args:
args:
- "%s"
- "-"
- "-"
default:
cmd: "tesseract"
pre-args:
args:
- "stdin"
- "stdout"
- "%s"
4 changes: 2 additions & 2 deletions scyllaridae.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ destination-http-method: "GET"
file-header: Apix-Ldp-Resource
arg-header: X-Islandora-Args
forward-auth: false
allowed-formats: [
allowed-mimetypes: [
"text/plain"
]
mimetypes:
cmd-by-mimetype:
default:
cmd: "cat"
Loading