From 656707237c464d8e8434ccbb8b573a17ed2527f6 Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Thu, 25 Apr 2024 12:32:31 -0400 Subject: [PATCH] [minor] Cleanup and simplify the service config YML (#1) * Just make an args config * Rename AllowedFormat -> AllowedMimeTypes * Rename mimetypes -> cmd-by-mimetype --- main.go | 42 ++++++++++++++++----------------- main_test.go | 8 +++---- scyllaridae.complex-example.yml | 10 ++++---- scyllaridae.example.yml | 4 ++-- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 017b5ed..e21f49d 100644 --- a/main.go +++ b/main.go @@ -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 ( @@ -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 diff --git a/main_test.go b/main_test.go index ecba57a..f1dca98 100644 --- a/main_test.go +++ b/main_test.go @@ -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" `) @@ -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)) diff --git a/scyllaridae.complex-example.yml b/scyllaridae.complex-example.yml index feb1884..fbe0e76 100644 --- a/scyllaridae.complex-example.yml +++ b/scyllaridae.complex-example.yml @@ -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" diff --git a/scyllaridae.example.yml b/scyllaridae.example.yml index 9103b56..34e13a7 100644 --- a/scyllaridae.example.yml +++ b/scyllaridae.example.yml @@ -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"