...

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

Documentation: github.com/sigstore/timestamp-authority/cmd/timestamp-cli/app

     1  //
     2  // Copyright 2022 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  	"sigs.k8s.io/release-utils/version"
    27  
    28  	"github.com/sigstore/timestamp-authority/pkg/log"
    29  )
    30  
    31  var rootCmd = &cobra.Command{
    32  	Use:   "timestamp-cli",
    33  	Short: "Timestamp CLI",
    34  	Long:  `Timestamp command line interface tool`,
    35  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    36  		return initConfig(cmd)
    37  	},
    38  }
    39  
    40  // Execute runs the base CLI
    41  func Execute() {
    42  	if err := rootCmd.Execute(); err != nil {
    43  		log.CliLogger.Fatal(err)
    44  	}
    45  }
    46  
    47  func init() {
    48  	initializePFlagMap()
    49  	rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.timestamp.yaml)")
    50  
    51  	rootCmd.PersistentFlags().Var(NewFlagValue(urlFlag, "https://timestamp.sigstore.dev"), "timestamp_server", "Server host:port")
    52  	rootCmd.PersistentFlags().Var(NewFlagValue(formatFlag, "default"), "format", "Command output format")
    53  	rootCmd.PersistentFlags().Var(NewFlagValue(timeoutFlag, "30s"), "timeout", "HTTP timeout")
    54  
    55  	// these are bound here and not in PreRun so that all child commands can use them
    56  	if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
    57  		log.CliLogger.Fatal(err)
    58  	}
    59  
    60  	rootCmd.AddCommand(version.Version())
    61  }
    62  
    63  func initConfig(cmd *cobra.Command) error {
    64  
    65  	viper.SetEnvPrefix("timestamp")
    66  	viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
    67  	viper.AutomaticEnv()
    68  
    69  	// manually set all values provided from viper through pflag validation logic
    70  	var changedFlags []string
    71  	cmd.Flags().VisitAll(func(f *pflag.Flag) {
    72  		if !f.Changed && viper.IsSet(f.Name) {
    73  			changedFlags = append(changedFlags, f.Name)
    74  		}
    75  	})
    76  
    77  	for _, flag := range changedFlags {
    78  		val := viper.Get(flag)
    79  		if err := cmd.Flags().Set(flag, fmt.Sprintf("%v", val)); err != nil {
    80  			return err
    81  		}
    82  	}
    83  
    84  	if viper.GetString("config") != "" {
    85  		viper.SetConfigFile(viper.GetString("config"))
    86  	} else {
    87  		// Find home directory.
    88  		home, err := homedir.Dir()
    89  		if err != nil {
    90  			return err
    91  		}
    92  
    93  		viper.AddConfigPath(home)
    94  		viper.SetConfigName(".timestamp")
    95  	}
    96  
    97  	if err := viper.ReadInConfig(); err != nil {
    98  		switch err.(type) {
    99  		case viper.ConfigFileNotFoundError:
   100  		default:
   101  			return err
   102  		}
   103  	} else if viper.GetString("format") == "default" {
   104  		log.CliLogger.Infof("Using config file:", viper.ConfigFileUsed())
   105  	}
   106  
   107  	return nil
   108  }
   109  

View as plain text