diff --git a/.drone.yml b/.drone.yml index ea7cd86..5f5c1d8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -9,7 +9,7 @@ pipeline: - CGO_ENABLED=0 commands: - go test -cover -coverprofile=coverage.out - - go build -ldflags "-s -w -X main.build=$DRONE_BUILD_NUMBER" -a + - go build -ldflags "-s -w -X main.revision=$(git rev-parse HEAD)" -a coverage: image: coverage diff --git a/main.go b/main.go index 2e36ae0..2669980 100644 --- a/main.go +++ b/main.go @@ -9,14 +9,14 @@ import ( "github.com/urfave/cli" ) -var version string // build number set at compile-time +var revision string // build number set at compile-time func main() { app := cli.NewApp() app.Name = "terraform plugin" app.Usage = "terraform plugin" app.Action = run - app.Version = version + app.Version = revision app.Flags = []cli.Flag{ // @@ -81,6 +81,10 @@ func main() { } func run(c *cli.Context) error { + logrus.WithFields(logrus.Fields{ + "Revision": revision, + }).Info("Drone Terraform Plugin Version") + if c.String("env-file") != "" { _ = godotenv.Load(c.String("env-file")) } diff --git a/plugin.go b/plugin.go index 34c6031..7a8adba 100644 --- a/plugin.go +++ b/plugin.go @@ -6,6 +6,7 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sts" + "github.com/Sirupsen/logrus" "io/ioutil" "os" "os/exec" @@ -13,10 +14,6 @@ import ( "time" ) -var ( - buildCommit string -) - type ( Config struct { Remote Remote @@ -41,8 +38,6 @@ type ( ) func (p Plugin) Exec() error { - fmt.Printf("Drone Terraform Plugin built from %s\n", buildCommit) - if p.Config.RoleARN != "" { assumeRole(p.Config.RoleARN) } @@ -81,11 +76,11 @@ func (p Plugin) Exec() error { err := c.Run() if err != nil { - fmt.Println("Error!") - fmt.Println(err) - os.Exit(1) + logrus.WithFields(logrus.Fields{ + "error": err, + }).Fatal("Failed to execute a command") } - fmt.Println("Command completed successfully") + logrus.Debug("Command completed successfully") } return nil @@ -176,9 +171,9 @@ func assumeRole(roleArn string) { value, err := credentials.NewCredentials(stsProvider).Get() if err != nil { - fmt.Println("Error assuming role!") - fmt.Println(err) - os.Exit(1) + logrus.WithFields(logrus.Fields{ + "error": err, + }).Fatal("Error assuming role!") } os.Setenv("AWS_ACCESS_KEY_ID", value.AccessKeyID) os.Setenv("AWS_SECRET_ACCESS_KEY", value.SecretAccessKey)