mirror of
https://github.com/adammck/terraform-inventory
synced 2024-11-23 00:12:13 +01:00
Check .terraform/terraform.tfstate, for remote state (#41)
* Move input path stuff to GetInputPath * Look for .terraform/terraform.tfstate * Update README
This commit is contained in:
parent
649b4a540e
commit
9493db0fbd
@ -24,8 +24,8 @@ ready to go.
|
||||
|
||||
## Usage
|
||||
|
||||
If your Terraform state file is named `terraform.tfstate` (the default), `cd` to
|
||||
it and run:
|
||||
If you are using [remote state][rs] (or if your state file happens to be named
|
||||
`terraform.tfstate`), `cd` to it and run:
|
||||
|
||||
ansible-playbook --inventory-file=/path/to/terraform-inventory deploy/playbook.yml
|
||||
|
||||
@ -139,4 +139,5 @@ MIT.
|
||||
|
||||
[ansible]: https://www.ansible.com
|
||||
[tf]: https://www.terraform.io
|
||||
[rel]: https://github.com/adammck/terraform-inventory/releases
|
||||
[rel]: https://github.com/adammck/terraform-inventory/releases
|
||||
[rs]: https://www.terraform.io/docs/state/remote/index.html
|
||||
|
35
input.go
Normal file
35
input.go
Normal file
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/adammck/venv"
|
||||
"github.com/blang/vfs"
|
||||
)
|
||||
|
||||
func GetInputPath(fs vfs.Filesystem, env venv.Env) string {
|
||||
|
||||
var fn string
|
||||
|
||||
fn = env.Getenv("TF_STATE")
|
||||
if fn != "" {
|
||||
return fn
|
||||
}
|
||||
|
||||
fn = env.Getenv("TI_TFSTATE")
|
||||
if fn != "" {
|
||||
return fn
|
||||
}
|
||||
|
||||
fn = "terraform.tfstate"
|
||||
_, err := fs.Stat(fn)
|
||||
if err == nil {
|
||||
return fn
|
||||
}
|
||||
|
||||
fn = ".terraform/terraform.tfstate"
|
||||
_, err = fs.Stat(fn)
|
||||
if err == nil {
|
||||
return fn
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
75
input_test.go
Normal file
75
input_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/adammck/venv"
|
||||
"github.com/blang/vfs"
|
||||
"github.com/blang/vfs/memfs"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetInputPath(t *testing.T) {
|
||||
assert.Equal(t, "", GetInputPath(memfs.Create(), venv.Mock()))
|
||||
assert.Equal(t, "aaa", GetInputPath(memfs.Create(), envWith(map[string]string{"TF_STATE": "aaa"})))
|
||||
assert.Equal(t, "bbb", GetInputPath(memfs.Create(), envWith(map[string]string{"TI_TFSTATE": "bbb"})))
|
||||
assert.Equal(t, "terraform.tfstate", GetInputPath(fsWithFiles([]string{"terraform.tfstate"}), venv.Mock()))
|
||||
assert.Equal(t, ".terraform/terraform.tfstate", GetInputPath(fsWithFiles([]string{".terraform/terraform.tfstate"}), venv.Mock()))
|
||||
}
|
||||
|
||||
func envWith(env map[string]string) venv.Env {
|
||||
e := venv.Mock()
|
||||
|
||||
for k, v := range env {
|
||||
e.Setenv(k, v)
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func fsWithFiles(filenames []string) vfs.Filesystem {
|
||||
fs := memfs.Create()
|
||||
var err error
|
||||
|
||||
for _, fn := range filenames {
|
||||
|
||||
path := filepath.Dir(fn)
|
||||
if path != "" {
|
||||
err = vfs.MkdirAll(fs, path, 0700)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
err = touchFile(fs, fn)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return fs
|
||||
}
|
||||
|
||||
// TODO: Upgrade this later with file contents.
|
||||
func touchFile(fs vfs.Filesystem, filename string) error {
|
||||
return writeFile(fs, filename, []byte{}, 0600)
|
||||
}
|
||||
|
||||
// port of ioutil.Writefile for vfs
|
||||
func writeFile(fs vfs.Filesystem, filename string, data []byte, perm os.FileMode) error {
|
||||
f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := f.Write(data)
|
||||
if err == nil && n < len(data) {
|
||||
err = io.ErrShortWrite
|
||||
}
|
||||
if err1 := f.Close(); err == nil {
|
||||
err = err1
|
||||
}
|
||||
return err
|
||||
}
|
21
main.go
21
main.go
@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/adammck/venv"
|
||||
"github.com/blang/vfs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
@ -21,23 +23,10 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// not given on the command line? try ENV.
|
||||
if file == "" {
|
||||
file = os.Getenv("TF_STATE")
|
||||
}
|
||||
|
||||
// also try the old ENV name.
|
||||
if file == "" {
|
||||
file = os.Getenv("TI_TFSTATE")
|
||||
}
|
||||
|
||||
// check for a file named terraform.tfstate in the pwd
|
||||
if file == "" {
|
||||
fn := "terraform.tfstate"
|
||||
_, err := os.Stat(fn)
|
||||
if err == nil {
|
||||
file = fn
|
||||
}
|
||||
fs := vfs.OS()
|
||||
env := venv.OS()
|
||||
file = GetInputPath(fs, env)
|
||||
}
|
||||
|
||||
if file == "" {
|
||||
|
Loading…
Reference in New Issue
Block a user