Skip to content

Commit

Permalink
feat: add logging w/ DEMUX_LOG env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnorris committed May 10, 2021
1 parent 28270b4 commit e8524c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
terraform-demux
/terraform-demux
15 changes: 14 additions & 1 deletion cmd/terraform-demux/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package main

import (
"io/ioutil"
"log"
"os"

"github.com/etsy/terraform-demux/internal/wrapper"
)

var (
Version = "v0.0.1+dev"
)

func main() {
if os.Getenv("DEMUX_LOG") == "" {
log.SetOutput(ioutil.Discard)
}

log.Printf("terraform-demux version %s", Version)

exitCode, err := wrapper.RunTerraform(os.Args[1:])

if err != nil {
log.Fatal(err)
log.SetOutput(os.Stderr)

log.Fatal("error: ", err)
}

os.Exit(exitCode)
Expand Down
11 changes: 10 additions & 1 deletion internal/releaseapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -89,6 +90,8 @@ func (c *Client) ListReleases(ctx context.Context) (ListReleasesResponse, error)
if err != nil {
return releases, err
} else if isFresh {
log.Printf("using cached release list")

return releases, nil
}

Expand All @@ -102,6 +105,8 @@ func (c *Client) ListReleases(ctx context.Context) (ListReleasesResponse, error)
req.Header.Set(`If-None-Match`, releases.ETag)
}

log.Printf("downloading release index from Hashicorp")

resp, err := c.httpclient.Do(req)

if err != nil {
Expand Down Expand Up @@ -233,11 +238,15 @@ func (c *Client) downloadBuild(b Build) (string, error) {
path := cachedExecutablePath(c.cacheDir, b)

if _, err := os.Stat(path); err == nil {
log.Printf("found cached Terraform executable at %s", path)

return path, nil
} else if !os.IsNotExist(err) {
return "", errors.Wrap(err, "could not stat terraform executable")
return "", errors.Wrap(err, "could not stat Terraform executable")
}

log.Printf("dowloading release archive from %s", b.URL)

zip, zipCleanupFunc, err := c.downloadZip(b.URL)

defer zipCleanupFunc()
Expand Down
11 changes: 10 additions & 1 deletion internal/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wrapper
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -80,9 +81,13 @@ func getTerraformVersionConstraints(directory string) ([]version.Constraints, er
currentDirectory := directory

for {
log.Printf("inspecting terraform module in %s", currentDirectory)

module, diags := tfconfig.LoadModule(currentDirectory)

if !diags.HasErrors() && len(module.RequiredCore) > 0 {
if diags.HasErrors() {
log.Printf("encountered error parsing configuration: %v", diags.Err())
} else if len(module.RequiredCore) > 0 {
var allConstraints []version.Constraints

for _, constraintString := range module.RequiredCore {
Expand All @@ -95,12 +100,16 @@ func getTerraformVersionConstraints(directory string) ([]version.Constraints, er
allConstraints = append(allConstraints, constraints)
}

log.Printf("found constraints: %v", allConstraints)

return allConstraints, nil
}

parentDirectory := filepath.Dir(currentDirectory)

if parentDirectory == currentDirectory {
log.Printf("no constraints found")

return nil, nil
}

Expand Down

0 comments on commit e8524c6

Please sign in to comment.