Skip to content

Commit

Permalink
Merge branch 'main' into hotfix_ambiguity_in_core_group
Browse files Browse the repository at this point in the history
  • Loading branch information
AvitalTamir authored Jan 3, 2025
2 parents c7ad676 + 06e4c6c commit 85cd7cf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
22 changes: 15 additions & 7 deletions cmd/cyphernetes/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/avitaltamir/cyphernetes/pkg/core"
"github.com/avitaltamir/cyphernetes/pkg/provider/apiserver"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -71,18 +72,25 @@ func runQuery(args []string, w io.Writer) {
return
}

// Print the results as pretty JSON.
json, err := json.MarshalIndent(results.Data, "", " ")
// Marshal data based on the output format
var output []byte
if core.OutputFormat == "yaml" {
output, err = yaml.Marshal(results.Data)
} else { // core.OutputFormat == "json"
output, err = json.MarshalIndent(results.Data, "", " ")
if !returnRawJsonOutput {
output = []byte(formatJson(string(output)))
}
}

// Handle marshalling errors
if err != nil {
fmt.Fprintln(w, "Error marshalling results: ", err)
return
}
if !returnRawJsonOutput {
json = []byte(formatJson(string(json)))
}

if string(json) != "{}" {
fmt.Fprintln(w, string(json))
if string(output) != "{}" {
fmt.Fprintln(w, string(output))
}
}

Expand Down
16 changes: 16 additions & 0 deletions cmd/cyphernetes/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ func TestRunQuery(t *testing.T) {
}
`,
},
{
name: "Successful query in YAML format",
args: []string{"MATCH (n:Pod)"},
mockParseQuery: func(query string) (*core.Expression, error) {
return &core.Expression{}, nil
},
mockExecute: func(expr *core.Expression, namespace string) (core.QueryResult, error) {
core.OutputFormat = "yaml"
return core.QueryResult{
Data: map[string]interface{}{
"test": "data",
},
}, nil
},
wantOut: "test: data\n\n",
},
{
name: "Parse query error",
args: []string{"INVALID QUERY"},
Expand Down
15 changes: 13 additions & 2 deletions cmd/cyphernetes/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,21 @@ func TestExecute(args []string) error {
func init() {
// First set the log level
rootCmd.PersistentFlags().StringVarP(&LogLevel, "loglevel", "l", "info", "The log level to use (debug, info, warn, error, fatal, panic)")
// Add a PreRun hook to set LogLevel after flag parsing
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {

// Set output format for shell and query
rootCmd.PersistentFlags().StringVar(&core.OutputFormat, "format", "json", "Output format for shell and query results")

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// Set LogLevel after flag parsing
LogLevel = cmd.Flag("loglevel").Value.String()
core.LogLevel = LogLevel

// Return an error if the format is incorrect
f := cmd.Flag("format").Value.String()
if f != "yaml" && f != "json" {
return fmt.Errorf("Invalid value for --format: must be 'json' or 'yaml'")
}
return nil
}

rootCmd.PersistentFlags().StringVarP(&core.Namespace, "namespace", "n", "default", "The namespace to query against")
Expand Down
18 changes: 16 additions & 2 deletions cmd/cyphernetes/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/avitaltamir/cyphernetes/pkg/provider/apiserver"
cobra "github.com/spf13/cobra"
"github.com/wader/readline"
"gopkg.in/yaml.v3"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/tools/clientcmd"
)
Expand Down Expand Up @@ -578,11 +579,24 @@ func buildDataAndGraph(resultMap map[string]interface{}, result *string, graph *
}

if data, ok := resultMap["Data"]; ok {
resultBytes, err := json.Marshal(data)
// Marshal data based on the output format
var output []byte
var err error
if core.OutputFormat == "yaml" {
output, err = yaml.Marshal(data)
} else { // core.OutputFormat == "json"
output, err = json.MarshalIndent(data, "", " ")
if !returnRawJsonOutput {
output = []byte(formatJson(string(output)))
}
}

// Handle marshalling errors
if err != nil {
return fmt.Errorf("error marshalling data: %w", err)
}
*result = string(resultBytes)

*result = string(output)
} else {
*result = "{}"
}
Expand Down
1 change: 1 addition & 0 deletions pkg/core/k8s_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type QueryExecutor struct {
var (
Namespace string
LogLevel string
OutputFormat string
AllNamespaces bool
CleanOutput bool
NoColor bool
Expand Down

0 comments on commit 85cd7cf

Please sign in to comment.