diff --git a/cmd/baton-sql/main.go b/cmd/baton-sql/main.go index 5fa963d5..fae8ece0 100644 --- a/cmd/baton-sql/main.go +++ b/cmd/baton-sql/main.go @@ -47,7 +47,19 @@ func main() { func getConnector(ctx context.Context, v *viper.Viper) (types.ConnectorServer, error) { l := ctxzap.Extract(ctx) - cb, err := connector.New(ctx, v.GetString("config-path")) + var opts []connector.NewOption + + // Apply app-name override if provided + if appName := v.GetString("app-name"); appName != "" { + opts = append(opts, connector.WithAppName(appName)) + } + + // Apply app-description override if provided + if appDescription := v.GetString("app-description"); appDescription != "" { + opts = append(opts, connector.WithAppDescription(appDescription)) + } + + cb, err := connector.New(ctx, v.GetString("config-path"), opts...) if err != nil { l.Error("error creating connector", zap.Error(err)) return nil, err diff --git a/pkg/config/schema.go b/pkg/config/schema.go index 514bb709..2fa49a97 100644 --- a/pkg/config/schema.go +++ b/pkg/config/schema.go @@ -9,9 +9,23 @@ var ( field.WithDescription("The file path to the baton-sql config to use"), ) + AppNameField = field.StringField( + "app-name", + field.WithRequired(false), + field.WithDescription("Override the app_name from the config file"), + ) + + AppDescriptionField = field.StringField( + "app-description", + field.WithRequired(false), + field.WithDescription("Override the app_description from the config file"), + ) + // ConfigurationFields defines the external configuration required for the connector to run. ConfigurationFields = []field.SchemaField{ ConfigPathField, + AppNameField, + AppDescriptionField, } ConfigurationSchema = field.NewConfiguration(ConfigurationFields) ) diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index f078a09a..25098e4d 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -84,15 +84,41 @@ func (c *Connector) Validate(ctx context.Context) (annotations.Annotations, erro } // New returns a new instance of the connector. -func New(ctx context.Context, configFilePath string) (*Connector, error) { +func New(ctx context.Context, configFilePath string, opts ...NewOption) (*Connector, error) { c, err := bsql.LoadConfigFromFile(configFilePath) if err != nil { return nil, err } + // Apply options to override config values + for _, opt := range opts { + opt(c) + } + return newConnector(ctx, c) } +// NewOption is a function that modifies the config. +type NewOption func(*bsql.Config) + +// WithAppName sets the app name, overriding the config file value. +func WithAppName(name string) NewOption { + return func(c *bsql.Config) { + if name != "" { + c.AppName = name + } + } +} + +// WithAppDescription sets the app description, overriding the config file value. +func WithAppDescription(description string) NewOption { + return func(c *bsql.Config) { + if description != "" { + c.AppDescription = description + } + } +} + func newConnector(ctx context.Context, c *bsql.Config) (*Connector, error) { opts := database.ConnectOptions{ DSN: c.Connect.DSN,