github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/testsummary/cmd/internal/print_commands.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/spf13/cobra"
    11  
    12  	ts "github.com/smartcontractkit/chainlink-testing-framework/libs/testsummary"
    13  )
    14  
    15  var PrintKeyCmd = &cobra.Command{
    16  	Use:   "print-key [key]",
    17  	Short: "Prints all values for the given key from test summary file",
    18  	RunE:  printKeyRunE,
    19  }
    20  
    21  func init() {
    22  	PrintKeyCmd.Flags().Bool("json", true, "print as json")
    23  	PrintKeyCmd.Flags().Bool("md", true, "print as mardkown")
    24  }
    25  
    26  func printKeyRunE(cmd *cobra.Command, args []string) error {
    27  	if len(args) != 1 || args[0] == "" {
    28  		return cmd.Help()
    29  	}
    30  
    31  	key := strings.ToLower(args[0])
    32  
    33  	f, err := os.OpenFile(ts.SUMMARY_FILE, os.O_RDONLY, 0444)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	defer f.Close()
    38  
    39  	fc, err := io.ReadAll(f)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	var sk ts.SummaryKeys
    45  	err = json.Unmarshal(fc, &sk)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	if entry, ok := sk[key]; ok {
    51  		if cmd.Flag("json").Value.String() == "true" {
    52  			fmt.Println(prettyPrint(entry))
    53  		} else if cmd.Flag("md").Value.String() == "true" {
    54  			panic("not implemented")
    55  		} else {
    56  			fmt.Printf("%+v\n", entry)
    57  		}
    58  		return nil
    59  	}
    60  
    61  	return fmt.Errorf("no entry for key '%s' found", args[0])
    62  }
    63  
    64  func prettyPrint(i interface{}) string {
    65  	s, _ := json.MarshalIndent(i, "", "\t")
    66  	return string(s)
    67  }