Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.23

- name: Build
run: make build
Expand Down
37 changes: 28 additions & 9 deletions cmd/hclgrep/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"errors"
"flag"
Expand All @@ -16,13 +17,17 @@
)

func main() {
var verbose bool
var verbosity int

flag.BoolFunc("v", "Verbosity level", func(_ string) error {
verbosity++
return nil
})

flag.BoolVar(&verbose, "v", false, "display Terraform body")
flag.Parse()

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
err := realMain(ctx, verbose, flag.Args())
err := realMain(ctx, verbosity, flag.Args())
cancel()

if err != nil {
Expand All @@ -31,7 +36,7 @@
}
}

func realMain(ctx context.Context, verbose bool, args []string) error {
func realMain(ctx context.Context, verbosity int, args []string) error {
if len(args) == 0 {
return errors.New("not enough arguments")
}
Expand All @@ -49,7 +54,7 @@
return err
}

print(os.Stdout, "-", src, res, verbose)
print(os.Stdout, "-", src, res, verbosity)

return nil
}
Expand Down Expand Up @@ -96,17 +101,31 @@
return err
}

print(os.Stdout, f, src, res, verbose)
print(os.Stdout, f, src, res, verbosity)
}

return nil
}

var lf = []byte{'\n'}
//nolint:errcheck
func print(w io.Writer, path string, src []byte, res []hcl.Range, verbosity int) {
if verbosity == 1 {
lines := bytes.Lines(src)

Check failure on line 113 in cmd/hclgrep/main.go

View workflow job for this annotation

GitHub Actions / build

undefined: bytes.Lines
line := 0
for _, r := range res {
for l := range lines {
line++
if line == r.Start.Line {
fmt.Fprintf(w, "%s:%d,%d-%d,%d:%s", path, r.Start.Line, r.Start.Column, r.End.Line, r.End.Column, l)
break
}
}
}
return
}

func print(w io.Writer, path string, src []byte, res []hcl.Range, verbose bool) {
for _, r := range res {
if verbose {
if verbosity >= 2 {
fmt.Fprintf(w, "%s @ %d,%d-%d,%d\n", path, r.Start.Line, r.Start.Column, r.End.Line, r.End.Column)
w.Write(r.SliceBytes(src)) //nolint:errcheck
fmt.Fprintf(w, "\n\n")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/inkel/hclgrep

go 1.19
go 1.23

require github.com/hashicorp/hcl/v2 v2.17.0

Expand Down
4 changes: 2 additions & 2 deletions test.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
terraform {
require_version = ">= 1.5.1"
required_version = ">= 1.5.1"
}

locals {
foo = "lorem ipsum dolor sit amet"
}

variable "bar" {
type = int
type = number
default = 1024
}

Expand Down
Loading