Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix #5: complete forward mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed May 30, 2019
1 parent 527c0d9 commit b7a6bca
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 37 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ install:

.PHONY: run
run:
@go run main.go up -f testdata/app.toml -m 6379:16379 -m 5432:15432
@go run main.go up -f testdata/app.toml -m 6379:16379 -m 5672:15672 -m 5432:15432
@echo ---
@go run main.go down -f testdata/app.toml -m 6379:16379 -m 5432:15432
@go run main.go down -f testdata/app.toml -m 6379:16379 -m 5672:15672 -m 5432:15432
@echo ---
@go run main.go env -f testdata/app.toml -m 6379:16379 -m 5432:15432
@go run main.go env -f testdata/app.toml -m 6379:16379 -m 5672:15672 -m 5432:15432
@echo ---
@go run main.go forward -f testdata/app.toml -m 6379:16379 -m 5432:15432
@go run main.go forward -f testdata/app.toml -m 6379:16379 -m 5672:15672 -m 5432:15432
@echo ---
@go run main.go call -f testdata/app.toml -m 6379:16379 -m 5432:15432 -- echo '$$REDIS_PORT $$PGPORT'
@go run main.go call -f testdata/app.toml -m 6379:16379 -m 5672:15672 -m 5432:15432 -- echo '$$REDIS_PORT $$PGPORT'
9 changes: 3 additions & 6 deletions internal/cmd/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/kamilsk/lift/internal/config"
"github.com/kamilsk/lift/internal/forward"
"github.com/kamilsk/lift/internal/shell"
)

Expand All @@ -17,16 +18,12 @@ var callCmd = &cobra.Command{
Example: "lift call -- echo $GOMODULE",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := scope(cmd)
if err != nil {
return err
}
cnf, err := config.FromScope(ctx)
cnf, err := config.FromScope(scope(cmd))
if err != nil {
return err
}
vars := make([]string, 0, len(cnf.Environment))
for variable, value := range cnf.Environment {
for variable, value := range forward.TransformEnvironment(cnf) {
vars = append(vars, fmt.Sprintf("%s=%s", variable, value))
}
var command = shell.Command(args[0])
Expand Down
6 changes: 1 addition & 5 deletions internal/cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ var downCmd = &cobra.Command{
Short: "Dump instruction for eval to down environment locally",
Long: "Dump instruction for eval to down environment locally.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := scope(cmd)
if err != nil {
return err
}
cnf, err := config.FromScope(ctx)
cnf, err := config.FromScope(scope(cmd))
if err != nil {
return err
}
Expand Down
9 changes: 3 additions & 6 deletions internal/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ import (
"github.com/spf13/cobra"

"github.com/kamilsk/lift/internal/config"
"github.com/kamilsk/lift/internal/forward"
)

var envCmd = &cobra.Command{
Use: "env",
Short: "Dump environment variables from configuration file",
Long: "Dump environment variables from configuration file.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := scope(cmd)
if err != nil {
return err
}
cnf, err := config.FromScope(ctx)
cnf, err := config.FromScope(scope(cmd))
if err != nil {
return err
}
vars := make([]string, 0, len(cnf.Environment))
for variable, value := range cnf.Environment {
for variable, value := range forward.TransformEnvironment(cnf) {
vars = append(vars, fmt.Sprintf("%s=%s", variable, value))
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), strings.Join(vars, "\n"))
Expand Down
6 changes: 1 addition & 5 deletions internal/cmd/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ var forwardCmd = &cobra.Command{
Short: "Dump instruction for port forwarding",
Long: "Dump instruction for port forwarding.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := scope(cmd)
if err != nil {
return err
}
cnf, err := config.FromScope(ctx)
cnf, err := config.FromScope(scope(cmd))
if err != nil {
return err
}
Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ var upCmd = &cobra.Command{
Short: "Dump instruction for eval to up environment locally",
Long: "Dump instruction for eval to up environment locally.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := scope(cmd)
if err != nil {
return err
}
cnf, err := config.FromScope(ctx)
cnf, err := config.FromScope(scope(cmd))
if err != nil {
return err
}
sh := shell.New(os.Getenv("SHELL"))
commands := make([]shell.Command, 0, 8)
for variable, value := range cnf.Environment {
for variable, value := range forward.TransformEnvironment(cnf) {
commands = append(commands, sh.Assign(variable, value))
}
command, err := forward.Command(cnf, true)
Expand Down
8 changes: 6 additions & 2 deletions internal/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ func Decode(scope internal.Scope, r io.Reader) (Service, error) {
}

// FromScope reads configuration from file and decodes it into the struct.
func FromScope(scope internal.Scope) (Service, error) {
func FromScope(scope internal.Scope, err error) (Service, error) {
var service Service
if err != nil {
return service, err
}
f, err := os.Open(scope.ConfigPath)
if err != nil {
return Service{}, err
return service, err
}
defer safe.Close(f)
return Decode(scope, f)
Expand Down
38 changes: 36 additions & 2 deletions internal/forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func Command(cnf config.Service, detach bool) (shell.Command, error) {
if err != nil {
return command, err
}
if _, present := ports[remote]; present {
if _, found := ports[remote]; found {
continue
}
ports[remote] = struct{}{}
forward := strconv.Itoa(int(remote))
if local, present := cnf.PortMapping[remote]; present {
if local, found := cnf.PortMapping[remote]; found {
forward = fmt.Sprintf("%d:%d", local, remote)
}
args = append(args, forward)
Expand Down Expand Up @@ -71,6 +71,11 @@ func ExtractPort(connection string) (uint16, error) {
}
}

// ReplacePort replaces a port number in a connection definition.
func ReplacePort(connection string, from, to uint16) string {
return strings.Replace(connection, strconv.FormatUint(uint64(from), 10), strconv.FormatUint(uint64(to), 10), 1)
}

// PodName builds pod name.
func PodName(service, entity string, isLocal bool) string {
parts := append(make([]string, 0, 4), service)
Expand All @@ -81,6 +86,35 @@ func PodName(service, entity string, isLocal bool) string {
return strings.ToLower(strings.Join(parts, "-"))
}

// TransformEnvironment applies a port mapping to a copy of environment variables.
func TransformEnvironment(cnf config.Service) config.Environment {
if len(cnf.PortMapping) == 0 {
return cnf.Environment
}

copied := config.Environment{}
for k, v := range cnf.Environment {
copied[k] = v
}

for _, dep := range cnf.Dependencies {
if len(dep.Forward) == 0 {
continue
}
for _, env := range dep.Forward {
remote, err := ExtractPort(copied[env])
if err != nil {
continue
}
if local, found := cnf.PortMapping[remote]; found {
copied[env] = ReplacePort(copied[env], remote, local)
}
}
}

return copied
}

// Shutdown returns commands to shutdown the forward tool.
func Shutdown(cnf config.Service) []shell.Command {
return []shell.Command{
Expand Down

0 comments on commit b7a6bca

Please sign in to comment.