Documentation ¶
Index ¶
- func LookupEnv(key string, env []string) string
- func MongoConnString(out SpinOut) (connStr string, err error)
- func MySQLConnString(out SpinOut) (connStr string, err error)
- func PostgresConnString(out SpinOut) (connStr string, err error)
- func RedisConnString(out SpinOut) (connString string, err error)
- func Slash(ctx context.Context, o *SpinOut) error
- func SlashID(ctx context.Context, id string) error
- type SpinConfig
- type SpinOut
- func Generic(ctx context.Context, c *SpinConfig) (SpinOut, error)
- func Mongo(ctx context.Context, c *SpinConfig) (SpinOut, error)
- func MySQL(ctx context.Context, c *SpinConfig) (SpinOut, error)
- func Postgres(ctx context.Context, c *SpinConfig) (SpinOut, error)
- func Redis(ctx context.Context, c *SpinConfig) (SpinOut, error)
- type Spinner
- type SpinnerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LookupEnv ¶ added in v0.2.0
LookupEnv returns the value (blank for not found) in the containers environment
func MongoConnString ¶ added in v0.2.0
MongoConnString returns the connection string from the spun mongo container
func MySQLConnString ¶ added in v0.2.0
MySQLConnString returns the sql open connection string from the spun container
func PostgresConnString ¶ added in v0.2.0
PostgresConnString returns the sql open connection string from the spun container
func RedisConnString ¶ added in v0.2.0
RedisConnString returns the address of the redis container
Types ¶
type SpinConfig ¶
type SpinConfig struct { Image string Tag string Name string ExposedPorts []string Persist bool PersistVols []string Env []string }
SpinConfig is a configuration struct for spinning up a particular service
type SpinOut ¶
SpinOut is an output structure containing values from the recently spun service
func Generic ¶
func Generic(ctx context.Context, c *SpinConfig) (SpinOut, error)
Generic is a generic spinner that assumes config input without modifying it
func Mongo ¶
func Mongo(ctx context.Context, c *SpinConfig) (SpinOut, error)
Mongo spins a Mongo Container with the given settings. Nil config uses defaults
Example ¶
package main import ( "context" "fmt" "time" "github.com/tchaudhry91/spinme/spin" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { out, err := spin.Mongo(context.Background(), nil) if err != nil { fmt.Println(err) return } defer spin.SlashID(context.Background(), out.ID) // Give mongo a few seconds to boot-up, sadly there is no "ready" check yet time.Sleep(10 * time.Second) connStr, err := spin.MongoConnString(out) if err != nil { fmt.Println(err) return } client, err := mongo.NewClient(options.Client().ApplyURI(connStr)) if err != nil { fmt.Println(err) return } err = client.Connect(context.Background()) if err != nil { fmt.Println(err) return } defer client.Disconnect(context.Background()) err = client.Ping(context.Background(), nil) if err != nil { fmt.Println(err) return } fmt.Println("Connected!") }
Output: Connected!
func MySQL ¶
func MySQL(ctx context.Context, c *SpinConfig) (SpinOut, error)
MySQL spins a MySQL container with the given settings. Nil config uses defaults
Example ¶
package main import ( "context" "database/sql" "fmt" "time" "github.com/tchaudhry91/spinme/spin" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" ) func main() { out, err := spin.MySQL(context.Background(), nil) if err != nil { fmt.Println(err) return } defer spin.SlashID(context.Background(), out.ID) // Give mysql a minute to boot-up, sadly there is no "ready" check yet time.Sleep(1 * time.Minute) connStr, err := spin.MySQLConnString(out) if err != nil { fmt.Println(err) return } db, err := sql.Open("mysql", connStr) if err != nil { fmt.Println(err) return } defer db.Close() err = db.Ping() if err != nil { fmt.Println(err) return } fmt.Println("Connected!") }
Output: Connected!
func Postgres ¶
func Postgres(ctx context.Context, c *SpinConfig) (SpinOut, error)
Postgres spins a Postgres container with the given settings. Nil config uses defaults
Example ¶
package main import ( "context" "database/sql" "fmt" "time" "github.com/tchaudhry91/spinme/spin" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" ) func main() { out, err := spin.Postgres(context.Background(), nil) if err != nil { fmt.Println(err) return } defer spin.SlashID(context.Background(), out.ID) // Give postgres a few seconds to boot-up, sadly there is no "ready" check yet time.Sleep(5 * time.Second) connStr, err := spin.PostgresConnString(out) if err != nil { fmt.Println(err) return } db, err := sql.Open("postgres", connStr) if err != nil { fmt.Println(err) return } defer db.Close() err = db.Ping() if err != nil { fmt.Println(err) return } fmt.Println("Connected!") }
Output: Connected!
func Redis ¶
func Redis(ctx context.Context, c *SpinConfig) (SpinOut, error)
Redis spins a Redis container with the given settings. Nil config uses defaults.
Example ¶
package main import ( "context" "fmt" "time" "github.com/tchaudhry91/spinme/spin" "github.com/go-redis/redis" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" ) func main() { out, err := spin.Redis(context.Background(), nil) if err != nil { fmt.Println(err) return } defer spin.SlashID(context.Background(), out.ID) // Give redis a few seconds to boot-up, sadly there is no "ready" check yet time.Sleep(10 * time.Second) connStr, err := spin.RedisConnString(out) if err != nil { fmt.Println(err) return } client := redis.NewClient(&redis.Options{ Addr: connStr, }) pong, err := client.Ping().Result() if err != nil { fmt.Println(err) return } fmt.Println(pong) }
Output: PONG
type Spinner ¶
type Spinner interface {
Spin(ctx context.Context, c *SpinConfig) (SpinOut, error)
}
Spinner is an interface to be implemented by service that need to be spun up
type SpinnerFunc ¶
type SpinnerFunc func(ctx context.Context, c *SpinConfig) (SpinOut, error)
SpinnerFunc is a wrapper that allows using functions as Spinners
func SpinnerFrom ¶
func SpinnerFrom(service string) (SpinnerFunc, error)
SpinnerFrom returns the required SpinnerFunc from the service string
func (SpinnerFunc) Spin ¶
func (f SpinnerFunc) Spin(ctx context.Context, c *SpinConfig) (SpinOut, error)
Spin is the implementation for the Spinner interface