cmd/tailscale/cli/jsonoutput: rename exported identifiers (#19994)
Since we don’t think anyone has actually imported the jsonoutput
package yet, we still have a chance to rename its fundamental types:
1. Rename the JSONSchemaVersion struct to SchemaVersion because
it is a flag.Value that can represent any schema version.
2. Rename the JSONSchemaVersion.Value field to SchemaVersion.Version
so the struct reads better:
if args.json.IsSet && args.json.Version == 1 {
// ...
}
Updates #17613
Signed-off-by: Simon Law <sfllaw@tailscale.com>
This commit is contained in:
+4
-4
@@ -11,15 +11,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var args struct {
|
var args struct {
|
||||||
json jsonoutput.JSONSchemaVersion
|
json jsonoutput.SchemaVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleJSONSchemaVersion() {
|
func ExampleSchemaVersion() {
|
||||||
fs := flag.NewFlagSet("ExampleJSONSchemaVersion", flag.ExitOnError)
|
fs := flag.NewFlagSet("ExampleSchemaVersion", flag.ExitOnError)
|
||||||
fs.Var(&args.json, "json", "output in JSON format")
|
fs.Var(&args.json, "json", "output in JSON format")
|
||||||
|
|
||||||
fs.Parse([]string{"-json=2"})
|
fs.Parse([]string{"-json=2"})
|
||||||
fmt.Printf(`{set: %t, value: %d}`, args.json.IsSet, args.json.Value)
|
fmt.Printf(`{set: %t, value: %d}`, args.json.IsSet, args.json.Version)
|
||||||
// Output:
|
// Output:
|
||||||
// {set: true, value: 2}
|
// {set: true, value: 2}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
// Historically we only used a boolean -json flag, so changing the output
|
// Historically we only used a boolean -json flag, so changing the output
|
||||||
// could break scripts that rely on the existing format.
|
// could break scripts that rely on the existing format.
|
||||||
//
|
//
|
||||||
// This package provides a [JSONSchemaVersion] flag type that allows callers
|
// This package provides a [SchemaVersion] flag type that allows callers
|
||||||
// to pass either a boolean or a version number and get a consistent output.
|
// to pass either a boolean or a version number and get a consistent output.
|
||||||
// We'll bump the version when we make a breaking change
|
// We'll bump the version when we make a breaking change
|
||||||
// that's likely to break scripts that rely on the existing output,
|
// that's likely to break scripts that rely on the existing output,
|
||||||
@@ -27,32 +27,32 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ flag.Value = &JSONSchemaVersion{}
|
var _ flag.Value = &SchemaVersion{}
|
||||||
|
|
||||||
// JSONSchemaVersion implements the [flag.Value] interface,
|
// SchemaVersion implements the [flag.Value] interface,
|
||||||
// tracking whether the flag has been set or cleared, and its value when set.
|
// tracking whether the flag has been set or cleared, and its value when set.
|
||||||
type JSONSchemaVersion struct {
|
type SchemaVersion struct {
|
||||||
// IsSet tracks if the flag was set or cleared.
|
// IsSet tracks if the flag was set or cleared.
|
||||||
// This flag is true when set by -name or -name=true or -name=INT,
|
// This flag is true when set by -name or -name=true or -name=INT,
|
||||||
// otherwise it is false when cleared by -name=false.
|
// otherwise it is false when cleared by -name=false.
|
||||||
IsSet bool
|
IsSet bool
|
||||||
|
|
||||||
// Value tracks the desired schema version, as set by the -name=INT flag.
|
// Version tracks the desired schema version, as set by the -name=INT flag.
|
||||||
// The version defaults to 1 when implicitly set by -name or -name=true.
|
// The version defaults to 1 when implicitly set by -name or -name=true.
|
||||||
Value int
|
Version int
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the default value which is printed in the CLI help text.
|
// String returns the default value which is printed in the CLI help text.
|
||||||
func (v *JSONSchemaVersion) String() string {
|
func (v *SchemaVersion) String() string {
|
||||||
if v.IsSet {
|
if v.IsSet {
|
||||||
return strconv.Itoa(v.Value)
|
return strconv.Itoa(v.Version)
|
||||||
} else {
|
} else {
|
||||||
return "(not set)"
|
return "(not set)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set is called when the user passes the flag as a command-line argument.
|
// Set is called when the user passes the flag as a command-line argument.
|
||||||
func (v *JSONSchemaVersion) Set(s string) error {
|
func (v *SchemaVersion) Set(s string) error {
|
||||||
if v.IsSet {
|
if v.IsSet {
|
||||||
return errors.New("received multiple instances of --json; only pass it once")
|
return errors.New("received multiple instances of --json; only pass it once")
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ func (v *JSONSchemaVersion) Set(s string) error {
|
|||||||
// This ensures that any existing scripts will continue to get their
|
// This ensures that any existing scripts will continue to get their
|
||||||
// current output.
|
// current output.
|
||||||
if s == "true" {
|
if s == "true" {
|
||||||
v.Value = 1
|
v.Version = 1
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,14 +71,14 @@ func (v *JSONSchemaVersion) Set(s string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid integer value passed to --json: %q", s)
|
return fmt.Errorf("invalid integer value passed to --json: %q", s)
|
||||||
}
|
}
|
||||||
v.Value = version
|
v.Version = version
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsBoolFlag reports that this [flag.Value] can be set without an argument.
|
// IsBoolFlag reports that this [flag.Value] can be set without an argument.
|
||||||
// This is the magic interface that makes -name equivalent to -name=true
|
// This is the magic interface that makes -name equivalent to -name=true
|
||||||
// rather than using the next command-line argument.
|
// rather than using the next command-line argument.
|
||||||
func (v *JSONSchemaVersion) IsBoolFlag() bool {
|
func (v *SchemaVersion) IsBoolFlag() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ func runTailnetLockInit(ctx context.Context, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var nlStatusArgs struct {
|
var nlStatusArgs struct {
|
||||||
json jsonoutput.JSONSchemaVersion
|
json jsonoutput.SchemaVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
var tlStatusCmd = &ffcli.Command{
|
var tlStatusCmd = &ffcli.Command{
|
||||||
@@ -221,10 +221,10 @@ func runTailnetLockStatus(ctx context.Context, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if nlStatusArgs.json.IsSet {
|
if nlStatusArgs.json.IsSet {
|
||||||
if nlStatusArgs.json.Value == 1 {
|
if nlStatusArgs.json.Version == 1 {
|
||||||
return jsonoutput.PrintNetworkLockStatusJSONV1(os.Stdout, st)
|
return jsonoutput.PrintNetworkLockStatusJSONV1(os.Stdout, st)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("unrecognised version: %d", nlStatusArgs.json.Value)
|
return fmt.Errorf("unrecognised version: %d", nlStatusArgs.json.Version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -604,7 +604,7 @@ func runTailnetLockDisablementKDF(ctx context.Context, args []string) error {
|
|||||||
|
|
||||||
var nlLogArgs struct {
|
var nlLogArgs struct {
|
||||||
limit int
|
limit int
|
||||||
json jsonoutput.JSONSchemaVersion
|
json jsonoutput.SchemaVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
var tlLogCmd = &ffcli.Command{
|
var tlLogCmd = &ffcli.Command{
|
||||||
@@ -712,12 +712,12 @@ func runTailnetLockLog(ctx context.Context, args []string) error {
|
|||||||
return printTailnetLockLog(updates, out, nlLogArgs.json, useColor)
|
return printTailnetLockLog(updates, out, nlLogArgs.json, useColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printTailnetLockLog(updates []ipnstate.NetworkLockUpdate, out io.Writer, jsonSchema jsonoutput.JSONSchemaVersion, useColor bool) error {
|
func printTailnetLockLog(updates []ipnstate.NetworkLockUpdate, out io.Writer, jsonSchema jsonoutput.SchemaVersion, useColor bool) error {
|
||||||
if jsonSchema.IsSet {
|
if jsonSchema.IsSet {
|
||||||
if jsonSchema.Value == 1 {
|
if jsonSchema.Version == 1 {
|
||||||
return jsonoutput.PrintNetworkLockLogJSONV1(out, updates)
|
return jsonoutput.PrintNetworkLockLogJSONV1(out, updates)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("unrecognised version: %d", jsonSchema.Value)
|
return fmt.Errorf("unrecognised version: %d", jsonSchema.Version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ func TestTailnetLockLogOutput(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
var outBuf bytes.Buffer
|
var outBuf bytes.Buffer
|
||||||
json := jsonoutput.JSONSchemaVersion{}
|
json := jsonoutput.SchemaVersion{}
|
||||||
useColor := false
|
useColor := false
|
||||||
|
|
||||||
printTailnetLockLog(updates, &outBuf, json, useColor)
|
printTailnetLockLog(updates, &outBuf, json, useColor)
|
||||||
@@ -189,9 +189,9 @@ KeyID: tlpub:0202
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
var outBuf bytes.Buffer
|
var outBuf bytes.Buffer
|
||||||
json := jsonoutput.JSONSchemaVersion{
|
json := jsonoutput.SchemaVersion{
|
||||||
IsSet: true,
|
IsSet: true,
|
||||||
Value: 1,
|
Version: 1,
|
||||||
}
|
}
|
||||||
useColor := false
|
useColor := false
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user