Skip to content

Commit

Permalink
Add linting and fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rishinair11 committed Jun 23, 2024
1 parent 2fd5ea6 commit 6df0d9d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ jobs:
with:
go-version: '1.22.3'

- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.59.0
args: --config=./.golangci.yml

- name: Build
run: make build

Expand Down
27 changes: 27 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
run:
go: "1.22.3"
concurrency: 8
timeout: 5m
build-tags:
- make

issues:
max-issues-per-linter: 0
max-same-issues: 0

linters-settings:
misspell:
locale: US

linters:
disable-all: true
enable:
- errcheck
- goimports
- gosimple
- govet
- ineffassign
- misspell
- staticcheck
- typecheck
- unused
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ build:
$(GO) build -o $(OUTPUT)
@echo "Binary at: $(OUTPUT)"

lint:
@echo "==> Running golangci-lint"
@golangci-lint run --config .golangci.yml

test:
$(GO) test -v ./...

Expand Down
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"io"
"log"
"os"
Expand All @@ -24,7 +23,7 @@ func main() {
var err error

// Read YAML input
yamlBytes, err = readYAML(inputFile)
yamlBytes, err = readInput(inputFile)
if err != nil {
log.Fatalf("Failed to read YAML: %v", err)
}
Expand All @@ -36,28 +35,34 @@ func main() {
}

// Process the graph
graph := graph.ProcessGraph(t)
graph, err := graph.ProcessGraph(t)
if err != nil {
log.Fatal(err)
}

// Write the graph to the output file
if err := os.WriteFile(outputFile, []byte(graph.String()), 0o755); err != nil {
log.Fatalf("Failed to write output file: %v", err)
}

log.Println("Generated graph:", outputFile)
},
}

rootCmd.Flags().StringVarP(&inputFile, "file", "f", "", "Specify input file")
rootCmd.Flags().StringVarP(&outputFile, "output", "o", "graph.dot", "Specify output file")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatal(err)
}
}

// readYAML reads the YAML input from a file or stdin
func readYAML(inputFile string) ([]byte, error) {
// readInput reads the YAML input from a file or stdin
func readInput(inputFile string) ([]byte, error) {
if inputFile != "" {
log.Println("Reading from file:", inputFile)
return os.ReadFile(inputFile)
}
log.Println("Reading from STDIN...")
return io.ReadAll(os.Stdin)
}
56 changes: 40 additions & 16 deletions pkg/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,62 @@ import (
)

// ProcessGraph creates a Graphviz graph from the given ResourceTree
func ProcessGraph(t *resource.ResourceTree) *gographviz.Graph {
func ProcessGraph(t *resource.ResourceTree) (*gographviz.Graph, error) {
log.Println("Processing tree...")

graphName := "fluxgraph"
g := gographviz.NewGraph()
g.SetDir(true)
g.SetName(graphName)
g.AddAttr(graphName, string(gographviz.RankDir), "LR")
g.AddAttr(graphName, string(gographviz.RankSep), "5.0")
g.SetDir(true) //nolint errcheck 'always returns nil'
g.SetName(graphName) //nolint errcheck 'always returns nil'
if err := g.Attrs.Add(string(gographviz.RankDir), "LR"); err != nil {
log.Fatal(err)
}
if err := g.Attrs.Add(string(gographviz.RankSep), "5.0"); err != nil {
log.Fatal(err)
}

// Initialize stack and process the root nodes
s := stack.New()
processRootNodes(g, t, s)
if err := processRootNodes(g, t, s); err != nil {
return nil, err
}

// Process the graph nodes
processOtherNodes(g, s)
if err := processOtherNodes(g, s); err != nil {
return nil, err
}

return g
return g, nil
}

// processRootNodes processes the root nodes of the resource tree
func processRootNodes(g *gographviz.Graph, root *resource.ResourceTree, s *stack.Stack) {
func processRootNodes(g *gographviz.Graph, root *resource.ResourceTree, s *stack.Stack) error {
// Add root node to graph
g.AddNode(g.Name, getName(root.Resource), getNodeAttrs(root.Resource, root.Resource))
if err := g.AddNode(g.Name, getName(root.Resource), getNodeAttrs(root.Resource, root.Resource)); err != nil {
return err
}
for _, child := range root.Resources {
// Add child nodes of root node to graph
g.AddNode(g.Name, getName(child.Resource), getNodeAttrs(child.Resource, root.Resource))
if err := g.AddNode(g.Name, getName(child.Resource), getNodeAttrs(child.Resource, root.Resource)); err != nil {
return err
}
// Add edge from root node to child node
g.AddEdge(getName(root.Resource), getName(child.Resource), true, setAttrsColorAndStyle(make(map[string]string), root.Resource.GetKind()))
if err := g.AddEdge(getName(root.Resource), getName(child.Resource), true, setAttrsColorAndStyle(make(map[string]string), root.Resource.GetKind())); err != nil {
return err
}

s.Push(child)
}

return nil
}

// processOtherNodes processes the nodes in the graph using a stack
func processOtherNodes(g *gographviz.Graph, s *stack.Stack) {
func processOtherNodes(g *gographviz.Graph, s *stack.Stack) error {
for {
poppedResource := s.Pop()
if poppedResource == nil {
log.Println("Done!")
log.Println("Done processing!")
break
}

Expand All @@ -58,13 +76,19 @@ func processOtherNodes(g *gographviz.Graph, s *stack.Stack) {

for _, child := range parent.Resources {
// Add child node of parent node to graph
g.AddNode(g.Name, getName(child.Resource), getNodeAttrs(child.Resource, parent.Resource))
if err := g.AddNode(g.Name, getName(child.Resource), getNodeAttrs(child.Resource, parent.Resource)); err != nil {
return err
}
// Add edge from parent node to child node
g.AddEdge(getName(parent.Resource), getName(child.Resource), true, setAttrsColorAndStyle(make(map[string]string), parent.Resource.GetKind()))
if err := g.AddEdge(getName(parent.Resource), getName(child.Resource), true, setAttrsColorAndStyle(make(map[string]string), parent.Resource.GetKind())); err != nil {
return err
}

s.Push(child)
}
}

return nil
}

// getName returns the formatted name for a resource
Expand Down

0 comments on commit 6df0d9d

Please sign in to comment.