github.com/GoogleCloudPlatform/testgrid@v0.0.174/config/print/main.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"encoding/json"
    23  	"flag"
    24  	"fmt"
    25  	"os"
    26  
    27  	"github.com/GoogleCloudPlatform/testgrid/config"
    28  	"github.com/GoogleCloudPlatform/testgrid/util/gcs"
    29  
    30  	"github.com/sirupsen/logrus"
    31  )
    32  
    33  type options struct {
    34  	configPath    string
    35  	creds         string
    36  	printFieldUse bool
    37  	compressed    bool
    38  }
    39  
    40  func gatherOptions() options {
    41  	var o options
    42  	if len(os.Args) > 1 {
    43  		o.configPath = os.Args[1]
    44  	}
    45  	flag.StringVar(&o.configPath, "path", o.configPath, "Local or cloud config to read")
    46  	flag.StringVar(&o.creds, "gcp-service-account", "", "/path/to/gcp/creds (use local creds if empty)")
    47  	flag.BoolVar(&o.printFieldUse, "print-field-use", false, "If True, print all config fields and # of uses of each")
    48  	flag.BoolVar(&o.compressed, "compressed", false, "Disables pretty-printing, removing all whitespace between fields")
    49  	flag.Parse()
    50  	return o
    51  }
    52  
    53  func main() {
    54  	log := logrus.WithField("component", "config-printer")
    55  	opt := gatherOptions()
    56  
    57  	ctx, cancel := context.WithCancel(context.Background())
    58  	defer cancel()
    59  	storageClient, err := gcs.ClientWithCreds(ctx, opt.creds)
    60  	if err != nil {
    61  		log.WithError(err).Info("Can't make cloud storage client; proceeding")
    62  	}
    63  
    64  	cfg, err := config.Read(ctx, opt.configPath, storageClient)
    65  	if err != nil {
    66  		logrus.WithError(err).WithField("path", opt.configPath).Fatal("Can't read from path")
    67  	}
    68  	b, err := json.Marshal(cfg)
    69  	if err != nil {
    70  		logrus.WithError(err).Fatal("Can't marshal JSON")
    71  	}
    72  
    73  	if opt.compressed {
    74  		fmt.Printf("%s\n", b)
    75  	} else {
    76  		var out bytes.Buffer
    77  		json.Indent(&out, b, "", "  ")
    78  		out.WriteTo(os.Stdout)
    79  	}
    80  
    81  	if opt.printFieldUse {
    82  		err = printFieldNames(b)
    83  		if err != nil {
    84  			logrus.WithError(err).Fatal("Error printing fields")
    85  		}
    86  	}
    87  }
    88  
    89  func printFieldNames(b []byte) error {
    90  	type Dashboard struct {
    91  		DashboardTabs []map[string]interface{} `json:"dashboard_tab"`
    92  	}
    93  	type Config struct {
    94  		TestGroups      []map[string]interface{} `json:"test_groups"`
    95  		Dashboards      []Dashboard              `json:"dashboards"`
    96  		DashboardGroups []map[string]interface{} `json:"dashboard_groups"`
    97  	}
    98  	var output Config
    99  	err := json.Unmarshal(b, &output)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	fmt.Printf("%d test groups, %d dashboards, %d dashboard groups\n", len(output.TestGroups), len(output.Dashboards), len(output.DashboardGroups))
   105  
   106  	testGroupFields := map[string]int64{}
   107  	for _, value := range output.TestGroups {
   108  		for k := range value {
   109  			testGroupFields[k]++
   110  		}
   111  	}
   112  	fmt.Printf("Test Groups (%d fields):\n", len(testGroupFields))
   113  	for k, v := range testGroupFields {
   114  		fmt.Printf("%s,%d\n", k, v)
   115  	}
   116  
   117  	tabFields := map[string]int64{}
   118  	for _, dashboard := range output.Dashboards {
   119  		for _, val := range dashboard.DashboardTabs {
   120  			for k := range val {
   121  				tabFields[k]++
   122  			}
   123  		}
   124  	}
   125  	fmt.Printf("Dashboard Tabs (%d fields):\n", len(tabFields))
   126  	for k, v := range tabFields {
   127  		fmt.Printf("%s,%d\n", k, v)
   128  	}
   129  	fmt.Printf("Test Groups (%d fields):\n", len(testGroupFields))
   130  	for k, v := range testGroupFields {
   131  		fmt.Printf("%s,%d\n", k, v)
   132  	}
   133  	return nil
   134  }