Skip to content

Commit

Permalink
refactor: Align code base to tektoncd/cli repo approach
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 6, 2023
1 parent 1a7bf98 commit 0ed6710
Show file tree
Hide file tree
Showing 15 changed files with 788 additions and 223 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": ["--namespace", "edp-delivery-tekton-dev", "--kind", "PipelineRun", "--output-format", "dot"],
"args": ["pipelinerun", "graph", "--namespace", "edp-delivery-tekton-dev", "--output-format", "mmd"],
"cwd": "${workspaceFolder}"
}
]
Expand Down
119 changes: 5 additions & 114 deletions cmd/graph/main.go
Original file line number Diff line number Diff line change
@@ -1,126 +1,17 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/sergk/tkn-graph/pkg/client"
"github.com/sergk/tkn-graph/pkg/taskgraph"
"github.com/spf13/cobra"
"github.com/sergk/tkn-graph/pkg/cmd"
"github.com/tektoncd/cli/pkg/cli"
)

type Options struct {
Namespace string
TektonKind string
OutputFormat string
OutputDir string
WithTaskRef bool
}

func main() {
var options Options

// Define the root command
rootCmd := &cobra.Command{
Use: "tkn-graph",
Short: "Generate a graph of a Tekton object",
Long: "tkn-graph is a command-line tool for generating graphs from Tekton kind: Pipelines and kind: PipelineRuns.",
Example: ` graph --namespace my-namespace --kind Pipeline --output-format dot
graph --namespace my-namespace --kind PipelineRun --output-format puml
graph --namespace my-namespace --kind Pipeline --output-format mmd --output-dir /tmp/output`,
Run: func(cmd *cobra.Command, args []string) {
// Create the Kubernetes client
tektonClient, err := client.NewClient()
if err != nil {
log.Fatalf("Failed to create Tekton client: %v", err)
}

// Get the namespace to use
if options.Namespace == "" {
namespace, err := tektonClient.GetNamespace()
if err != nil {
log.Fatalf("Failed to get namespace: %v", err)
}
options.Namespace = namespace
}

// Build the list of task graphs
var graphs []*taskgraph.TaskGraph

switch options.TektonKind {
case "Pipeline":
pipelines, err := tektonClient.GetPipelines(options.Namespace)
if err != nil {
log.Fatalf("Failed to get Pipelines: %v", err)
}
for i := range pipelines {
pipeline := &pipelines[i]
graph := taskgraph.BuildTaskGraph(pipeline.Spec.Tasks)
graph.PipelineName = pipeline.Name
graphs = append(graphs, graph)
}

case "PipelineRun":
pipelineRuns, err := tektonClient.GetPipelineRuns(options.Namespace)
if err != nil {
log.Fatalf("Failed to get PipelineRuns: %v", err)
}
for i := range pipelineRuns {
pipelineRun := &pipelineRuns[i]
graph := taskgraph.BuildTaskGraph(pipelineRun.Status.PipelineSpec.Tasks)
graph.PipelineName = pipelineRun.Name
graphs = append(graphs, graph)
}

default:
log.Fatalf("Invalid kind type: %s", options.TektonKind)
}

// Generate graph for each object
for _, graph := range graphs {
// Generate the output format string
output, err := taskgraph.FormatFunc(graph, options.OutputFormat, options.WithTaskRef)
if err != nil {
log.Fatalf("Failed to generate output: %v", err)
}

// Print or save the graph
if options.OutputDir == "" {
// Print the graph to the screen
fmt.Println(output)
} else {
// Save the graph to a file
err := os.MkdirAll(options.OutputDir, 0755)
if err != nil {
log.Fatalf("Failed to create directory %s: %v", options.OutputDir, err)
}
filename := filepath.Join(options.OutputDir, fmt.Sprintf("%s.%s", graph.PipelineName, options.OutputFormat))
err = os.WriteFile(filename, []byte(output), 0600)
if err != nil {
log.Fatalf("Failed to write file %s: %v", filename, err)
}
}
}
},
}

// Define the command-line options
rootCmd.Flags().StringVar(
&options.Namespace, "namespace", "", "the Kubernetes namespace to use. Will try to get namespace from KUBECONFIG if not specified then fallback to 'default'")
rootCmd.Flags().StringVar(
&options.TektonKind, "kind", "Pipeline", "the kind of the Tekton object to parse (Pipeline or PipelineRun)")
rootCmd.Flags().StringVar(
&options.OutputFormat, "output-format", "dot", "the output format (dot - DOT, puml - PlantUML or mmd - Mermaid)")
rootCmd.Flags().StringVar(
&options.OutputDir, "output-dir", "", "the directory to save the output files. Otherwise, the output is printed to the screen")
rootCmd.Flags().BoolVar(
&options.WithTaskRef, "with-task-ref", false, "Include TaskRefName information in the output")
tp := &cli.TektonParams{}
tkn := cmd.Root(tp)

// Parse the command-line options
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
if err := tkn.Execute(); err != nil {
os.Exit(1)
}
}
48 changes: 30 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,70 @@ go 1.21.1

require (
github.com/stretchr/testify v1.8.4
github.com/tektoncd/cli v0.32.0
k8s.io/client-go v0.28.2
)

require (
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blendle/zapdriver v1.3.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/cel-go v0.17.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/statsd_exporter v0.21.0 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/tektoncd/triggers v0.25.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/sync v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/api v0.138.0 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/grpc v1.58.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.2 // indirect
k8s.io/apiextensions-apiserver v0.26.5 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
knative.dev/pkg v0.0.0-20230718152110-aef227e72ead // indirect
)
Expand All @@ -67,18 +79,18 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/pflag v1.0.5
github.com/tektoncd/pipeline v0.52.0
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
Loading

0 comments on commit 0ed6710

Please sign in to comment.