...

Source file src/github.com/sigstore/rekor/cmd/rekor-cli/app/root.go

Documentation: github.com/sigstore/rekor/cmd/rekor-cli/app

     1  //
     2  // Copyright 2021 The Sigstore 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  package app
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  
    22  	homedir "github.com/mitchellh/go-homedir"
    23  	"github.com/spf13/cobra"
    24  	"github.com/spf13/pflag"
    25  	"github.com/spf13/viper"
    26  
    27  	"github.com/sigstore/rekor/pkg/client"
    28  	"github.com/sigstore/rekor/pkg/log"
    29  
    30  	// these imports are to call the packages' init methods
    31  	_ "github.com/sigstore/rekor/pkg/types/alpine/v0.0.1"
    32  	_ "github.com/sigstore/rekor/pkg/types/cose/v0.0.1"
    33  	_ "github.com/sigstore/rekor/pkg/types/dsse/v0.0.1"
    34  	_ "github.com/sigstore/rekor/pkg/types/hashedrekord/v0.0.1"
    35  	_ "github.com/sigstore/rekor/pkg/types/helm/v0.0.1"
    36  	_ "github.com/sigstore/rekor/pkg/types/intoto/v0.0.1"
    37  	_ "github.com/sigstore/rekor/pkg/types/intoto/v0.0.2"
    38  	_ "github.com/sigstore/rekor/pkg/types/jar/v0.0.1"
    39  	_ "github.com/sigstore/rekor/pkg/types/rekord/v0.0.1"
    40  	_ "github.com/sigstore/rekor/pkg/types/rfc3161/v0.0.1"
    41  	_ "github.com/sigstore/rekor/pkg/types/rpm/v0.0.1"
    42  	_ "github.com/sigstore/rekor/pkg/types/tuf/v0.0.1"
    43  )
    44  
    45  var rootCmd = &cobra.Command{
    46  	Use:   "rekor-cli",
    47  	Short: "Rekor CLI",
    48  	Long:  `Rekor command line interface tool`,
    49  	PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
    50  		return initConfig(cmd)
    51  	},
    52  }
    53  
    54  // Execute runs the base CLI
    55  func Execute() {
    56  	if err := rootCmd.Execute(); err != nil {
    57  		log.CliLogger.Fatal(err)
    58  	}
    59  }
    60  
    61  func init() {
    62  	initializePFlagMap()
    63  	rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.rekor.yaml)")
    64  	rootCmd.PersistentFlags().Bool("store_tree_state", true, "whether to store tree state in between invocations for additional verification")
    65  
    66  	rootCmd.PersistentFlags().Var(NewFlagValue(urlFlag, "https://rekor.sigstore.dev"), "rekor_server", "Server address:port")
    67  	rootCmd.PersistentFlags().Var(NewFlagValue(formatFlag, "default"), "format", "Command output format")
    68  	rootCmd.PersistentFlags().Var(NewFlagValue(timeoutFlag, "30s"), "timeout", "HTTP timeout")
    69  	rootCmd.PersistentFlags().Var(NewFlagValue(uintFlag, fmt.Sprintf("%d", client.DefaultRetryCount)), "retry", "Number of times to retry HTTP requests")
    70  
    71  	// these are bound here and not in PreRun so that all child commands can use them
    72  	if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
    73  		log.CliLogger.Fatal(err)
    74  	}
    75  }
    76  
    77  func initConfig(cmd *cobra.Command) error {
    78  
    79  	viper.SetEnvPrefix("rekor")
    80  	viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
    81  	viper.AutomaticEnv()
    82  
    83  	// manually set all values provided from viper through pflag validation logic
    84  	var changedFlags []string
    85  	cmd.Flags().VisitAll(func(f *pflag.Flag) {
    86  		if !f.Changed && viper.IsSet(f.Name) {
    87  			changedFlags = append(changedFlags, f.Name)
    88  		}
    89  	})
    90  
    91  	for _, flag := range changedFlags {
    92  		val := viper.Get(flag)
    93  		if err := cmd.Flags().Set(flag, fmt.Sprintf("%v", val)); err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	if viper.GetString("config") != "" {
    99  		viper.SetConfigFile(viper.GetString("config"))
   100  	} else {
   101  		// Find home directory.
   102  		home, err := homedir.Dir()
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		viper.AddConfigPath(home)
   108  		viper.SetConfigName(".rekor")
   109  	}
   110  
   111  	if err := viper.ReadInConfig(); err != nil {
   112  		switch err.(type) {
   113  		case viper.ConfigFileNotFoundError:
   114  		default:
   115  			return err
   116  		}
   117  	} else if viper.GetString("format") == "default" {
   118  		log.CliLogger.Infof("Using config file:", viper.ConfigFileUsed())
   119  	}
   120  
   121  	return nil
   122  }
   123  

View as plain text