diff --git a/build/app/models/post_params_body.go b/build/app/models/post_params_body.go index c62d5d3..8b5ab29 100644 --- a/build/app/models/post_params_body.go +++ b/build/app/models/post_params_body.go @@ -9,7 +9,6 @@ import ( "context" "strconv" - "github.com/direktiv/apps/go/pkg/apps" "github.com/go-openapi/errors" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -21,34 +20,23 @@ import ( // swagger:model postParamsBody type PostParamsBody struct { - // File to create before running commands. - Files []apps.DirektivFile `json:"files"` - - // Name of the json input file to utilise - Infile string `json:"infile,omitempty"` - // the filename of the output spreadsheet // Required: true OutFilename *string `json:"out-filename"` - // Label for the worksheet created in the spreadsheet - // Required: true - WorksheetName *string `json:"worksheet-name"` + // Array of worksheets to create in spreadsheet + Sheets []*PostParamsBodySheetsItems `json:"sheets"` } // Validate validates this post params body func (m *PostParamsBody) Validate(formats strfmt.Registry) error { var res []error - if err := m.validateFiles(formats); err != nil { - res = append(res, err) - } - if err := m.validateOutFilename(formats); err != nil { res = append(res, err) } - if err := m.validateWorksheetName(formats); err != nil { + if err := m.validateSheets(formats); err != nil { res = append(res, err) } @@ -58,27 +46,6 @@ func (m *PostParamsBody) Validate(formats strfmt.Registry) error { return nil } -func (m *PostParamsBody) validateFiles(formats strfmt.Registry) error { - if swag.IsZero(m.Files) { // not required - return nil - } - - for i := 0; i < len(m.Files); i++ { - - if err := m.Files[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("files" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("files" + "." + strconv.Itoa(i)) - } - return err - } - - } - - return nil -} - func (m *PostParamsBody) validateOutFilename(formats strfmt.Registry) error { if err := validate.Required("out-filename", "body", m.OutFilename); err != nil { @@ -88,10 +55,27 @@ func (m *PostParamsBody) validateOutFilename(formats strfmt.Registry) error { return nil } -func (m *PostParamsBody) validateWorksheetName(formats strfmt.Registry) error { +func (m *PostParamsBody) validateSheets(formats strfmt.Registry) error { + if swag.IsZero(m.Sheets) { // not required + return nil + } + + for i := 0; i < len(m.Sheets); i++ { + if swag.IsZero(m.Sheets[i]) { // not required + continue + } + + if m.Sheets[i] != nil { + if err := m.Sheets[i].Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("sheets" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("sheets" + "." + strconv.Itoa(i)) + } + return err + } + } - if err := validate.Required("worksheet-name", "body", m.WorksheetName); err != nil { - return err } return nil @@ -101,7 +85,7 @@ func (m *PostParamsBody) validateWorksheetName(formats strfmt.Registry) error { func (m *PostParamsBody) ContextValidate(ctx context.Context, formats strfmt.Registry) error { var res []error - if err := m.contextValidateFiles(ctx, formats); err != nil { + if err := m.contextValidateSheets(ctx, formats); err != nil { res = append(res, err) } @@ -111,17 +95,19 @@ func (m *PostParamsBody) ContextValidate(ctx context.Context, formats strfmt.Reg return nil } -func (m *PostParamsBody) contextValidateFiles(ctx context.Context, formats strfmt.Registry) error { +func (m *PostParamsBody) contextValidateSheets(ctx context.Context, formats strfmt.Registry) error { - for i := 0; i < len(m.Files); i++ { + for i := 0; i < len(m.Sheets); i++ { - if err := m.Files[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("files" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("files" + "." + strconv.Itoa(i)) + if m.Sheets[i] != nil { + if err := m.Sheets[i].ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("sheets" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("sheets" + "." + strconv.Itoa(i)) + } + return err } - return err } } diff --git a/build/app/models/post_params_body_sheets_items.go b/build/app/models/post_params_body_sheets_items.go new file mode 100644 index 0000000..301cf8f --- /dev/null +++ b/build/app/models/post_params_body_sheets_items.go @@ -0,0 +1,116 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "strconv" + + "github.com/direktiv/apps/go/pkg/apps" + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" +) + +// PostParamsBodySheetsItems post params body sheets items +// +// swagger:model postParamsBodySheetsItems +type PostParamsBodySheetsItems struct { + + // Name of the json input file to utilise + Infile string `json:"infile,omitempty"` + + // File to create before running commands + InputFile []apps.DirektivFile `json:"input-file"` + + // Label for the worksheet created in the spreadsheet + WorksheetName *string `json:"worksheet-name,omitempty"` +} + +// Validate validates this post params body sheets items +func (m *PostParamsBodySheetsItems) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateInputFile(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *PostParamsBodySheetsItems) validateInputFile(formats strfmt.Registry) error { + if swag.IsZero(m.InputFile) { // not required + return nil + } + + for i := 0; i < len(m.InputFile); i++ { + + if err := m.InputFile[i].Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("input-file" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("input-file" + "." + strconv.Itoa(i)) + } + return err + } + + } + + return nil +} + +// ContextValidate validate this post params body sheets items based on the context it is used +func (m *PostParamsBodySheetsItems) ContextValidate(ctx context.Context, formats strfmt.Registry) error { + var res []error + + if err := m.contextValidateInputFile(ctx, formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *PostParamsBodySheetsItems) contextValidateInputFile(ctx context.Context, formats strfmt.Registry) error { + + for i := 0; i < len(m.InputFile); i++ { + + if err := m.InputFile[i].ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("input-file" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("input-file" + "." + strconv.Itoa(i)) + } + return err + } + + } + + return nil +} + +// MarshalBinary interface implementation +func (m *PostParamsBodySheetsItems) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *PostParamsBodySheetsItems) UnmarshalBinary(b []byte) error { + var res PostParamsBodySheetsItems + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/build/app/restapi/embedded_spec.go b/build/app/restapi/embedded_spec.go index 318b6a6..86ce76c 100644 --- a/build/app/restapi/embedded_spec.go +++ b/build/app/restapi/embedded_spec.go @@ -68,31 +68,39 @@ func init() { "schema": { "type": "object", "required": [ - "worksheet-name", "out-filename" ], "properties": { - "files": { - "description": "File to create before running commands.", - "type": "array", - "default": null, - "items": { - "$ref": "#/definitions/direktivFile" - } - }, - "infile": { - "description": "Name of the json input file to utilise", - "type": "string" - }, "out-filename": { "description": "the filename of the output spreadsheet", "type": "string", "default": "output.xlsx" }, - "worksheet-name": { - "description": "Label for the worksheet created in the spreadsheet", - "type": "string", - "default": "Sheet1" + "sheets": { + "description": "Array of worksheets to create in spreadsheet", + "type": "array", + "items": { + "type": "object", + "properties": { + "infile": { + "description": "Name of the json input file to utilise", + "type": "string" + }, + "input-file": { + "description": "File to create before running commands", + "type": "array", + "default": null, + "items": { + "$ref": "#/definitions/direktivFile" + } + }, + "worksheet-name": { + "description": "Label for the worksheet created in the spreadsheet", + "type": "string", + "default": "Sheet1" + } + } + } } } } @@ -132,8 +140,9 @@ func init() { "x-direktiv": { "cmds": [ { - "action": "exec", - "exec": "/bin/json2excel -inputJson {{- if eq (deref .Infile) \"\" }} \"input.json\" {{- else }} {{ .Infile }} {{- end }} -worksheetName \"{{ .WorksheetName }}\" -outputFilename '{{ .OutFilename }}'", + "action": "foreach", + "exec": "/bin/json2excel -inputJson {{- if eq (deref .Item.Infile) \"\" }} \"input.json\" {{- else }} {{ .Item.Infile }} {{- end }} -worksheetName \"{{ .Item.WorksheetName }}\" -outputFilename '{{ .OutFilename }}'", + "loop": ".Sheets", "print": true, "silent": false }, @@ -151,11 +160,11 @@ func init() { }, "x-direktiv-examples": [ { - "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n files:\n - name: input.json\n data: |\n jq(.input) \n worksheet-name: \"TestSpreadsheet\"\n out-filename: example.xlsx", + "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n out-filename: example.xlsx\n sheets:\n - sheet:\n worksheet-name: \"TestSpreadsheet\"\n input-file:\n - name: input.json\n data: |\n jq(.input)", "title": "Basic" }, { - "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n files:\n - name: input.json\n data: |\n jq(.array | {input: . } | tostring ) \n worksheet-name: \"TestSpreadsheet\"\n out-filename: example.xlsx ", + "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n out-filename: example.xlsx \n sheets:\n - sheet:\n worksheet-name: \"TestSpreadsheet\"\n input-file:\n - name: input.json\n data: |\n jq(.array | {input: . } | tostring ) ", "title": "Use jq to add a parent key 'input' containing array of objects" } ], @@ -295,8 +304,9 @@ func init() { "x-direktiv": { "cmds": [ { - "action": "exec", - "exec": "/bin/json2excel -inputJson {{- if eq (deref .Infile) \"\" }} \"input.json\" {{- else }} {{ .Infile }} {{- end }} -worksheetName \"{{ .WorksheetName }}\" -outputFilename '{{ .OutFilename }}'", + "action": "foreach", + "exec": "/bin/json2excel -inputJson {{- if eq (deref .Item.Infile) \"\" }} \"input.json\" {{- else }} {{ .Item.Infile }} {{- end }} -worksheetName \"{{ .Item.WorksheetName }}\" -outputFilename '{{ .OutFilename }}'", + "loop": ".Sheets", "print": true, "silent": false }, @@ -314,11 +324,11 @@ func init() { }, "x-direktiv-examples": [ { - "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n files:\n - name: input.json\n data: |\n jq(.input) \n worksheet-name: \"TestSpreadsheet\"\n out-filename: example.xlsx", + "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n out-filename: example.xlsx\n sheets:\n - sheet:\n worksheet-name: \"TestSpreadsheet\"\n input-file:\n - name: input.json\n data: |\n jq(.input)", "title": "Basic" }, { - "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n files:\n - name: input.json\n data: |\n jq(.array | {input: . } | tostring ) \n worksheet-name: \"TestSpreadsheet\"\n out-filename: example.xlsx ", + "content": "- id: export-xlsx\n type: action\n action:\n function: export-xlsx\n input: \n out-filename: example.xlsx \n sheets:\n - sheet:\n worksheet-name: \"TestSpreadsheet\"\n input-file:\n - name: input.json\n data: |\n jq(.array | {input: . } | tostring ) ", "title": "Use jq to add a parent key 'input' containing array of objects" } ], @@ -388,27 +398,39 @@ func init() { "postParamsBody": { "type": "object", "required": [ - "worksheet-name", "out-filename" ], "properties": { - "files": { - "description": "File to create before running commands.", + "out-filename": { + "description": "the filename of the output spreadsheet", + "type": "string", + "default": "output.xlsx" + }, + "sheets": { + "description": "Array of worksheets to create in spreadsheet", + "type": "array", + "items": { + "$ref": "#/definitions/postParamsBodySheetsItems" + } + } + }, + "x-go-gen-location": "operations" + }, + "postParamsBodySheetsItems": { + "type": "object", + "properties": { + "infile": { + "description": "Name of the json input file to utilise", + "type": "string" + }, + "input-file": { + "description": "File to create before running commands", "type": "array", "default": [], "items": { "$ref": "#/definitions/direktivFile" } }, - "infile": { - "description": "Name of the json input file to utilise", - "type": "string" - }, - "out-filename": { - "description": "the filename of the output spreadsheet", - "type": "string", - "default": "output.xlsx" - }, "worksheet-name": { "description": "Label for the worksheet created in the spreadsheet", "type": "string", diff --git a/build/app/restapi/operations/direktiv_post.go b/build/app/restapi/operations/direktiv_post.go index 1d759b3..89422d8 100644 --- a/build/app/restapi/operations/direktiv_post.go +++ b/build/app/restapi/operations/direktiv_post.go @@ -86,9 +86,7 @@ func PostDirektivHandle(params PostParams) middleware.Responder { // if foreach returns an error there is no continue // - // default we do not continue - cont = convertTemplateToBool("", accParams, false) - // cont = convertTemplateToBool("", accParams, true) + // cont = false // if err != nil && !cont { @@ -163,34 +161,65 @@ func PostDirektivHandle(params PostParams) middleware.Responder { return NewPostOK().WithPayload(resp) } -// exec +// foreach command +type LoopStruct0 struct { + accParams + Item interface{} + DirektivDir string +} + func runCommand0(ctx context.Context, - params accParams, ri *apps.RequestInfo) (map[string]interface{}, error) { + params accParams, ri *apps.RequestInfo) ([]map[string]interface{}, error) { - ir := make(map[string]interface{}) - ir[successKey] = false + var cmds []map[string]interface{} - at := accParamsTemplate{ - *params.Body, - params.Commands, - params.DirektivDir, + if params.Body == nil { + return cmds, nil } - cmd, err := templateString(`/bin/json2excel -inputJson {{- if eq (deref .Infile) "" }} "input.json" {{- else }} {{ .Infile }} {{- end }} -worksheetName "{{ .WorksheetName }}" -outputFilename '{{ .OutFilename }}'`, at) - if err != nil { - ri.Logger().Infof("error executing command: %v", err) - ir[resultKey] = err.Error() - return ir, err + for a := range params.Body.Sheets { + + ls := &LoopStruct0{ + params, + params.Body.Sheets[a], + params.DirektivDir, + } + + cmd, err := templateString(`/bin/json2excel -inputJson {{- if eq (deref .Item.Infile) "" }} "input.json" {{- else }} {{ .Item.Infile }} {{- end }} -worksheetName "{{ .Item.WorksheetName }}" -outputFilename '{{ .OutFilename }}'`, ls) + if err != nil { + ir := make(map[string]interface{}) + ir[successKey] = false + ir[resultKey] = err.Error() + cmds = append(cmds, ir) + continue + } + + silent := convertTemplateToBool("false", ls, false) + print := convertTemplateToBool("true", ls, true) + cont := convertTemplateToBool("", ls, false) + output := "" + + envs := []string{} + + r, err := runCmd(ctx, cmd, envs, output, silent, print, ri) + if err != nil { + ir := make(map[string]interface{}) + ir[successKey] = false + ir[resultKey] = err.Error() + cmds = append(cmds, ir) + + if cont { + continue + } + + return cmds, err + + } + cmds = append(cmds, r) + } - cmd = strings.Replace(cmd, "\n", "", -1) - silent := convertTemplateToBool("false", at, false) - print := convertTemplateToBool("true", at, true) - output := "" - - envs := []string{} - - return runCmd(ctx, cmd, envs, output, silent, print, ri) + return cmds, nil } diff --git a/readme.md b/readme.md index db0c073..faf3575 100644 --- a/readme.md +++ b/readme.md @@ -31,12 +31,14 @@ functions: action: function: export-xlsx input: - files: - - name: input.json - data: | - jq(.input) - worksheet-name: "TestSpreadsheet" out-filename: example.xlsx + sheets: + - sheet: + worksheet-name: "TestSpreadsheet" + input-file: + - name: input.json + data: | + jq(.input) ``` #### Use jq to add a parent key 'input' containing array of objects ```yaml @@ -45,12 +47,14 @@ functions: action: function: export-xlsx input: - files: - - name: input.json - data: | - jq(.array | {input: . } | tostring ) - worksheet-name: "TestSpreadsheet" out-filename: example.xlsx + sheets: + - sheet: + worksheet-name: "TestSpreadsheet" + input-file: + - name: input.json + data: | + jq(.array | {input: . } | tostring ) ``` ### Secrets @@ -115,9 +119,22 @@ functions: | Name | Type | Go type | Required | Default | Description | Example | |------|------|---------|:--------:| ------- |-------------|---------| -| files | [][DirektivFile](#direktiv-file)| `[]apps.DirektivFile` | | | File to create before running commands. | | -| infile | string| `string` | | | Name of the json input file to utilise | | | out-filename | string| `string` | ✓ | `"output.xlsx"`| the filename of the output spreadsheet | | -| worksheet-name | string| `string` | ✓ | `"Sheet1"`| Label for the worksheet created in the spreadsheet | | +| sheets | [][PostParamsBodySheetsItems](#post-params-body-sheets-items)| `[]*PostParamsBodySheetsItems` | | | Array of worksheets to create in spreadsheet | | + + +#### postParamsBodySheetsItems + + + + + +**Properties** + +| Name | Type | Go type | Required | Default | Description | Example | +|------|------|---------|:--------:| ------- |-------------|---------| +| infile | string| `string` | | | Name of the json input file to utilise | | +| input-file | [][DirektivFile](#direktiv-file)| `[]apps.DirektivFile` | | | File to create before running commands | | +| worksheet-name | string| `string` | | `"Sheet1"`| Label for the worksheet created in the spreadsheet | |