Skip to content

Commit

Permalink
chore(cli): set up logging generically (#138)
Browse files Browse the repository at this point in the history
* chore(cli): set up logging generically

* chore(selectors): add logging
  • Loading branch information
Tieske committed Jan 19, 2024
1 parent f38f736 commit ff8e2ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"log"
"os"

"github.com/kong/go-apiops/logbasics"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +18,16 @@ var rootCmd = &cobra.Command{
Long: `A CLI for testing the Kong go-apiops library.
go-apiops houses an improved APIOps toolset for operating Kong Gateway deployments.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// set the verbosity level of the log output
verbosity, err := cmd.Flags().GetInt("verbose")
if err != nil {
return err
}
logbasics.Initialize(log.LstdFlags, verbosity)
return nil
},

// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down
22 changes: 16 additions & 6 deletions yamlbasics/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yamlbasics
import (
"fmt"

"github.com/kong/go-apiops/logbasics"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -44,9 +45,10 @@ func IsIntersection(mainSet NodeSet, subset NodeSet) bool {

// Intersection returns the intersection of the two given sets of nodes.
// nil entries will be ignored. The result will have no duplicates.
func Intersection(set1, set2 NodeSet) NodeSet {
// the second return value is the remainder of set2 after the intersection was removed.
func Intersection(set1, set2 NodeSet) (intersection NodeSet, remainder NodeSet) {
if len(set1) == 0 || len(set2) == 0 {
return make(NodeSet, 0)
return make(NodeSet, 0), make(NodeSet, 0)
}

// deduplicate
Expand All @@ -57,20 +59,27 @@ func Intersection(set1, set2 NodeSet) NodeSet {
}
}

intersection := make(NodeSet, 0)
intersection = make(NodeSet, 0)
remainder = make(NodeSet, 0)
seen2 := make(map[*yaml.Node]bool)
for _, node := range set2 {
if node != nil && seen1[node] && !seen2[node] {
if node != nil && !seen2[node] {
seen2[node] = true
intersection = append(intersection, node)
if seen1[node] {
intersection = append(intersection, node)
} else {
remainder = append(remainder, node)
}
}
}
return intersection
logbasics.Debug("intersection", "#found", len(intersection), "#remainder", len(remainder))
return intersection, remainder
}

// SubtractSet returns the set of nodes that are in mainSet but not in setToSubtract.
// nil entries will be ignored. The result will have no duplicates.
func SubtractSet(mainSet NodeSet, setToSubtract NodeSet) NodeSet {
// TODO: this can be implemneted by using Intersection, and returning the remainderset.
if len(mainSet) == 0 || len(setToSubtract) == 0 {
return make(NodeSet, 0)
}
Expand Down Expand Up @@ -164,6 +173,7 @@ func (set *SelectorSet) Find(nodeToSearch *yaml.Node) (NodeSet, error) {
if err != nil {
return nil, fmt.Errorf("failed to execute selector '%s'; %w", set.source[i], err)
}
logbasics.Debug("selector results", "selector", set.source[i], "#found", len(matches))
for _, match := range matches {
if match != nil && !seen[match] {
results = append(results, match)
Expand Down

0 comments on commit ff8e2ca

Please sign in to comment.