forked from roots/trellis-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
411d5e7
commit ece2cc2
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package trellis | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"trellis-cli/github" | ||
) | ||
|
||
const VirtualenvDir string = "virtualenv" | ||
const EnvName string = "VIRTUALENV" | ||
|
||
type Virtualenv struct { | ||
Path string | ||
BinPath string | ||
} | ||
|
||
func NewVirtualenv(path string) *Virtualenv { | ||
return &Virtualenv{ | ||
Path: filepath.Join(path, VirtualenvDir), | ||
BinPath: filepath.Join(path, VirtualenvDir, "bin"), | ||
} | ||
} | ||
|
||
func (v *Virtualenv) Activate() { | ||
os.Setenv(EnvName, v.Path) | ||
os.Setenv("PATH", fmt.Sprintf("%s/bin:$PATH", v.Path)) | ||
} | ||
|
||
func (v *Virtualenv) Active() bool { | ||
return v.BinPath == os.Getenv(EnvName) | ||
} | ||
|
||
func (v *Virtualenv) Create() (err error) { | ||
// TODO: error if not installed? or install | ||
_, cmd := v.Installed() | ||
|
||
cmd.Args = append(cmd.Args, v.Path) | ||
|
||
err = cmd.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
v.Activate() | ||
return nil | ||
} | ||
|
||
func (v *Virtualenv) Initialized() bool { | ||
if _, err := os.Stat(filepath.Join(v.BinPath, "python")); os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
if _, err := os.Stat(filepath.Join(v.BinPath, "pip")); os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (v *Virtualenv) Install(path string) string { | ||
installPath := github.DownloadLatestRelease("pypa/virtualenv", os.TempDir(), path) | ||
return installPath | ||
} | ||
|
||
func (v *Virtualenv) Installed() (ok bool, cmd *exec.Cmd) { | ||
path, err := exec.LookPath("python3") | ||
if err == nil { | ||
return true, exec.Command(path, "-m", "venv") | ||
} | ||
|
||
path, err = exec.LookPath("virtualenv") | ||
if err == nil { | ||
return true, exec.Command(path) | ||
} | ||
|
||
// TODO: check for local virtualenv installed by trellis-cli | ||
|
||
return false, nil | ||
} |