pg

package
v0.0.0-...-6c61ff5 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandPg = &cli.Command{
	Name:  "pg",
	Usage: "Start PostgreSQL containers",
	Action: func(c *cli.Context) error {
		rand.Seed(time.Now().UnixNano())

		port, _ := nat.NewPort("tcp", "5432")
		hostPort := nat.PortBinding{
			HostIP:   "0.0.0.0",
			HostPort: fmt.Sprintf("%d", rand.Intn(10000)+10000),
		}

		cid, err := docker.Client.Start(
			"docker.io/library/postgres:latest",
			"postgres:latest",
			fmt.Sprintf("pg__%d", rand.Int()),
			[]string{
				"POSTGRES_PASSWORD=postgres",
				"POSTGRES_USER=postgres",
				fmt.Sprintf("POSTGRES_DB=%s", c.Args().First()),
			},
			nat.PortMap{
				port: []nat.PortBinding{hostPort},
			},
		)
		if err != nil {
			return err
		}

		address := fmt.Sprintf("postgresql://postgres:postgres@localhost:%s/%s", hostPort.HostPort, c.Args().First())

		fmt.Printf("Started container %s with address %s\n\n\u001b[0mPress ENTER to stop.\n", cid[:7], address)
		fmt.Scanln()

		err = docker.Client.StopContainer(cid)
		if err != nil {
			return err
		}

		return docker.Client.RemoveContainer(cid)
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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