From 967af7e3a9b513bb85e62684b83097d6e33aea91 Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Thu, 25 Apr 2024 12:20:03 -0400 Subject: [PATCH 1/3] Just make an args config --- main.go | 20 +++++++++----------- scyllaridae.complex-example.yml | 6 ++++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 017b5ed..177f1b1 100644 --- a/main.go +++ b/main.go @@ -55,9 +55,8 @@ 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 { @@ -230,15 +229,14 @@ func buildExecCommand(mimetype, addtlArgs string, c *Config) (*exec.Cmd, error) } 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/scyllaridae.complex-example.yml b/scyllaridae.complex-example.yml index feb1884..6273178 100644 --- a/scyllaridae.complex-example.yml +++ b/scyllaridae.complex-example.yml @@ -10,11 +10,13 @@ allowed-formats: [ mimetypes: application/pdf: cmd: "pdftotext" - post-args: + args: + - "%s" - "-" - "-" default: cmd: "tesseract" - pre-args: + args: - "stdin" - "stdout" + - "%s" From 1d7e436782db156473728fcdaf885f004b0bbbed Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Thu, 25 Apr 2024 12:22:37 -0400 Subject: [PATCH 2/3] Rename AllowedFormat -> AllowedMimeTypes --- main.go | 18 +++++++++--------- main_test.go | 4 ++-- scyllaridae.complex-example.yml | 2 +- scyllaridae.example.yml | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 177f1b1..69a655e 100644 --- a/main.go +++ b/main.go @@ -60,13 +60,13 @@ type Cmd struct { } 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"` + Mimetypes map[string]Cmd `yaml:"mimetypes"` } var ( @@ -217,8 +217,8 @@ 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) { + slog.Info("Allowed formats", "formats", c.AllowedMimeTypes) + if isAllowedMIMEType(mimetype, c.AllowedMimeTypes) { cmdConfig, exists = c.Mimetypes[mimetype] if !exists || (len(cmdConfig.Command) == 0) { // Fallback to default if specific MIME type not configured or if command is empty diff --git a/main_test.go b/main_test.go index ecba57a..4898f6e 100644 --- a/main_test.go +++ b/main_test.go @@ -47,7 +47,7 @@ destination-http-method: "PUT" file-header: Apix-Ldp-Resource arg-header: X-Islandora-Args forward-auth: false -allowed-formats: [ +allowed-mimetypes: [ "text/plain" ] mimetypes: @@ -139,7 +139,7 @@ destination-http-method: "%s" file-header: Apix-Ldp-Resource arg-header: X-Islandora-Args forward-auth: false -allowed-formats: [ +allowed-mimetypes: [ "text/plain" ] mimetypes: diff --git a/scyllaridae.complex-example.yml b/scyllaridae.complex-example.yml index 6273178..206059f 100644 --- a/scyllaridae.complex-example.yml +++ b/scyllaridae.complex-example.yml @@ -3,7 +3,7 @@ destination-http-method: "PUT" file-header: Apix-Ldp-Resource arg-header: X-Islandora-Args forward-auth: false -allowed-formats: [ +allowed-mimetypes: [ "application/pdf", "image/*" ] diff --git a/scyllaridae.example.yml b/scyllaridae.example.yml index 9103b56..006e351 100644 --- a/scyllaridae.example.yml +++ b/scyllaridae.example.yml @@ -9,7 +9,7 @@ destination-http-method: "GET" file-header: Apix-Ldp-Resource arg-header: X-Islandora-Args forward-auth: false -allowed-formats: [ +allowed-mimetypes: [ "text/plain" ] mimetypes: From a2d6b37d20140399bc4590adfa86f13b080015da Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Thu, 25 Apr 2024 12:25:07 -0400 Subject: [PATCH 3/3] Rename mimetypes -> cmd-by-mimetype --- main.go | 6 +++--- main_test.go | 4 ++-- scyllaridae.complex-example.yml | 2 +- scyllaridae.example.yml | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 69a655e..e21f49d 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ type Config struct { ArgHeader string `yaml:"arg-header"` ForwardAuth bool `yaml:"forward-auth"` AllowedMimeTypes []string `yaml:"allowed-mimetypes"` - Mimetypes map[string]Cmd `yaml:"mimetypes"` + CmdByMimeType map[string]Cmd `yaml:"cmd-by-mimetype"` } var ( @@ -219,10 +219,10 @@ func buildExecCommand(mimetype, addtlArgs string, c *Config) (*exec.Cmd, error) var exists bool slog.Info("Allowed formats", "formats", c.AllowedMimeTypes) if isAllowedMIMEType(mimetype, c.AllowedMimeTypes) { - cmdConfig, exists = c.Mimetypes[mimetype] + 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) diff --git a/main_test.go b/main_test.go index 4898f6e..f1dca98 100644 --- a/main_test.go +++ b/main_test.go @@ -50,7 +50,7 @@ forward-auth: false allowed-mimetypes: [ "text/plain" ] -mimetypes: +cmd-by-mimetype: default: cmd: "cat" `) @@ -142,7 +142,7 @@ forward-auth: false 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 206059f..fbe0e76 100644 --- a/scyllaridae.complex-example.yml +++ b/scyllaridae.complex-example.yml @@ -7,7 +7,7 @@ allowed-mimetypes: [ "application/pdf", "image/*" ] -mimetypes: +cmd-by-mimetype: application/pdf: cmd: "pdftotext" args: diff --git a/scyllaridae.example.yml b/scyllaridae.example.yml index 006e351..34e13a7 100644 --- a/scyllaridae.example.yml +++ b/scyllaridae.example.yml @@ -12,6 +12,6 @@ forward-auth: false allowed-mimetypes: [ "text/plain" ] -mimetypes: +cmd-by-mimetype: default: cmd: "cat"