...
1
2
3 package runhcs
4
5 import (
6 "context"
7 "errors"
8 "path/filepath"
9 "strconv"
10 )
11
12
13 func (r *Runhcs) CreateScratch(context context.Context, destpath string) error {
14 return r.CreateScratchWithOpts(context, destpath, nil)
15 }
16
17
18
19 type CreateScratchOpts struct {
20
21 SizeGB int
22
23
24 CacheFile string
25 }
26
27 func (opt *CreateScratchOpts) args() ([]string, error) {
28 var out []string
29 if opt.SizeGB < 0 {
30 return nil, errors.New("sizeGB must be >= 0")
31 } else if opt.SizeGB > 0 {
32 out = append(out, "--sizeGB", strconv.Itoa(opt.SizeGB))
33 }
34 if opt.CacheFile != "" {
35 abs, err := filepath.Abs(opt.CacheFile)
36 if err != nil {
37 return nil, err
38 }
39 out = append(out, "--cache-path", abs)
40 }
41 return out, nil
42 }
43
44
45
46 func (r *Runhcs) CreateScratchWithOpts(context context.Context, destpath string, opts *CreateScratchOpts) error {
47 args := []string{"create-scratch", "--destpath", destpath}
48 if opts != nil {
49 oargs, err := opts.args()
50 if err != nil {
51 return err
52 }
53 args = append(args, oargs...)
54 }
55 return r.runOrError(r.command(context, args...))
56 }
57
View as plain text