Skip to content

Commit

Permalink
Move PREFIX and getCurrentEnv from environment package to utils
Browse files Browse the repository at this point in the history
Signed-off-by: Lou Marvin Caraig <[email protected]>
  • Loading branch information
se7entyse7en committed Jun 6, 2020
1 parent 17b28cf commit 2430b37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
30 changes: 13 additions & 17 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import (
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/se7entyse7en/pydockenv/internal/utils"
"github.com/se7entyse7en/pydockenv/log"
"github.com/sirupsen/logrus"
)

var PREFIX = "pydockenv_"

type Config struct {
Name string
Python string
Expand Down Expand Up @@ -49,6 +48,7 @@ func Create(conf *Config) error {
contImg := fmt.Sprintf("python:%s", conf.Python)
images, err := listDockerImages(cli)
if err != nil {

return fmt.Errorf("cannot list docker images: %w", err)
}

Expand All @@ -72,7 +72,7 @@ func Create(conf *Config) error {
ctxLogger.Debug("Image pulled!")
}

netName := fmt.Sprintf("%s_%s_network", PREFIX, conf.Name)
netName := fmt.Sprintf("%s_%s_network", utils.RESOURCES_PREFIX, conf.Name)
ctxLogger = logger.WithFields(logrus.Fields{
"network-name": netName,
})
Expand All @@ -89,7 +89,7 @@ func Create(conf *Config) error {

ctxLogger.Debug("Network created!")

contName := fmt.Sprintf("%s_%s", PREFIX, conf.Name)
contName := fmt.Sprintf("%s_%s", utils.RESOURCES_PREFIX, conf.Name)
workdir, err := filepath.Abs(conf.ProjectDir)
if err != nil {
return err
Expand Down Expand Up @@ -143,7 +143,7 @@ func Create(conf *Config) error {

func Status() error {
logger := log.Logger
envName := getCurrentEnv()
envName := utils.GetCurrentEnv()
if envName == "" {
logger.Info("No active environment")
return nil
Expand All @@ -160,7 +160,7 @@ func Activate(envName string) error {
return err
}

contName := fmt.Sprintf("%s_%s", PREFIX, envName)
contName := fmt.Sprintf("%s_%s", utils.RESOURCES_PREFIX, envName)

ctxLogger := logger.WithFields(logrus.Fields{
"container-name": contName,
Expand All @@ -186,8 +186,8 @@ func Deactivate() error {
return err
}

envName := getCurrentEnv()
contName := fmt.Sprintf("%s_%s", PREFIX, envName)
envName := utils.GetCurrentEnv()
contName := fmt.Sprintf("%s_%s", utils.RESOURCES_PREFIX, envName)

ctxLogger := logger.WithFields(logrus.Fields{
"container-name": contName,
Expand All @@ -212,7 +212,7 @@ func ListEnvironments() error {
return err
}

currentEnvName := getCurrentEnv()
currentEnvName := utils.GetCurrentEnv()
containers, err := cli.ContainerList(
context.Background(), types.ContainerListOptions{All: true})
if err != nil {
Expand All @@ -222,11 +222,11 @@ func ListEnvironments() error {
var msgBuilder strings.Builder
for _, c := range containers {
contName := c.Names[0]
if !strings.HasPrefix(contName, PREFIX) {
if !strings.HasPrefix(contName, utils.RESOURCES_PREFIX) {
continue
}

envName := contName[len(PREFIX):]
envName := contName[len(utils.RESOURCES_PREFIX):]
m := fmt.Sprintf("%s\n", envName)
if envName == currentEnvName {
m = fmt.Sprintf("* %s", m)
Expand All @@ -252,7 +252,7 @@ func Remove(envName string) error {
return err
}

contName := fmt.Sprintf("%s_%s", PREFIX, envName)
contName := fmt.Sprintf("%s_%s", utils.RESOURCES_PREFIX, envName)

ctxLogger := logger.WithFields(logrus.Fields{
"container-name": contName,
Expand All @@ -270,7 +270,7 @@ func Remove(envName string) error {

ctxLogger.Debug("Container removed!")

netName := fmt.Sprintf("%s_%s_network", PREFIX, envName)
netName := fmt.Sprintf("%s_%s_network", utils.RESOURCES_PREFIX, envName)
ctxLogger = logger.WithFields(logrus.Fields{
"network-name": netName,
})
Expand All @@ -285,10 +285,6 @@ func Remove(envName string) error {
return nil
}

func getCurrentEnv() string {
return os.Getenv("PYDOCKENV")
}

func listDockerImages(cli *client.Client) (map[string]struct{}, error) {
images, err := cli.ImageList(context.Background(),
types.ImageListOptions{All: true})
Expand Down
9 changes: 9 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

import "os"

var RESOURCES_PREFIX = "pydockenv_"

func GetCurrentEnv() string {
return os.Getenv("PYDOCKENV")
}

0 comments on commit 2430b37

Please sign in to comment.