Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Binaries for programs and plugins
output
*.exe
*.exe~
*.dll
Expand Down
79 changes: 79 additions & 0 deletions cmd/cmd.go
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)
Copy link
Collaborator

@shueybubbles shueybubbles Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configFilename

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

Copy link
Collaborator Author

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

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:")
Copy link
Collaborator

@shueybubbles shueybubbles Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n

are we going to use \n on all platforms? Do all the output renderers on Windows do the right thing with it? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 SqlcmdEol to be safe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)
}
}
}
Loading