spin

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LookupEnv added in v0.2.0

func LookupEnv(key string, env []string) string

LookupEnv returns the value (blank for not found) in the containers environment

func MongoConnString added in v0.2.0

func MongoConnString(out SpinOut) (connStr string, err error)

MongoConnString returns the connection string from the spun mongo container

func MySQLConnString added in v0.2.0

func MySQLConnString(out SpinOut) (connStr string, err error)

MySQLConnString returns the sql open connection string from the spun container

func PostgresConnString added in v0.2.0

func PostgresConnString(out SpinOut) (connStr string, err error)

PostgresConnString returns the sql open connection string from the spun container

func RedisConnString added in v0.2.0

func RedisConnString(out SpinOut) (connString string, err error)

RedisConnString returns the address of the redis container

func Slash

func Slash(ctx context.Context, o *SpinOut) error

Slash is a function to remove the given container

func SlashID

func SlashID(ctx context.Context, id string) error

SlashID is a function to slash a given container id

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

type SpinOut struct {
	ID        string
	IP        string
	Service   string
	Endpoints map[string]string
	Env       []string
}

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

Jump to

Keyboard shortcuts

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