go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/cli/flags.go (about)

     1  //  Copyright (c) 2020 Cisco and/or its affiliates.
     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 cli
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/sirupsen/logrus"
    22  	"github.com/spf13/pflag"
    23  	"github.com/spf13/viper"
    24  	"go.ligato.io/cn-infra/v2/logging"
    25  
    26  	"go.ligato.io/vpp-agent/v3/cmd/agentctl/client"
    27  )
    28  
    29  const (
    30  	defaultEtcdEndpoint = "127.0.0.1:2379"
    31  )
    32  
    33  // ClientOptions define options for the client.
    34  type ClientOptions struct {
    35  	Debug    bool
    36  	LogLevel string
    37  }
    38  
    39  // NewClientOptions returns a new ClientOptions
    40  func NewClientOptions() *ClientOptions {
    41  	return &ClientOptions{}
    42  }
    43  
    44  // InstallFlags adds flags for the common options on the FlagSet
    45  func (opts *ClientOptions) InstallFlags(flags *pflag.FlagSet) {
    46  	// TODO: consider using viper.AutomaticEnv with some prefix like `AGENTCTL`
    47  
    48  	flags.BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug mode")
    49  
    50  	flags.StringVarP(&opts.LogLevel, "log-level", "l", "", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
    51  
    52  	flags.StringP("host", "H", client.DefaultAgentHost, "Address on which agent is reachable, default from AGENT_HOST env var")
    53  	_ = viper.BindPFlag("host", flags.Lookup("host"))
    54  	_ = viper.BindEnv("host", "AGENT_HOST")
    55  
    56  	flags.String("service-label", "", "Service label for specific agent instance, default from MICROSERVICE_LABEL env var")
    57  	_ = viper.BindPFlag("service-label", flags.Lookup("service-label"))
    58  	_ = viper.BindEnv("service-label", "MICROSERVICE_LABEL")
    59  
    60  	flags.Int("http-port", client.DefaultPortHTTP, "HTTP server port")
    61  	_ = viper.BindPFlag("http-port", flags.Lookup("http-port"))
    62  
    63  	flags.Duration("timeout", client.DefaultTimeout, "Timeout for client requests")
    64  
    65  	flags.Int("grpc-port", client.DefaultPortGRPC, "gRPC server port")
    66  	_ = viper.BindPFlag("grpc-port", flags.Lookup("grpc-port"))
    67  
    68  	flags.StringSliceP("etcd-endpoints", "e", []string{defaultEtcdEndpoint}, "Etcd endpoints to connect to, default from ETCD_ENDPOINTS env var")
    69  	_ = viper.BindPFlag("etcd-endpoints", flags.Lookup("etcd-endpoints"))
    70  	_ = viper.BindEnv("etcd-endpoints", "ETCD_ENDPOINTS")
    71  
    72  	flags.String("http-basic-auth", "", "Basic auth for HTTP connection in form \"user:pass\"")
    73  	_ = viper.BindPFlag("http-basic-auth", flags.Lookup("http-basic-auth"))
    74  	_ = viper.BindEnv("http-basic-auth", "AGENTCTL_HTTP_BASIC_AUTH")
    75  
    76  	flags.Bool("insecure-tls", false, "Use TLS without server's certificate validation")
    77  	_ = viper.BindPFlag("insecure-tls", flags.Lookup("insecure-tls"))
    78  
    79  	flags.String("config-dir", "", "Path to directory with config file.")
    80  	_ = viper.BindPFlag("config-dir", flags.Lookup("config-dir"))
    81  	_ = viper.BindEnv("config-dir", "CONFIG_DIR")
    82  
    83  	flags.String("config", "", "Path to config file.")
    84  	_ = viper.BindPFlag("config", flags.Lookup("config"))
    85  
    86  	_ = viper.BindEnv("ligato-api-version", "LIGATO_API_VERSION")
    87  }
    88  
    89  // SetLogLevel sets the logrus logging level (WarnLevel for empty string).
    90  func SetLogLevel(logLevel string) {
    91  	if logLevel == "" {
    92  		logrus.SetLevel(logrus.WarnLevel)
    93  		logging.DefaultLogger.SetLevel(logging.WarnLevel)
    94  		return
    95  	}
    96  	lvl, err := logrus.ParseLevel(logLevel)
    97  	if err != nil {
    98  		fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
    99  		os.Exit(1)
   100  	}
   101  	logrus.SetLevel(lvl)
   102  	logging.DefaultLogger.SetLevel(logging.LogLevel(lvl))
   103  }