Skip to content
Open
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
14 changes: 13 additions & 1 deletion cmd/baton-sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
28 changes: 27 additions & 1 deletion pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down