2014-10-13 23:05:29 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-checkpoint"
|
|
|
|
"github.com/hashicorp/terraform/command"
|
2020-01-13 22:50:05 +01:00
|
|
|
"github.com/hashicorp/terraform/command/cliconfig"
|
2014-10-13 23:05:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
checkpointResult = make(chan *checkpoint.CheckResponse, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkpointResult chan *checkpoint.CheckResponse
|
|
|
|
|
|
|
|
// runCheckpoint runs a HashiCorp Checkpoint request. You can read about
|
|
|
|
// Checkpoint here: https://github.com/hashicorp/go-checkpoint.
|
2020-01-13 22:50:05 +01:00
|
|
|
func runCheckpoint(c *cliconfig.Config) {
|
2014-10-13 23:05:29 +02:00
|
|
|
// If the user doesn't want checkpoint at all, then return.
|
|
|
|
if c.DisableCheckpoint {
|
|
|
|
log.Printf("[INFO] Checkpoint disabled. Not running.")
|
|
|
|
checkpointResult <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:50:05 +01:00
|
|
|
configDir, err := cliconfig.ConfigDir()
|
2014-10-13 23:05:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Checkpoint setup error: %s", err)
|
|
|
|
checkpointResult <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
version := Version
|
|
|
|
if VersionPrerelease != "" {
|
2015-02-07 16:55:59 +01:00
|
|
|
version += fmt.Sprintf("-%s", VersionPrerelease)
|
2014-10-13 23:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
signaturePath := filepath.Join(configDir, "checkpoint_signature")
|
|
|
|
if c.DisableCheckpointSignature {
|
|
|
|
log.Printf("[INFO] Checkpoint signature disabled")
|
|
|
|
signaturePath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := checkpoint.Check(&checkpoint.CheckParams{
|
|
|
|
Product: "terraform",
|
|
|
|
Version: version,
|
|
|
|
SignatureFile: signaturePath,
|
|
|
|
CacheFile: filepath.Join(configDir, "checkpoint_cache"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Checkpoint error: %s", err)
|
|
|
|
resp = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
checkpointResult <- resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// commandVersionCheck implements command.VersionCheckFunc and is used
|
|
|
|
// as the version checker.
|
|
|
|
func commandVersionCheck() (command.VersionCheckInfo, error) {
|
|
|
|
// Wait for the result to come through
|
|
|
|
info := <-checkpointResult
|
|
|
|
if info == nil {
|
|
|
|
var zero command.VersionCheckInfo
|
|
|
|
return zero, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the alerts that we may have received about our version
|
|
|
|
alerts := make([]string, len(info.Alerts))
|
|
|
|
for i, a := range info.Alerts {
|
|
|
|
alerts[i] = a.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
return command.VersionCheckInfo{
|
|
|
|
Outdated: info.Outdated,
|
|
|
|
Latest: info.CurrentVersion,
|
|
|
|
Alerts: alerts,
|
|
|
|
}, nil
|
|
|
|
}
|