Skip to content

Commit

Permalink
Merge pull request #79 from fujiwara/interactive
Browse files Browse the repository at this point in the history
Add interactive mode to tfstate-lookup command and refactor object display.
  • Loading branch information
fujiwara committed Mar 24, 2023
2 parents f205ca6 + 2233bc2 commit 5216545
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $ brew install fujiwara/tap/tfstate-lookup

```
Usage of tfstate-lookup:
-i interactive mode
-s string
tfstate file path or URL (default "terraform.tfstate")
-state string
Expand Down
60 changes: 48 additions & 12 deletions cmd/tfstate-lookup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/manifoldco/promptui"
"github.com/mattn/go-isatty"
)

Expand All @@ -30,6 +31,7 @@ func _main() error {
var (
stateLoc string
defaultStateFile = DefaultStateFiles[0]
interactive bool
timeout time.Duration
)
for _, name := range DefaultStateFiles {
Expand All @@ -41,6 +43,7 @@ func _main() error {

flag.StringVar(&stateLoc, "state", defaultStateFile, "tfstate file path or URL")
flag.StringVar(&stateLoc, "s", defaultStateFile, "tfstate file path or URL")
flag.BoolVar(&interactive, "i", false, "interactive mode")
flag.DurationVar(&timeout, "timeout", 0, "timeout for reading tfstate")
flag.Parse()

Expand All @@ -60,22 +63,55 @@ func _main() error {
if err != nil {
return err
}
fmt.Println(strings.Join(names, "\n"))
if interactive {
selected, err := promptForSelection(names)
if err != nil {
return err
}
obj, err := state.Lookup(selected)
if err != nil {
return err
}
printObject(obj)
} else {
fmt.Println(strings.Join(names, "\n"))
}
} else {
res, err := state.Lookup(flag.Arg(0))
obj, err := state.Lookup(flag.Arg(0))
if err != nil {
return err
}
b := res.Bytes()
w := os.Stdout
if isatty.IsTerminal(w.Fd()) && (bytes.HasPrefix(b, []byte("[")) || bytes.HasPrefix(b, []byte("{"))) {
var out bytes.Buffer
json.Indent(&out, b, "", " ")
out.WriteRune('\n')
out.WriteTo(w)
} else {
fmt.Fprintln(w, string(b))
}
printObject(obj)
}
return nil
}

func printObject(obj *tfstate.Object) {
b := obj.Bytes()
w := os.Stdout
if isatty.IsTerminal(w.Fd()) && (bytes.HasPrefix(b, []byte("[")) || bytes.HasPrefix(b, []byte("{"))) {
var out bytes.Buffer
json.Indent(&out, b, "", " ")
out.WriteRune('\n')
out.WriteTo(w)
} else {
fmt.Fprintln(w, string(b))
}
}

func promptForSelection(choices []string) (string, error) {
prompt := promptui.Select{
Label: "Select an item",
Items: choices,
StartInSearchMode: true,
Size: 20,
Searcher: func(input string, index int) bool {
return strings.Contains(choices[index], input)
},
}
_, result, err := prompt.Run()
if err != nil {
return "", err
}
return result, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-tfe v1.2.0
github.com/itchyny/gojq v0.12.11
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-isatty v0.0.17
github.com/pkg/errors v0.9.1
google.golang.org/api v0.45.0
Expand Down Expand Up @@ -46,6 +47,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.12.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.3 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.18.4/go.mod h1:1mKZHLLpDMHTNSYPJ7qrc
github.com/aws/smithy-go v1.13.5 h1:hgz0X/DX0dGqTYpGALqXJoRKRj5oQ7150i5FdTePzO8=
github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down Expand Up @@ -249,6 +252,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-ieproxy v0.0.1 h1:qiyop7gCflfhwCzGyeT0gro3sF9AIg9HU98JORTkqfI=
github.com/mattn/go-ieproxy v0.0.1/go.mod h1:pYabZ6IHcRpFh7vIaLfK7rdcWgFEb3SFJ6/gNWuh88E=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
Expand Down Expand Up @@ -389,6 +394,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit 5216545

Please sign in to comment.