1// Copyright 2015 The etcd Authors 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 ctlv3 contains the main entry point for the etcdctl for v3 API. 16package ctlv3 17 18import ( 19 "time" 20 21 "go.etcd.io/etcd/etcdctl/v3/ctlv3/command" 22 23 "github.com/spf13/cobra" 24) 25 26const ( 27 cliName = "etcdctl" 28 cliDescription = "A simple command line client for etcd3." 29 30 defaultDialTimeout = 2 * time.Second 31 defaultCommandTimeOut = 5 * time.Second 32 defaultKeepAliveTime = 2 * time.Second 33 defaultKeepAliveTimeOut = 6 * time.Second 34) 35 36var ( 37 globalFlags = command.GlobalFlags{} 38) 39 40var ( 41 rootCmd = &cobra.Command{ 42 Use: cliName, 43 Short: cliDescription, 44 SuggestFor: []string{"etcdctl"}, 45 } 46) 47 48func init() { 49 rootCmd.PersistentFlags().StringSliceVar(&globalFlags.Endpoints, "endpoints", []string{"127.0.0.1:2379"}, "gRPC endpoints") 50 rootCmd.PersistentFlags().BoolVar(&globalFlags.Debug, "debug", false, "enable client-side debug logging") 51 52 rootCmd.PersistentFlags().StringVarP(&globalFlags.OutputFormat, "write-out", "w", "simple", "set the output format (fields, json, protobuf, simple, table)") 53 rootCmd.PersistentFlags().BoolVar(&globalFlags.IsHex, "hex", false, "print byte strings as hex encoded strings") 54 55 rootCmd.PersistentFlags().DurationVar(&globalFlags.DialTimeout, "dial-timeout", defaultDialTimeout, "dial timeout for client connections") 56 rootCmd.PersistentFlags().DurationVar(&globalFlags.CommandTimeOut, "command-timeout", defaultCommandTimeOut, "timeout for short running command (excluding dial timeout)") 57 rootCmd.PersistentFlags().DurationVar(&globalFlags.KeepAliveTime, "keepalive-time", defaultKeepAliveTime, "keepalive time for client connections") 58 rootCmd.PersistentFlags().DurationVar(&globalFlags.KeepAliveTimeout, "keepalive-timeout", defaultKeepAliveTimeOut, "keepalive timeout for client connections") 59 60 // TODO: secure by default when etcd enables secure gRPC by default. 61 rootCmd.PersistentFlags().BoolVar(&globalFlags.Insecure, "insecure-transport", true, "disable transport security for client connections") 62 rootCmd.PersistentFlags().BoolVar(&globalFlags.InsecureDiscovery, "insecure-discovery", true, "accept insecure SRV records describing cluster endpoints") 63 rootCmd.PersistentFlags().BoolVar(&globalFlags.InsecureSkipVerify, "insecure-skip-tls-verify", false, "skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)") 64 rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify secure client using this TLS certificate file") 65 rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify secure client using this TLS key file") 66 rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.TrustedCAFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle") 67 rootCmd.PersistentFlags().StringVar(&globalFlags.User, "user", "", "username[:password] for authentication (prompt if password is not supplied)") 68 rootCmd.PersistentFlags().StringVar(&globalFlags.Password, "password", "", "password for authentication (if this option is used, --user option shouldn't include password)") 69 rootCmd.PersistentFlags().StringVarP(&globalFlags.TLS.ServerName, "discovery-srv", "d", "", "domain name to query for SRV records describing cluster endpoints") 70 rootCmd.PersistentFlags().StringVarP(&globalFlags.DNSClusterServiceName, "discovery-srv-name", "", "", "service name to query when using DNS discovery") 71 72 rootCmd.AddCommand( 73 command.NewGetCommand(), 74 command.NewPutCommand(), 75 command.NewDelCommand(), 76 command.NewTxnCommand(), 77 command.NewCompactionCommand(), 78 command.NewAlarmCommand(), 79 command.NewDefragCommand(), 80 command.NewEndpointCommand(), 81 command.NewMoveLeaderCommand(), 82 command.NewWatchCommand(), 83 command.NewVersionCommand(), 84 command.NewLeaseCommand(), 85 command.NewMemberCommand(), 86 command.NewSnapshotCommand(), 87 command.NewMakeMirrorCommand(), 88 command.NewMigrateCommand(), 89 command.NewLockCommand(), 90 command.NewElectCommand(), 91 command.NewAuthCommand(), 92 command.NewUserCommand(), 93 command.NewRoleCommand(), 94 command.NewCheckCommand(), 95 ) 96} 97 98func Start() error { 99 rootCmd.SetUsageFunc(usageFunc) 100 // Make help just show the usage 101 rootCmd.SetHelpTemplate(`{{.UsageString}}`) 102 return rootCmd.Execute() 103} 104 105func MustStart() { 106 if err := Start(); err != nil { 107 command.ExitWithError(command.ExitError, err) 108 } 109} 110 111func init() { 112 cobra.EnablePrefixMatching = true 113} 114