Help us shape the future of Go! Take the 2026 Go Developer Survey

appconf

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package appconf reads the database and the address an application is set up with, from the files each technology keeps them in: .env, appsettings .json, application.properties, database.yml, env.php, settings.py, compose.yaml and the like. It never runs the project's code: PHP, Python and ERB are read as text, and what only code could compute is reported as such.

A password is read only to build a DSN when a command runs (Ref.DSN). Nothing Find returns holds one, and no error of this package quotes a value read from a file or the environment: errors name the file and the key.

Index

Constants

View Source
const (
	Env         = "env"         // Key is the variable
	Dotenv      = "dotenv"      // .env files
	AppSettings = "appsettings" // ASP.NET appsettings*.json, or a .csproj for its layered settings
	Properties  = "properties"  // Spring, Quarkus and Micronaut application*.properties / .yml
	YAML        = "yaml"        // Rails config/database.yml, a Go config.yaml
	PHP         = "php"         // Magento app/etc/env.php
	Python      = "python"      // Django settings.py
	Compose     = "compose"     // a database service of compose.yaml
)

Sources of a Ref: the format of the file it names, or the environment.

View Source
const (
	MySQL    = "mysql" // MySQL and MariaDB
	Postgres = "postgres"
)

Drivers.

View Source
const ComposeHost = "compose:"

ComposeHost starts a Ref.Host that names a service of the project's compose file (compose:mysql): the setting's host is that service, and this machine reaches it at the port the service publishes, read when a command runs.

Variables

Sources lists every source of a file, in the order the documentation gives them.

Functions

func CheckFile

func CheckFile(file string) error

CheckFile checks the file of a Ref: relative to the project root and inside it. A \ is taken for a /, so one configuration serves Windows too.

func CheckRef

func CheckRef(r Ref) error

CheckRef checks the shape of a Ref, without reading anything: a known source, a file inside the project, the key a source needs, a host that is host or host:port.

func Convert

func Convert(dsn, driver string) (string, error)

Convert returns a DSN given in a form an application uses but DriftGate's drivers do not take, a JDBC URL or an ADO.NET connection string, as one they take (see Ref.DSN), as is libpq's host=… user=… form; any other DSN comes back as it is. It serves a DSN of the environment or 1Password.

Types

type AppURL

type AppURL struct {
	URL  string
	File string
	Key  string
}

AppURL is an address the application is set up to answer at.

type Database

type Database struct {
	Driver string // mysql (also MariaDB) or postgres; empty when unknown (Note says why)
	Host   string // as this machine reaches it (Ref.Host applied)
	Port   string
	Name   string // the database the application uses: the working database
	User   string
	File   string // where it was found: a file relative to the root with /, or the variable
	Key    string
	Ref    Ref    // what driftgate.yaml would hold; zero when it cannot be referenced (Note says why)
	Note   string // what to check by hand, if anything
	// HostNeeded is the host: the setting needs before DriftGate can use it
	// (127.0.0.1:3306), "" for none: its host is one its client may take
	// for the local socket (MySQL's localhost or none, PostgreSQL's none),
	// and DriftGate does not guess. Note says so.
	HostNeeded string
}

Database is a database setting Find found. It never holds the password.

type Found

type Found struct {
	Databases []Database
	AppURLs   []AppURL
	Notes     []Note
}

Found is everything Find found, in the order of its sources.

func Find

func Find(root string, env Lookup) Found

Find looks for the database settings and the address of the application in root, where each technology keeps them: PHP (.env, Magento's app/etc/env.php), .NET (appsettings*.json through each .csproj, user secrets, launchSettings.json), Java (application*.properties and .yml), Rails (config/database.yml), Django (settings.py), Node (.env*, prisma/schema.prisma), Go (.env, config.yaml), compose (the database service and the port it publishes) and the environment (env, nil for none: DATABASE_URL, ConnectionStrings__*, SPRING_DATASOURCE_*). It reads files only; nothing it returns holds a password.

type Lookup

type Lookup func(string) (string, bool)

Lookup returns an environment variable, as os.LookupEnv does.

type Note

type Note struct {
	File string
	Text string
}

Note is a setting found that DriftGate cannot use as it is: another kind of database, a value set by code, a file it cannot read.

type Ref

type Ref struct {
	Source string // one of Sources, or Env
	File   string // relative to the project root, with / (empty for Env)
	Key    string // where the setting is in the file (per source); for Env the variable
	// Host replaces the host and port of the setting: the address the
	// database is reached at from this machine, when the application's
	// own is the name of a compose service (127.0.0.1:3307).
	Host string
}

Ref names a database setting in the project instead of holding it: what driftgate.yaml stores in place of a DSN. It is read when a command runs.

func (Ref) DSN

func (r Ref) DSN(root, driver string, env Lookup) (string, error)

DSN reads the setting r names and returns it as a DSN the database drivers of DriftGate accept: mysql://user:pass@host:port/ (the driver's own form for a socket or when the file holds it) or postgres://user:pass @host:port/. The database of the setting is left out (state: and data: name the databases), as are the parameters of the application's driver that DriftGate's do not take. driver is the driver of the component; a file that names another one is an error.

func (Ref) IsZero

func (r Ref) IsZero() bool

IsZero reports a Ref that names nothing.

func (Ref) Read

func (r Ref) Read(root, driver string, env Lookup) (dsn, database string, err error)

Read is DSN that also returns the database the setting names (the one the application runs on), "" when it names none.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL