functionbuilder again
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-11-21 14:14:28 +11:00
parent d431eadbcc
commit e4392acbe3
5 changed files with 292 additions and 122 deletions

View File

@@ -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,11 +55,28 @@ 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
}
if err := validate.Required("worksheet-name", "body", m.WorksheetName); err != 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
}
}
}
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,18 +95,20 @@ 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 m.Sheets[i] != nil {
if err := m.Sheets[i].ContextValidate(ctx, formats); err != nil {
if ve, ok := err.(*errors.Validation); ok {
return ve.ValidateName("files" + "." + strconv.Itoa(i))
return ve.ValidateName("sheets" + "." + strconv.Itoa(i))
} else if ce, ok := err.(*errors.CompositeError); ok {
return ce.ValidateName("files" + "." + strconv.Itoa(i))
return ce.ValidateName("sheets" + "." + strconv.Itoa(i))
}
return err
}
}
}

View File

@@ -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
}

View File

@@ -68,27 +68,32 @@ func init() {
"schema": {
"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": {
"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"
}
},
"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",
@@ -97,6 +102,9 @@ func init() {
}
}
}
}
}
}
],
"responses": {
"200": {
@@ -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",

View File

@@ -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("<no value>", accParams, false)
// cont = convertTemplateToBool("<no value>", 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,
if params.Body == nil {
return cmds, nil
}
for a := range params.Body.Sheets {
ls := &LoopStruct0{
params,
params.Body.Sheets[a],
params.DirektivDir,
}
cmd, err := templateString(`/bin/json2excel -inputJson {{- if eq (deref .Infile) "" }} "input.json" {{- else }} {{ .Infile }} {{- end }} -worksheetName "{{ .WorksheetName }}" -outputFilename '{{ .OutFilename }}'`, at)
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 {
ri.Logger().Infof("error executing command: %v", err)
ir := make(map[string]interface{})
ir[successKey] = false
ir[resultKey] = err.Error()
return ir, err
cmds = append(cmds, ir)
continue
}
cmd = strings.Replace(cmd, "\n", "", -1)
silent := convertTemplateToBool("false", at, false)
print := convertTemplateToBool("true", at, true)
silent := convertTemplateToBool("false", ls, false)
print := convertTemplateToBool("true", ls, true)
cont := convertTemplateToBool("<no value>", ls, false)
output := ""
envs := []string{}
return runCmd(ctx, cmd, envs, output, silent, print, ri)
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)
}
return cmds, nil
}

View File

@@ -31,12 +31,14 @@ functions:
action:
function: export-xlsx
input:
files:
out-filename: example.xlsx
sheets:
- sheet:
worksheet-name: "TestSpreadsheet"
input-file:
- name: input.json
data: |
jq(.input)
worksheet-name: "TestSpreadsheet"
out-filename: example.xlsx
```
#### Use jq to add a parent key 'input' containing array of objects
```yaml
@@ -45,12 +47,14 @@ functions:
action:
function: export-xlsx
input:
files:
out-filename: example.xlsx
sheets:
- sheet:
worksheet-name: "TestSpreadsheet"
input-file:
- name: input.json
data: |
jq(.array | {input: . } | tostring )
worksheet-name: "TestSpreadsheet"
out-filename: example.xlsx
```
### 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 | |
#### <span id="post-params-body-sheets-items"></span> 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 | |