Skip to content

Commit

Permalink
load plan
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Sep 27, 2023
1 parent a82e35f commit 744303e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.tools
.gopath
tf-kyverno
.terraform/
.terraform.lock.hcl
tf.plan
tf.plan.json
17 changes: 15 additions & 2 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package commands

import (
"errors"
"fmt"

"github.com/eddycharly/tf-kyverno/pkg/plan"
"github.com/eddycharly/tf-kyverno/pkg/policy"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/pluralize"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type command struct {
Expand All @@ -22,11 +25,21 @@ func (c *command) Run(cmd *cobra.Command, _ []string) error {
}
fmt.Fprintln(out, "-", len(policies), pluralize.Pluralize(len(policies), "policy", "policies"), "loaded")
fmt.Fprintln(out, "Loading plan ...")
// TODO
plan, err := plan.Load(c.plan)
if err != nil {
return err
}
resources, ok, err := unstructured.NestedSlice(plan, "planned_values", "root_module", "resources")
if err != nil {
return err
}
if !ok {
return errors.New("failed to find resources in the plan")
}
fmt.Fprintln(out, "-", len(resources), pluralize.Pluralize(len(resources), "resource", "resources"), "loaded")
fmt.Fprintln(out, "Running ...")
// TODO
fmt.Fprintln(out, "Done")
// TODO
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/plan/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package plan

import (
"encoding/json"
"os"
"path/filepath"
)

func Load(path string) (map[string]interface{}, error) {
content, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
var plan map[string]interface{}
if err := json.Unmarshal(content, &plan); err != nil {
return nil, err
}
return plan, nil
}

0 comments on commit 744303e

Please sign in to comment.