This patch adds examples for unmarshalling the JSON outputs of the following commands: tailscale dns query --json tailscale dns status --json It also adds an example usage of `tailscale dns` to both jsonoutput.DNSQueryResult and jsonoutput.DNSStatusResult. Updates #13326 Updates #18750 Signed-off-by: Simon Law <sfllaw@tailscale.com>
32 lines
683 B
Go
32 lines
683 B
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package jsonoutput_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"tailscale.com/cmd/tailscale/cli/jsonoutput"
|
|
)
|
|
|
|
func ExampleDNSQueryResult() {
|
|
cmd := exec.Command("tailscale", "dns", "query", "--json", "hello.ts.net")
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
if err, ok := errors.AsType[*exec.ExitError](err); ok {
|
|
fmt.Fprintf(os.Stderr, "%s", err.Stderr)
|
|
}
|
|
panic(err)
|
|
}
|
|
|
|
var dnsQuery jsonoutput.DNSQueryResult
|
|
if err := json.Unmarshal(out, &dnsQuery); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("{type: %s, name: %q}\n", dnsQuery.QueryType, dnsQuery.Name)
|
|
}
|