Documentation
¶
Index ¶
- func AddContext(context Context) string
- func AddContextWithContainer(contextName string, imageName string, portNumber int, containerId string, ...)
- func AddEndpoint(endpoint Endpoint) (actualEndpointName string)
- func AddUser(user User) (actualUserName string)
- func Clean()
- func ContainerId() (containerId string)
- func ContextExists(name string) (exists bool)
- func CurrentContext() (endpoint Endpoint, user *User)
- func CurrentContextEndpointHasContainer() (exists bool)
- func CurrentContextName() string
- func DefaultFileName() (filename string)
- func DeleteContext(name string)
- func DeleteEndpoint(name string)
- func DeleteUser(name string)
- func EndpointExists(name string) (exists bool)
- func EndpointNameExists(name string) (exists bool)
- func EndpointsExists() (exists bool)
- func FindFreePortForTds() (portNumber int)
- func FindUniqueContextName(name string, username string) (uniqueContextName string)
- func FindUniqueEndpointName(name string) (uniqueEndpointName string)
- func FindUniqueUserName(name string) (uniqueUserName string)
- func GetConfigFileUsed() string
- func GetContext(name string) (context Context)
- func GetCurrentContextInfo() (server string, username string, password string)
- func GetCurrentContextOrFatal() (currentContextName string)
- func GetEndpoint(name string) (endpoint Endpoint)
- func GetUser(name string) (user User)
- func Initialize(errorHandler func(err error), traceHandler func(format string, a ...any), ...)
- func IsEmpty() (isEmpty bool)
- func Load()
- func OutputContexts(formatter func(interface{}) []byte, detailed bool)
- func OutputEndpoints(formatter func(interface{}) []byte, detailed bool)
- func OutputUsers(formatter func(interface{}) []byte, detailed bool)
- func RedactedConfig(raw bool) (c Sqlconfig)
- func RemoveCurrentContext()
- func Save()
- func SetCurrentContextName(name string)
- func SetFileName(name string)
- func SetFileNameForTest(t *testing.T)
- func UserExists(context Context) bool
- func UserNameExists(name string) (exists bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddContext ¶
func AddContext(context Context) string
AddContext adds the context to the sqlconfig file, and returns the context name, which maybe uniquified, if the passed in name already exists.
Before calling this method, verify the Endpoint exists and give the user a descriptive error, (this function will panic, which should never be hit)
func AddContextWithContainer ¶
func AddContextWithContainer( contextName string, imageName string, portNumber int, containerId string, username string, password string, passwordEncryption string, )
AddContextWithContainer adds a new context to the application's configuration with the given parameters. The context is associated with a container identified by its container ID. If any of the required parameters (i.e. containerId, imageName, portNumber, username, password, contextName) are empty or zero-valued, the function will panic. The function also ensures that the given contextName and username are unique, and it encrypts the password if requested. The updated configuration is saved to file.
func AddEndpoint ¶
func AddEndpoint(endpoint Endpoint) (actualEndpointName string)
AddEndpoint adds a new endpoint to the application's configuration with the given parameters. If the provided endpoint name is not unique, the function will modify it to ensure that it is unique before adding it to the configuration. The updated configuration is saved to file, and the function returns the actual endpoint name that was added. This may be different from the provided name if the original name was not unique.
func AddUser ¶
func AddUser(user User) (actualUserName string)
AddUser adds a new user to the configuration. The user's name is first modified to be unique by calling the FindUniqueUserName function. If the user's authentication type is "basic", the user's BasicAuth field must be non-nil and the username must be non-empty. The new user is then added to the list of users in the configuration object and the configuration is saved to the file. the function returns the actual username that was added. This may be different from the provided name if the original name was not unique.
func Clean ¶
func Clean()
Clean resets the application's configuration by setting the Users, Contexts, and Endpoints fields to nil, the CurrentContext field to an empty string, and saving the updated configuration. This effectively resets the configuration to its initial state.
func ContainerId ¶
func ContainerId() (containerId string)
This function gets the container ID of the current context's endpoint. It first checks if the current context exists and has an endpoint. Then it checks if the endpoint has a container and retrieves its ID. Otherwise, it returns the container ID.
func ContextExists ¶
ContextExists returns whether a context with the given name exists in the configuration. This function iterates over the list of contexts in the configuration and returns true if a context with the given name is found. Otherwise, the function returns false.
func CurrentContext ¶
func CurrentContext() (endpoint Endpoint, user *User)
CurrentContext returns the current context's endpoint and user from the configuration. The function iterates over the list of contexts and endpoints in the configuration and returns the endpoint and user for the current context. If the current context does not have an endpoint, the function panics.
func CurrentContextEndpointHasContainer ¶
func CurrentContextEndpointHasContainer() (exists bool)
CurrentContextEndpointHasContainer() checks if the current context endpoint has a container. If the endpoint has a AssetDetails.ContainerDetails field, the function returns true, otherwise it returns false.
func CurrentContextName ¶
func CurrentContextName() string
CurrentContextName returns the name of the current context in the configuration. The current context is the one that is currently active and used by the application.
func DefaultFileName ¶
func DefaultFileName() (filename string)
DefaultFileName returns the default filename for the file that the application reads from and writes to. This is typically located in the user's home directory under the ".sqlcmd" directory. If an error occurs while attempting to retrieve the user's home directory, the function will return an empty string.
func DeleteContext ¶
func DeleteContext(name string)
DeleteContext removes the context with the given name from the application's configuration. If the context does not exist, the function does nothing. The function also updates the CurrentContext field in the configuration to the first remaining context, or an empty string if no contexts remain. The updated configuration is saved to file.
func DeleteEndpoint ¶
func DeleteEndpoint(name string)
DeleteEndpoint removes the endpoint with the given name from the application's configuration. If the endpoint does not exist, the function does nothing. The updated configuration is saved to file.
func DeleteUser ¶
func DeleteUser(name string)
DeleteUser removes a user from the configuration by their name. If the user does not exist, the function does nothing. Otherwise, the user is removed from the list of users in the configuration object and the configuration is saved to the file.
func EndpointExists ¶
EndpointExists returns whether an endpoint with the given name exists in the configuration. This function iterates over the list of endpoints in the configuration and returns true if an endpoint with the given name is found. Otherwise, the function returns false.
func EndpointNameExists ¶
EndpointNameExists returns whether an endpoint with the given name exists in the configuration. This function iterates over the list of endpoints in the configuration and returns true if an endpoint with the given name is found. Otherwise, the function returns false.
func EndpointsExists ¶
func EndpointsExists() (exists bool)
EndpointsExists returns whether there are any endpoints in the configuration. This function checks the length of the Endpoints field in the configuration object and returns true if it is greater than zero. Otherwise, the function returns false.
func FindFreePortForTds ¶
func FindFreePortForTds() (portNumber int)
FindFreePortForTds is used to find a free port number to use for the TDS protocol. It starts at port number 1433 and continues until it finds a port number that is not currently in use by any of the endpoints in the configuration. It also checks that the port is available on the local machine. If no available port is found after trying up to port number 5000, the function panics.
func FindUniqueContextName ¶
FindUniqueContextName finds a unique context name, that is both a unique context name, but also a unique sa@context name. If the name passed in is unique then this is returned, else we look for the name with a numeral postfix, starting at 2
func FindUniqueEndpointName ¶
FindUniqueEndpointName returns a unique name for an endpoint with the given name. If an endpoint with the given name does not exist in the configuration, the function returns the given name. Otherwise, the function returns a modified version of the given name that includes a number at the end to make it unique.
func FindUniqueUserName ¶
FindUniqueUserName generates a unique user name based on the given name. If the given name is not already in use, it is returned as-is. Otherwise, a number is appended to the end of the given name to make it unique. This number starts at 2 and is incremented until a unique user name is found.
func GetConfigFileUsed ¶
func GetConfigFileUsed() string
GetConfigFileUsed returns the path to the configuration file used by the Viper library.
func GetContext ¶
func GetContext(name string) (context Context)
GetContext retrieves a context from the configuration by its name. If the context does not exist, the function panics. If the context is not found, the function panics to indicate that the context must exist.
func GetCurrentContextInfo ¶ added in v0.15.0
GetCurrentContextInfo returns endpoint and basic auth info associated with current context
func GetCurrentContextOrFatal ¶
func GetCurrentContextOrFatal() (currentContextName string)
GetCurrentContextOrFatal returns the name of the current context in the configuration or panics if it is not set. This function first calls the CurrentContextName function to retrieve the current context's name, if the current context's name is empty, the function panics with an error message indicating that a context must be set. Otherwise, the current context's name is returned.
func GetEndpoint ¶
func GetEndpoint(name string) (endpoint Endpoint)
GetEndpoint returns the endpoint with the given name from the configuration.
func GetUser ¶
func GetUser(name string) (user User)
GetUser retrieves a user from the configuration by their name.
func Initialize ¶
func Initialize( errorHandler func(err error), traceHandler func(format string, a ...any), encryptHandler func(plainText string, encryptionMethod string) (cipherText string), decryptHandler func(cipherText string, encryptionMethod string) (secret string), isLocalPortAvailableHandler func(port int) (portAvailable bool), )
Initialize sets the callback functions used by the config package. These callback functions are used for logging errors, tracing debug messages, encrypting and decrypting data, and checking if a local port is available. The callback functions are passed to the function as arguments. This function should be called at the start of the application to ensure that the config package has the necessary callback functions available.
func IsEmpty ¶
func IsEmpty() (isEmpty bool)
IsEmpty returns a boolean indicating whether the application's configuration is empty. The configuration is considered empty if all of the following fields are empty or zero-valued: Users, Contexts, Endpoints, and CurrentContext. This function can be used to determine whether the configuration has been initialized or reset.
func Load ¶
func Load()
Load loads the configuration from the file specified by the SetFileName() function. Any errors encountered while marshalling or saving the configuration are checked and handled by the injected errorHandler (via the checkErr function).
func OutputContexts ¶
OutputContexts outputs the list of contexts in the configuration. The output can be either detailed, which includes all information about each context, or a list of context names only. This is controlled by the detailed flag, which is passed to the function.
func OutputEndpoints ¶
OutputEndpoints outputs the list of endpoints in the configuration in a specified format. This function takes a formatter function and a flag indicating whether to output detailed information or just the names of the endpoints. If detailed information is requested, the formatter function is called with the list of endpoints in the configuration as the argument. Otherwise, the formatter function is called with a list of just the names of the endpoints in the configuration.
func OutputUsers ¶
OutputUsers outputs the list of users in the configuration. The output can be either detailed, which includes all information about each user, or a list of user names only. This is controlled by the detailed flag, which is passed to the function.
func RedactedConfig ¶
func RedactedConfig(raw bool) (c Sqlconfig)
RedactedConfig function returns a Sqlconfig struct with the Users field having their BasicAuth password field either replaced with the decrypted password or the string "REDACTED", depending on the value of the raw parameter. This allows the caller to either get the full password or a redacted version, where the password is hidden.
func RemoveCurrentContext ¶
func RemoveCurrentContext()
RemoveCurrentContext removes the current context from the configuration. This function iterates over the list of contexts, endpoints, and users in the configuration and removes the current context, its endpoint, and its user. If there are no remaining contexts in the configuration after removing the current context, the CurrentContext field in the configuration object is set to an empty string. Otherwise, the CurrentContext field is set to the name of the first remaining context.
func Save ¶
func Save()
Save marshals the current configuration object and saves it to the configuration file previously specified by the SetFileName variable. Any errors encountered while marshalling or saving the configuration are checked and handled by the injected errorHandler (via the checkErr function).
func SetCurrentContextName ¶
func SetCurrentContextName(name string)
SetCurrentContextName sets the current context in the configuration to the given name. If a context with the given name does not exist, the function panics. Otherwise, the CurrentContext field in the configuration object is updated with the given name and the configuration is saved to the file.
func SetFileName ¶
func SetFileName(name string)
SetFileName sets the filename for the file that the application reads from and writes to. The file is created if it does not already exist, and Viper is configured to use the given filename.
func SetFileNameForTest ¶
func UserExists ¶ added in v0.15.0
func UserExists(context Context) bool
UserExists checks if the current context has a 'user', e.g. a context used for trusted authentication will not have a user.
func UserNameExists ¶
UserNameExists checks if a user with the given name exists in the configuration. It iterates over the list of users in the configuration object and returns true if a user with the given name is found. Otherwise, it returns false. This function can be useful for checking if a user with a given name already exists before adding a new user or updating an existing user.
Types ¶
This section is empty.