-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconda.go
138 lines (114 loc) · 2.89 KB
/
conda.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package goconda
import (
"errors"
"os"
"github.com/CREDOProject/sharedutils/env"
"github.com/CREDOProject/sharedutils/shell"
)
type verb string
var execCommander = shell.New
const (
Install verb = "install"
Download = "download"
)
var (
ErrNoPackageName = errors.New("Package name not specified.")
ErrNoVerb = errors.New("Verb not specified.")
)
type conda struct {
binaryName string
condaPath string // used in CONDA_ENVS_PATH.
downloadPath string // used in CONDA_PKGS_DIRS.
dryRun bool
packageInfo PackageInfo
verb verb
}
type command struct {
binaryName *string
binaryArguments []string
env map[string]string
}
type PackageInfo struct {
PackageName string
Channel string
}
// Installs a package.
// https://docs.anaconda.com/free/working-with-conda/packages/install-packages/
func (c *conda) Install(info *PackageInfo) *conda {
c.packageInfo = *info
c.verb = Install
return c
}
// Downloads a package.
// https://docs.anaconda.com/free/working-with-conda/packages/shared-pkg-cache/
func (c *conda) Download(info *PackageInfo, downloadPath string) *conda {
c.packageInfo = *info
c.verb = Download
c.downloadPath = downloadPath
return c
}
// Enables dry-run.
// https://docs.conda.io/projects/conda/en/latest/commands/install.html#output,-prompt,-and-flow-control-options
func (c *conda) DryRun() *conda {
c.dryRun = true
return c
}
// Start a new Conda command.
// https://docs.anaconda.com/free/working-with-conda/
func New(binaryName string, downloadPath string, condaPath string) *conda {
return &conda{
binaryName: binaryName,
condaPath: condaPath,
downloadPath: downloadPath,
}
}
// Build the Conda command so it can be run.
func (p *conda) Seal() (*command, error) {
args := []string{}
switch p.verb {
case Install:
args = append(args, "install")
case Download:
args = append(args, "install", "--download-only")
default:
return nil, errors.New("No verb specified.")
}
if p.packageInfo.PackageName == "" {
return nil, errors.New("No package name specified.")
}
args = append(args, p.packageInfo.PackageName)
if p.packageInfo.Channel != "" {
args = append(args, "--channel", p.packageInfo.Channel)
}
if p.dryRun {
args = append(args, "--dry-run")
}
env := make(map[string]string)
if p.downloadPath != "" {
env["CONDA_PKGS_DIRS"] = p.downloadPath
}
if p.condaPath != "" {
env["CONDA_ENVS_PATH"] = p.condaPath
}
return &command{
binaryName: &p.binaryName,
binaryArguments: args,
env: env,
}, nil
}
type RunOptions struct {
Output *os.File
}
// Runs the command.
func (c *command) Run(options *RunOptions) error {
command := execCommander().Command(*c.binaryName, c.binaryArguments...)
if options.Output != nil {
command.Stdout = options.Output
command.Stderr = options.Output
}
if c.env != nil {
command.Env = env.Roll(c.env)
}
error := command.Run()
return error
}