Skip to content

Commit

Permalink
Add deactivate command
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 695ad46 commit c8ea9c2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/deactivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/se7entyse7en/pydockenv/internal/environment"
"github.com/se7entyse7en/pydockenv/log"
"github.com/spf13/cobra"
)

var deactivateCmd = &cobra.Command{
Use: "deactivate",
Short: "Deactivate the current virtual environment",
Run: func(cmd *cobra.Command, args []string) {
logger := log.Logger
err := log.SetupLogger(cmd)
if err != nil {
logger.WithError(err).Fatal("Cannot setup logger")
}

logger.Info("Deactivating virtual environment...")

if err := environment.Deactivate(); err != nil {
logger.WithError(err).Fatal("Cannot deactivate virtual environment")
}

logger.Info("Virtual environment deactivated!")
},
}

func init() {
rootCmd.AddCommand(deactivateCmd)
}
30 changes: 30 additions & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ func Activate(envName string) error {
return nil
}

func Deactivate() error {
logger := log.Logger

cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
}

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

ctxLogger := logger.WithFields(logrus.Fields{
"container-name": contName,
})
ctxLogger.Debug("Stopping container...")

err = cli.ContainerStop(context.Background(), contName, nil)
if err != nil {
return fmt.Errorf("cannot stop container: %w", err)
}

ctxLogger.Debug("Container stopped!")

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

0 comments on commit c8ea9c2

Please sign in to comment.