hashicorp / hc-install

Go module for downloading or locating HashiCorp binaries, verifying signatures and checksums, and asserting version constraints.

Home Page:https://pkg.go.dev/github.com/hashicorp/hc-install

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use JSON output for getting product versions

radeksimko opened this issue · comments

Currently we leverage the default (mainly human-readable) output of terraform version and consul version, but both have introduced JSON output which we could leverage and fall back to the default human output if parsing or execution fails.

https://www.terraform.io/docs/cli/commands/version.html#json
https://www.consul.io/commands/version#json-output

GetVersion: func(ctx context.Context, path string) (*version.Version, error) {
cmd := exec.CommandContext(ctx, path, "version")
out, err := cmd.Output()
if err != nil {
return nil, err
}
stdout := strings.TrimSpace(string(out))
submatches := terraformVersionOutputRe.FindStringSubmatch(stdout)
if len(submatches) != 2 {
return nil, fmt.Errorf("unexpected number of version matches %d for %s", len(submatches), stdout)
}
v, err := version.NewVersion(submatches[1])
if err != nil {
return nil, fmt.Errorf("unable to parse version %q: %w", submatches[1], err)
}
return v, err
},

GetVersion: func(ctx context.Context, path string) (*version.Version, error) {
cmd := exec.CommandContext(ctx, path, "version")
out, err := cmd.Output()
if err != nil {
return nil, err
}
stdout := strings.TrimSpace(string(out))
submatches := consulVersionOutputRe.FindStringSubmatch(stdout)
if len(submatches) != 2 {
return nil, fmt.Errorf("unexpected number of version matches %d for %s", len(submatches), stdout)
}
v, err := version.NewVersion(submatches[1])
if err != nil {
return nil, fmt.Errorf("unable to parse version %q: %w", submatches[1], err)
}
return v, err
},