forked from microsoft/go-sqlcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
143 lines (119 loc) · 4.55 KB
/
root.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package main
import (
"runtime"
"github.com/microsoft/go-sqlcmd/cmd/modern/root"
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
"github.com/microsoft/go-sqlcmd/internal/config"
"github.com/microsoft/go-sqlcmd/internal/localizer"
)
// Root type implements the very top-level command for sqlcmd (which contains
// all the sub-commands, like install, query, config etc.
type Root struct {
cmdparser.Cmd
configFilename string
loggingLevel int
outputType string
}
// DefineCommand defines the top-level sqlcmd sub-commands.
// It sets the cli name, description, and subcommands, and adds global flags.
// It also provides usage examples for sqlcmd.
func (c *Root) DefineCommand(...cmdparser.CommandOptions) {
// Example usage steps
steps := []string{"sqlcmd create mssql --accept-eula --using https://aka.ms/AdventureWorksLT.bak"}
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
steps = append(steps, "sqlcmd open ads")
}
steps = append(steps, `sqlcmd query "SELECT @@version"`)
steps = append(steps, "sqlcmd delete")
examples := []cmdparser.ExampleOptions{
{Description: localizer.Sprintf("Install/Create, Query, Uninstall SQL Server"),
Steps: steps},
{Description: localizer.Sprintf("View configuration information and connection strings"),
Steps: []string{"sqlcmd config view", "sqlcmd config cs"}},
}
const feedbackUrl = "https://github.com/microsoft/go-sqlcmd/issues/new"
commandOptions := cmdparser.CommandOptions{
Use: "sqlcmd",
Short: localizer.Sprintf(`sqlcmd: Install/Create/Query SQL Server, Azure SQL, and Tools
Feedback:
%s`, feedbackUrl),
SubCommands: c.SubCommands(),
Examples: examples,
}
c.Cmd.DefineCommand(commandOptions)
c.addGlobalFlags()
}
// SubCommands returns a slice of subcommands for the Root command.
// The returned subcommands are Config, Install, query, and Uninstall.
func (c *Root) SubCommands() []cmdparser.Command {
dependencies := c.Dependencies()
subCommands := []cmdparser.Command{
cmdparser.New[*root.Config](dependencies),
cmdparser.New[*root.Install](dependencies),
cmdparser.New[*root.Query](dependencies),
cmdparser.New[*root.Start](dependencies),
cmdparser.New[*root.Stop](dependencies),
cmdparser.New[*root.Uninstall](dependencies),
}
// BUG(stuartpa): - Add Linux support
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
subCommands = append(subCommands, cmdparser.New[*root.Open](dependencies))
}
return subCommands
}
// Execute runs the application based on the command-line
// parameters the user has passed in.
func (c *Root) Execute() {
c.Cmd.Execute()
}
// IsValidSubCommand is TEMPORARY code, that will be removed when
// we enable the new cobra based CLI by default. It returns true if the
// command-line provided by the user indicates they want the new cobra
// based CLI, e.g. sqlcmd install, or sqlcmd query, or sqlcmd --help etc.
func (c *Root) IsValidSubCommand(command string) bool {
return c.IsSubCommand(command)
}
func (c *Root) addGlobalFlags() {
// BUG(stuartpa): - This is a temporary flag until we have migrated
// the kong impl to cobra. sqlcmd -? will show the kong help (all the back-compat
// flags), sqlcmd --? will show the kong "did you mean one of" help.
var unused bool
c.AddFlag(cmdparser.FlagOptions{
Bool: &unused,
Name: "?",
Shorthand: "?",
Usage: localizer.Sprintf("help for backwards compatibility flags (-S, -U, -E etc.)"),
})
// BUG(stuartpa): - This is a temporary flag until we have migrated
// the kong impl to cobra. The implementation of --version is coming from
// kong, but we need to add it to the list of flags so that it shows up in the --help
c.AddFlag(cmdparser.FlagOptions{
Bool: &unused,
Name: "version",
Usage: localizer.Sprintf("print version of sqlcmd"),
})
c.AddFlag(cmdparser.FlagOptions{
String: &c.configFilename,
DefaultString: config.DefaultFileName(),
Name: "sqlconfig",
Usage: localizer.Sprintf("configuration file"),
})
/* BUG(stuartpa): - At the moment this is a top level flag, but it doesn't
work with all sub-commands (e.g. query), so removing for now.
c.AddFlag(cmdparser.FlagOptions{
String: &c.outputType,
DefaultString: "json",
Name: "output",
Shorthand: "o",
Usage: "output type (yaml, json or xml)",
})
*/
c.AddFlag(cmdparser.FlagOptions{
Int: (*int)(&c.loggingLevel),
DefaultInt: 2,
Name: "verbosity",
Usage: localizer.Sprintf("log level, error=0, warn=1, info=2, debug=3, trace=4"),
})
}