You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/command/version/formatter.go

80 lines
1.8 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package version
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
const (
PrettyFormat string = "pretty"
JSONFormat string = "json"
)
type Formatter interface {
Format(info *VersionInfo) (string, error)
}
func GetSupportedFormats() []string {
return []string{PrettyFormat, JSONFormat}
}
func NewFormatter(format string) (Formatter, error) {
switch format {
case PrettyFormat:
return newPrettyFormatter(), nil
case JSONFormat:
return newJSONFormatter(), nil
default:
return nil, fmt.Errorf("Unknown format: %s", format)
}
}
type prettyFormatter struct{}
func newPrettyFormatter() Formatter {
return &prettyFormatter{}
}
func (_ *prettyFormatter) Format(info *VersionInfo) (string, error) {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Consul v%s\n", info.HumanVersion))
if info.Revision != "" {
buffer.WriteString(fmt.Sprintf("Revision %s\n", info.Revision))
}
buffer.WriteString(fmt.Sprintf("Build Date %s\n", info.BuildDate.Format(time.RFC3339)))
if info.FIPS != "" {
buffer.WriteString(fmt.Sprintf("FIPS: %s\n", info.FIPS))
}
var supplement string
if info.RPC.Default < info.RPC.Max {
supplement = fmt.Sprintf(" (agent will automatically use protocol >%d when speaking to compatible agents)",
info.RPC.Default)
}
buffer.WriteString(fmt.Sprintf("Protocol %d spoken by default, understands %d to %d%s\n",
info.RPC.Default, info.RPC.Min, info.RPC.Max, supplement))
return buffer.String(), nil
}
type jsonFormatter struct{}
func newJSONFormatter() Formatter {
return &jsonFormatter{}
}
func (_ *jsonFormatter) Format(info *VersionInfo) (string, error) {
b, err := json.MarshalIndent(info, "", " ")
if err != nil {
return "", fmt.Errorf("Failed to marshal version info: %v", err)
}
return string(b), nil
}