Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Defaults for Laravel Scanner: DB, Redis #3090

Merged
merged 14 commits into from
Dec 18, 2023
Merged
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
72 changes: 70 additions & 2 deletions scanner/laravel.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package scanner

import (
"bufio"
"encoding/base64"
"fmt"
"github.com/superfly/flyctl/helpers"
"os"
"os/exec"
"regexp"
"strconv"

"github.com/superfly/flyctl/helpers"
"strings"
)

type ComposerLock struct {
Expand Down Expand Up @@ -67,6 +69,14 @@ func configureLaravel(sourceDir string, config *ScannerConfig) (*SourceInfo, err
"NODE_VERSION": "18",
}

// Extract DB, Redis config from dotenv
db, redis, skipDB := extractConnections(".env")
s.SkipDatabase = skipDB
s.RedisDesired = redis
if db != 0 {
s.DatabaseDesired = db
}

return s, nil
}

Expand Down Expand Up @@ -104,3 +114,61 @@ func extractPhpVersion() (string, error) {

return "", fmt.Errorf("could not find php version")
}

var dbRegStr = "^ *(DB_CONNECTION|DATABASE_URL) *= *(\"|')? *[a-zA-Z]+(\"|')?"
var redisRegStr = "^[^#]*redis"

// extractConnections detects the database connection of a laravel fly app
// by checking the .env file in the project's base directory for connection keywords.
// This ignores commented out lines and prioritizes the first connection occurance over others.
//
// Returns three variables:
//
// db - DatabaseKind of the connection extracted
// redis - reports whether redis was detected
// skipDb - reports whether a connection or redis was detected
func extractConnections(path string) (db DatabaseKind, redis bool, skipDb bool) {
// Get File Content

file, err := os.Open(path)
if err != nil {
return 0, false, true
}
defer file.Close() //skipcq: GO-S2307

// Set up Regex to match
// -not commented out, with DB_CONNECTION
dbReg := regexp.MustCompile(dbRegStr)
// -not commented out with redis keyword
redisReg := regexp.MustCompile(redisRegStr)

// Default Return Variables
db = 0
redis = false
skipDb = true

// Check each line for
// match on redis or db regex
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()

if redisReg.MatchString(text) {
redis = true
skipDb = false
} else if db == 0 && dbReg.MatchString(text) {
if strings.Contains(text, "mysql") {
db = DatabaseKindMySQL
skipDb = false
} else if strings.Contains(text, "pgsql") || strings.Contains(text, "postgres") {
db = DatabaseKindPostgres
skipDb = false
} else if strings.Contains(text, "sqlite") {
db = DatabaseKindSqlite
skipDb = false
}
}
}

return db, redis, skipDb
}