Skip to content

Commit f1c0b5c

Browse files
authored
Merge pull request #165 from microsoft/stuartpa/modern-cli
Modern POSIX style CLI for SQLCMD
2 parents ec7b53a + 39e2a73 commit f1c0b5c

File tree

134 files changed

+7605
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+7605
-27
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Binaries for programs and plugins
2-
output
32
*.exe
43
*.exe~
54
*.dll

cmd/cmd.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
package cmd
5+
6+
import (
7+
"github.com/microsoft/go-sqlcmd/cmd/root"
8+
"github.com/microsoft/go-sqlcmd/internal"
9+
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
10+
"github.com/microsoft/go-sqlcmd/internal/config"
11+
"github.com/microsoft/go-sqlcmd/internal/output"
12+
)
13+
14+
var loggingLevel int
15+
var outputType string
16+
var configFilename string
17+
var rootCmd cmdparser.Command
18+
19+
// Initialize initializes the command-line interface. The func passed into
20+
// cmdparser.Initialize is called after the command-line from the user has been
21+
// parsed, so the helpers are initialized with the values from the command-line
22+
// like '-v 4' which sets the logging level to maximum etc.
23+
func Initialize() {
24+
cmdparser.Initialize(initialize)
25+
rootCmd = cmdparser.New[*Root](root.SubCommands()...)
26+
}
27+
28+
func initialize() {
29+
options := internal.InitializeOptions{
30+
ErrorHandler: checkErr,
31+
HintHandler: displayHints,
32+
OutputType: "yaml",
33+
LoggingLevel: 2,
34+
}
35+
36+
config.SetFileName(configFilename)
37+
config.Load()
38+
internal.Initialize(options)
39+
}
40+
41+
// Execute runs the application based on the command-line
42+
// parameters the user has passed in.
43+
func Execute() {
44+
rootCmd.Execute()
45+
}
46+
47+
// IsValidSubCommand is TEMPORARY code, that will be removed when
48+
// we enable the new cobra based CLI by default. It returns true if the
49+
// command-line provided by the user indicates they want the new cobra
50+
// based CLI, e.g. sqlcmd install, or sqlcmd query, or sqlcmd --help etc.
51+
func IsValidSubCommand(command string) bool {
52+
return rootCmd.IsSubCommand(command)
53+
}
54+
55+
// checkErr uses Cobra to check err, and halts the application if err is not
56+
// nil. Pass (inject) checkErr into all dependencies (helpers etc.) as an
57+
// errorHandler.
58+
//
59+
// To aid debugging issues, if the logging level is > 2 (e.g. -v 3 or -4), we
60+
// panic which outputs a stacktrace.
61+
func checkErr(err error) {
62+
if loggingLevel > 2 {
63+
if err != nil {
64+
panic(err)
65+
}
66+
}
67+
rootCmd.CheckErr(err)
68+
}
69+
70+
// displayHints displays helpful information on what the user should do next
71+
// to make progress. displayHints is injected into dependencies (helpers etc.)
72+
func displayHints(hints []string) {
73+
if len(hints) > 0 {
74+
output.Infof("\nHINT:")
75+
for i, hint := range hints {
76+
output.Infof(" %d. %v", i+1, hint)
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)