Skip to content

Commit 06fd263

Browse files
author
lijianguo
committed
feat(wrokflow-cli):drycc run cmd add --mount para
1 parent a80e354 commit 06fd263

7 files changed

Lines changed: 41 additions & 18 deletions

File tree

cmd/apps.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"regexp"
67
"strings"
78
"time"
89

@@ -190,16 +191,20 @@ func (d *DryccCmd) AppLogs(appID string, lines int) error {
190191
}
191192

192193
// AppRun runs a one time command in the app.
193-
func (d *DryccCmd) AppRun(appID, command string) error {
194+
func (d *DryccCmd) AppRun(appID, command string, volumeVars []string) error {
194195
s, appID, err := load(d.ConfigFile, appID)
195196

196197
if err != nil {
197198
return err
198199
}
199200

200201
d.Printf("Running '%s'...\n", command)
202+
volumeMap, err := parseMount(volumeVars)
203+
//volumeObj := map[string]interface{}{
204+
// "volumes": volumeMap,
205+
//}
201206

202-
out, err := apps.Run(s.Client, appID, command)
207+
out, err := apps.Run(s.Client, appID, command, volumeMap)
203208
if d.checkAPICompatibility(s.Client, err) != nil {
204209
return err
205210
}
@@ -214,6 +219,21 @@ func (d *DryccCmd) AppRun(appID, command string) error {
214219
return nil
215220
}
216221

222+
func parseMount(volumeVars []string) (map[string]interface{}, error) {
223+
volumeMap := make(map[string]interface{})
224+
225+
regex := regexp.MustCompile(`^([A-z_]+[A-z0-9_]*):([\s\S]*)$`)
226+
for _, volume := range volumeVars {
227+
if regex.MatchString(volume) {
228+
captures := regex.FindStringSubmatch(volume)
229+
volumeMap[captures[1]] = captures[2]
230+
} else {
231+
return nil, fmt.Errorf("'%s' does not match the pattern 'key:var', ex: MODE:test", volume)
232+
}
233+
}
234+
return volumeMap, nil
235+
}
236+
217237
// AppDestroy destroys an app.
218238
func (d *DryccCmd) AppDestroy(appID, confirm string) error {
219239
gitSession := false

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Commander interface {
1515
AppInfo(string) error
1616
AppOpen(string) error
1717
AppLogs(string, int) error
18-
AppRun(string, string) error
18+
AppRun(string, string, []string) error
1919
AppDestroy(string, string) error
2020
AppTransfer(string, string) error
2121
AutoscaleList(string) error

cmd/volumes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (d *DryccCmd) VolumesList(appID string, results int) error {
2020
if results == defaultLimit {
2121
results = s.Limit
2222
}
23-
fmt.Println("-----app-----", appID)
23+
//fmt.Println("-----app-----", appID) # debug
2424
volumes, count, err := volumes.List(s.Client, appID, results)
2525
if d.checkAPICompatibility(s.Client, err) != nil {
2626
return err
@@ -38,10 +38,10 @@ func (d *DryccCmd) VolumesList(appID string, results int) error {
3838
func printVolumes(d *DryccCmd, appID string, volumes api.Volumes, wOut io.Writer) {
3939
//volumes := ps.ByType(input)
4040

41-
fmt.Fprintf(wOut, "=== %s Volumes\n", appID)
41+
fmt.Fprintf(wOut, "=== %s volumes\n", appID)
4242

4343
for _, volume := range volumes {
44-
fmt.Fprintf(wOut, "--- %s %s\n", volume.Name, volume.Size)
44+
fmt.Fprintf(wOut, "--- %s %s\n", volume.Name, volume.Size)
4545
for k, v := range volume.Path {
4646
fmt.Println(k, v)
4747
}
@@ -120,7 +120,7 @@ func (d *DryccCmd) VolumesMount(appID string, name string, volumeVars []string)
120120
return err
121121
}
122122

123-
d.Print("done\n\n")
123+
d.Print("done\n")
124124

125125
return nil
126126
}
@@ -153,7 +153,7 @@ func (d *DryccCmd) VolumesUnmount(appID string, name string, volumeVars []string
153153
return err
154154
}
155155

156-
d.Print("done\n\n")
156+
d.Print("done\n")
157157

158158
return nil
159159
}

cmd/volumes_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func TestVolumesList(t *testing.T) {
4545
err = cmdr.VolumesList("example-go", -1)
4646
assert.NoErr(t, err)
4747

48-
assert.Equal(t, b.String(), `=== example-go Volumes
49-
--- myvolume 500M
48+
assert.Equal(t, b.String(), `=== example-go volumes
49+
--- myvolume 500M
5050
`, "output")
5151
}
5252

@@ -131,11 +131,10 @@ func TestVolumesMount(t *testing.T) {
131131
assert.NoErr(t, err)
132132

133133
assert.Equal(t, testutil.StripProgress(b.String()), `Mounting volume... done
134-
135134
`, "output")
136135
//
137136
//=== example-go Volumes
138-
//--- myvolume 500M
137+
//--- myvolume 500M
139138
//cmd /data/cmd1
140139
}
141140

@@ -175,9 +174,8 @@ func TestVolumesUnmount(t *testing.T) {
175174
assert.NoErr(t, err)
176175

177176
assert.Equal(t, testutil.StripProgress(b.String()), `Unmounting volume... done
178-
179177
`, "output")
180178

181179
//=== example-go Volumes
182-
//--- myvolume 500M
180+
//--- myvolume 500M
183181
}

drycc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Subcommands, use 'drycc help [subcommand]' to learn more::
7070
whitelist manage whitelisted addresses of an application
7171
services manage services for your applications
7272
timeouts manage pods termination grace period
73+
volumes manage volumes for your applications
7374
7475
Shortcut commands, use 'drycc shortcuts' to see all::
7576

parser/apps.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,13 @@ func appRun(argv []string, cmdr cmd.Commander) error {
205205
Runs a command inside an ephemeral app container. Default environment is
206206
/bin/bash.
207207
208-
Usage: drycc apps:run [options] [--] <command>...
208+
Usage: drycc apps:run [--mount=<volume>:<path>...] [options] [--] <command>...
209209
210210
Arguments:
211+
<volume>
212+
the volume name
213+
<path>
214+
the filesystem path.
211215
<command>
212216
the shell command to run inside the container.
213217
@@ -224,8 +228,8 @@ Options:
224228

225229
app := safeGetValue(args, "--app")
226230
command := strings.Join(args["<command>"].([]string), " ")
227-
228-
return cmdr.AppRun(app, command)
231+
mounts := args["--mount"].([]string)
232+
return cmdr.AppRun(app, command, mounts)
229233
}
230234

231235
func appDestroy(argv []string, cmdr cmd.Commander) error {

parser/apps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (d FakeDryccCmd) AppLogs(string, int) error {
3232
return errors.New("apps:logs")
3333
}
3434

35-
func (d FakeDryccCmd) AppRun(string, string) error {
35+
func (d FakeDryccCmd) AppRun(string, string, []string) error {
3636
return errors.New("apps:run")
3737
}
3838

0 commit comments

Comments
 (0)