-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
main.go
108 lines (87 loc) · 2.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/spf13/pflag"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtctl/vtctlclient"
logutilpb "vitess.io/vitess/go/vt/proto/logutil"
// Include deprecation warnings for soon-to-be-unsupported flag invocations.
)
// The default values used by these flags cannot be taken from wrangler and
// actionnode modules, as we don't want to depend on them at all.
var (
actionTimeout = time.Hour
server string
)
func init() {
servenv.OnParse(func(fs *pflag.FlagSet) {
fs.DurationVar(&actionTimeout, "action_timeout", actionTimeout, "timeout for the total command")
fs.StringVar(&server, "server", server, "server to use for connection")
acl.RegisterFlags(fs)
})
}
// checkDeprecations runs quick and dirty checks to see whether any command or flag are deprecated.
// For any depracated command or flag, the function issues a warning message.
// this function will change on each Vitess version. Each depracation message should only last a version.
// VEP-4 will replace the need for this function. See https://github.com/vitessio/enhancements/blob/main/veps/vep-4.md
func checkDeprecations(args []string) {
// utility:
// name this to findSubstring if you need to use it
_ = func(s string) (arg string, ok bool) {
for _, arg := range args {
if strings.Contains(arg, s) {
return arg, true
}
}
return "", false
}
}
func main() {
defer exit.Recover()
args := servenv.ParseFlagsWithArgs("vtctlclient")
closer := trace.StartTracing("vtctlclient")
defer trace.LogErrorsWhenClosing(closer)
logger := logutil.NewConsoleLogger()
// We can't do much without a --server flag
if server == "" {
log.Error(errors.New("please specify --server <vtctld_host:vtctld_port> to specify the vtctld server to connect to"))
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), actionTimeout)
defer cancel()
checkDeprecations(args)
err := vtctlclient.RunCommandAndWait(ctx, server, args, func(e *logutilpb.Event) {
logutil.LogEvent(logger, e)
})
if err != nil {
if strings.Contains(err.Error(), "flag: help requested") {
return
}
errStr := strings.Replace(err.Error(), "remote error: ", "", -1)
fmt.Printf("%s Error: %s\n", args[0], errStr)
log.Error(err)
os.Exit(1)
}
}