go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv2/command/format.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package command
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/coreos/etcd/client"
    23  )
    24  
    25  // printResponseKey only supports to print key correctly.
    26  func printResponseKey(resp *client.Response, format string) {
    27  	// Format the result.
    28  	switch format {
    29  	case "simple":
    30  		if resp.Action != "delete" {
    31  			fmt.Println(resp.Node.Value)
    32  		} else {
    33  			fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
    34  		}
    35  	case "extended":
    36  		// Extended prints in a rfc2822 style format
    37  		fmt.Println("Key:", resp.Node.Key)
    38  		fmt.Println("Created-Index:", resp.Node.CreatedIndex)
    39  		fmt.Println("Modified-Index:", resp.Node.ModifiedIndex)
    40  
    41  		if resp.PrevNode != nil {
    42  			fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
    43  		}
    44  
    45  		fmt.Println("TTL:", resp.Node.TTL)
    46  		fmt.Println("Index:", resp.Index)
    47  		if resp.Action != "delete" {
    48  			fmt.Println("")
    49  			fmt.Println(resp.Node.Value)
    50  		}
    51  	case "json":
    52  		b, err := json.Marshal(resp)
    53  		if err != nil {
    54  			panic(err)
    55  		}
    56  		fmt.Println(string(b))
    57  	default:
    58  		fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
    59  	}
    60  }