Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON output for getting product versions #23

Open
radeksimko opened this issue Oct 27, 2021 · 0 comments
Open

Use JSON output for getting product versions #23

radeksimko opened this issue Oct 27, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@radeksimko
Copy link
Member

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
},

@radeksimko radeksimko added the enhancement New feature or request label Oct 27, 2021
@radeksimko radeksimko changed the title Use version -json for getting product versions Use JSON output for getting product versions Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant