Skip to content

Commit

Permalink
Short circuit if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
rmweir committed Sep 3, 2024
1 parent d00b612 commit 03a8ed2
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions pkg/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ func getKineStorageBackend(ctx context.Context, driver, dsn string, cfg Config)

// ParseStorageEndpoint returns the driver name and endpoint string from a datastore endpoint URL.
func ParseStorageEndpoint(storageEndpoint string) (string, string, error) {
if err := validateStorageEndpoint(storageEndpoint); err != nil {
if storageEndpoint == "" {
return SQLiteBackend, "", nil
}
network, address, err := networkAndAddress(storageEndpoint)
if err != nil {
return "", "", err
}

network, address := networkAndAddress(storageEndpoint)
switch network {
case "":
return SQLiteBackend, "", nil
case "nats":
return NATSBackend, storageEndpoint, nil
case "http":
Expand All @@ -258,23 +258,10 @@ func ParseStorageEndpoint(storageEndpoint string) (string, string, error) {

// networkAndAddress crudely splits a URL string into network (scheme) and address,
// where the address includes everything after the scheme/authority separator.
func networkAndAddress(str string) (string, string) {
func networkAndAddress(str string) (string, string, error) {
parts := strings.SplitN(str, "://", 2)
if len(parts) > 1 {
return parts[0], parts[1]
}
return "", parts[0]
}

// validateStorageEndpoint validates the given string can be used to infer backend and optionally extract connection
// info
func validateStorageEndpoint(str string) error {
if strings.Contains(str, "://") {
return nil
}
if str == "" || str == "sqlite" {
return nil
return parts[0], parts[1], nil
}
// without this check kine will silently run embedded sqlite, despite user intending to use a passed backend
return fmt.Errorf("invalid storage endpoint [%s]. Non-empty storage endpoint should be of the format <network>://<address>", str)
return "", "", errors.New("invalid datastore endpoint; endpoint should be a DSN URI in the format <scheme>://<authority>")
}

0 comments on commit 03a8ed2

Please sign in to comment.