Skip to content

Commit

Permalink
feat(plugin/rpaasv2): show extra files on info command
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Jul 25, 2022
1 parent 78db734 commit ff6a8ee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cmd/plugin/rpaasv2/cmd/extra_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package cmd

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"unicode/utf8"

"github.com/olekukonko/tablewriter"
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client"
Expand Down Expand Up @@ -266,20 +267,29 @@ func runDeleteExtraFiles(c *cli.Context) error {
return nil
}

func writeExtraFilesOnTableFormat(writer io.Writer, files []types.RpaasFile) {
func writeExtraFilesOnTableFormat(files []types.RpaasFile) string {
var buffer bytes.Buffer

data := [][]string{}
for _, file := range files {
data = append(data, []string{file.Name, string(file.Content)})
content := string(file.Content)
if !utf8.Valid(file.Content) {
content = "WARNING!\nCANNOT SHOW THE FILE CONTENT AS IT'S NOT UTF-8 ENCODED."
}

data = append(data, []string{file.Name, content})
}

table := tablewriter.NewWriter(writer)
table := tablewriter.NewWriter(&buffer)
table.SetHeader([]string{"Name", "Content"})
table.SetAutoWrapText(false)
table.SetRowLine(true)
table.SetAutoFormatHeaders(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
table.Render()

return buffer.String()
}

func runListExtraFiles(c *cli.Context) error {
Expand All @@ -303,7 +313,7 @@ func runListExtraFiles(c *cli.Context) error {
fmt.Fprintln(c.App.Writer, file.Name)
}
case true:
writeExtraFilesOnTableFormat(c.App.Writer, files)
fmt.Fprint(c.App.Writer, writeExtraFilesOnTableFormat(files))
}
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/plugin/rpaasv2/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func newFuncMap() template.FuncMap {
"formatCertificates": writeCertificatesOnTableFormat,
"formatEvents": writeEventsOnTableFormat,
"formatACLs": writeAccessControlListOnTableFormat,
"formatExtraFiles": writeExtraFilesOnTableFormat,
}

for k, v := range sprig.HtmlFuncMap() {
Expand Down Expand Up @@ -131,6 +132,11 @@ Certificates:
{{ formatCertificates . }}
{{- end }}
{{- with .ExtraFiles }}
Extra files:
{{ formatExtraFiles . }}
{{- end }}
{{- with .Blocks }}
Blocks:
{{ formatBlocks . }}
Expand Down
14 changes: 14 additions & 0 deletions cmd/plugin/rpaasv2/cmd/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ func TestInfo(t *testing.T) {
WorkerConnections: 4096,
},
},
ExtraFiles: []clientTypes.RpaasFile{
{Name: "modsecurity.cfg", Content: []byte("a bunch of WAF configs...")},
{Name: "binary.exe", Content: []byte{66, 250, 0, 10}},
},
}, nil
},
},
Expand Down Expand Up @@ -453,6 +457,16 @@ Certificates:
| | 384 | 2050-07-31T00:00:00Z | |
+---------------+--------------------+----------------------+----------------------------+
Extra files:
+-----------------+---------------------------------------------------------+
| Name | Content |
+-----------------+---------------------------------------------------------+
| modsecurity.cfg | a bunch of WAF configs... |
+-----------------+---------------------------------------------------------+
| binary.exe | WARNING! |
| | CANNOT SHOW THE FILE CONTENT AS IT'S NOT UTF-8 ENCODED. |
+-----------------+---------------------------------------------------------+
Blocks:
+---------+---------------------------------------+
| Context | Configuration |
Expand Down
1 change: 1 addition & 0 deletions pkg/rpaas/client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type InstanceInfo struct {
Certificates []CertificateInfo `json:"certificates,omitempty"`
Events []Event `json:"events,omitempty"`
PlanOverride *v1alpha1.RpaasPlanSpec `json:"planOverride,omitempty"`
ExtraFiles []RpaasFile `json:"extraFiles,omitempty"`
}

type AllowedUpstream struct {
Expand Down

0 comments on commit ff6a8ee

Please sign in to comment.