Skip to content

Commit

Permalink
Merge pull request #16 from safesoftware/repositories-workspaces-DEVO…
Browse files Browse the repository at this point in the history
…PS-2780

Add Repositories and workspaces endpoints
  • Loading branch information
garnold54 authored Jan 18, 2023
2 parents 3a28e8e + d78c0f3 commit 184af8a
Show file tree
Hide file tree
Showing 13 changed files with 3,277 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cmd/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/spf13/viper"
)

type CancelMessage struct {
type Message struct {
Message string `json:"message"`
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func runCancel(f *cancelFlags) func(cmd *cobra.Command, args []string) error {
responseData, err := io.ReadAll(response.Body)
if err == nil {

var responseMessage CancelMessage
var responseMessage Message
if err := json.Unmarshal(responseData, &responseMessage); err == nil {

// if json output is requested, output the JSON to stdout before erroring
Expand Down
67 changes: 43 additions & 24 deletions cmd/custom-columns.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"

"github.com/PaesslerAG/jsonpath"
"github.com/jedib0t/go-pretty/v6/table"
"k8s.io/client-go/util/jsonpath"
)

var jsonRegexp = regexp.MustCompile(`^\{\.?([^{}]+)\}$|^\.?([^{}]+)$`)

// RelaxedJSONPathExpression attempts to be flexible with JSONPath expressions, it accepts:
// - metadata.name (no leading '.' or curly braces '{...}'
// - {metadata.name} (no leading '.')
// - .metadata.name (no curly braces '{...}')
// - {.metadata.name} (complete expression)
//
// And transforms them all into a valid jsonpath expression:
//
// {.metadata.name}
func RelaxedJSONPathExpression(pathExpression string) (string, error) {
if len(pathExpression) == 0 {
return pathExpression, nil
}
submatches := jsonRegexp.FindStringSubmatch(pathExpression)
if submatches == nil {
return "", fmt.Errorf("unexpected path string, expected a 'name1.name2' or '.name1.name2' or '{name1.name2}' or '{.name1.name2}'")
}
if len(submatches) != 3 {
return "", fmt.Errorf("unexpected submatch list: %v", submatches)
}
var fieldSpec string
if len(submatches[1]) != 0 {
fieldSpec = submatches[1]
} else {
fieldSpec = submatches[2]
}
return fmt.Sprintf("{.%s}", fieldSpec), nil
}

// This will create a table object and return it with the columns and queries specified by columnsInput
// applied to the jsonItems array
func createTableFromCustomColumns(jsonItems [][]byte, columnsInput []string) (table.Writer, error) {
Expand All @@ -28,11 +58,11 @@ func createTableFromCustomColumns(jsonItems [][]byte, columnsInput []string) (ta
if !strings.Contains(column, ":") {
return nil, errors.New("custom column \"" + column + "\" syntax invalid")
}
headerQueryArr := strings.Split(column, ":")
columnHeader := headerQueryArr[0]
columnQuery, err := massageQuery(headerQueryArr[1])
// split on the first instance of ":"
columnHeader, columnQuery, _ := strings.Cut(column, ":")
columnQuery, err := RelaxedJSONPathExpression(columnQuery)
if err != nil {
return nil, err
return nil, fmt.Errorf("error parsing JSON Query for custom column: %w", err)
}
if first {
headers = append(headers, columnHeader)
Expand All @@ -41,11 +71,16 @@ func createTableFromCustomColumns(jsonItems [][]byte, columnsInput []string) (ta
v := interface{}(nil)
json.Unmarshal(element, &v)

test, err := jsonpath.Get(columnQuery, v)
if err != nil {
j := jsonpath.New("Parser")
if err := j.Parse(columnQuery); err != nil {
return nil, err
}
row = append(row, test)
valueString := new(bytes.Buffer)
err = j.Execute(valueString, v)
if err != nil {
return nil, fmt.Errorf("error parsing JSON Query for custom column: %w", err)
}
row = append(row, valueString)
}
first = false
t.AppendRow(row)
Expand All @@ -54,19 +89,3 @@ func createTableFromCustomColumns(jsonItems [][]byte, columnsInput []string) (ta
t.AppendHeader(headers)
return t, nil
}
func massageQuery(q string) (string, error) {
submatches := jsonRegexp.FindStringSubmatch(q)
if submatches == nil {
return "", errors.New("unexpected path string, expected a 'name1.name2' or '.name1.name2' or '{name1.name2}' or '{.name1.name2}'")
}
if len(submatches) != 3 {
return "", fmt.Errorf("unexpected submatch list: %v", submatches)
}
var fieldSpec string
if len(submatches[1]) != 0 {
fieldSpec = submatches[1]
} else {
fieldSpec = submatches[2]
}
return fieldSpec, nil
}
Loading

0 comments on commit 184af8a

Please sign in to comment.