Skip to content

Commit

Permalink
[minor] Cleanup and simplify the service config YML (#1)
Browse files Browse the repository at this point in the history
* Just make an args config

* Rename AllowedFormat -> AllowedMimeTypes

* Rename mimetypes -> cmd-by-mimetype
  • Loading branch information
joecorall authored Apr 25, 2024
1 parent 413650c commit 6567072
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
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"

0 comments on commit 6567072

Please sign in to comment.