-
Notifications
You must be signed in to change notification settings - Fork 74
Modern POSIX style CLI for SQLCMD #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Binaries for programs and plugins | ||
output | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/microsoft/go-sqlcmd/cmd/root" | ||
"github.com/microsoft/go-sqlcmd/internal" | ||
"github.com/microsoft/go-sqlcmd/internal/cmdparser" | ||
"github.com/microsoft/go-sqlcmd/internal/config" | ||
"github.com/microsoft/go-sqlcmd/internal/output" | ||
) | ||
|
||
var loggingLevel int | ||
var outputType string | ||
var configFilename string | ||
var rootCmd cmdparser.Command | ||
|
||
// Initialize initializes the command-line interface. The func passed into | ||
// cmdparser.Initialize is called after the command-line from the user has been | ||
// parsed, so the helpers are initialized with the values from the command-line | ||
// like '-v 4' which sets the logging level to maximum etc. | ||
func Initialize() { | ||
cmdparser.Initialize(initialize) | ||
rootCmd = cmdparser.New[*Root](root.SubCommands()...) | ||
} | ||
|
||
func initialize() { | ||
options := internal.InitializeOptions{ | ||
ErrorHandler: checkErr, | ||
HintHandler: displayHints, | ||
OutputType: "yaml", | ||
LoggingLevel: 2, | ||
} | ||
|
||
config.SetFileName(configFilename) | ||
config.Load() | ||
internal.Initialize(options) | ||
} | ||
|
||
// Execute runs the application based on the command-line | ||
// parameters the user has passed in. | ||
func Execute() { | ||
rootCmd.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 IsValidSubCommand(command string) bool { | ||
return rootCmd.IsSubCommand(command) | ||
} | ||
|
||
// checkErr uses Cobra to check err, and halts the application if err is not | ||
// nil. Pass (inject) checkErr into all dependencies (helpers etc.) as an | ||
// errorHandler. | ||
// | ||
// To aid debugging issues, if the logging level is > 2 (e.g. -v 3 or -4), we | ||
// panic which outputs a stacktrace. | ||
func checkErr(err error) { | ||
if loggingLevel > 2 { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
rootCmd.CheckErr(err) | ||
} | ||
|
||
// displayHints displays helpful information on what the user should do next | ||
// to make progress. displayHints is injected into dependencies (helpers etc.) | ||
func displayHints(hints []string) { | ||
if len(hints) > 0 { | ||
output.Infof("\nHINT:") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I don't know if all the output renderers on Windows do the right thing. Should i replace this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've replaced all to sqlcmd.SqlcmdEol for next PR (in sheuybubble/bcp) (which I'll then need to work out how to inject, rather than having everything take an import on sqlcmd package) |
||
for i, hint := range hints { | ||
output.Infof(" %d. %v", i+1, hint) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configFilename isn't assigned any value. Is it supposed to be nul for the default with an option for the user to pass it along as a command line parameter or environment variable? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's set the default value for the --sqlconfig flag, so it's ~.sqlcmd\sqlconfig (this func is called by the cobra framework after the flag defaults have been set