| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "os/exec" |
| "strings" |
| ) |
| |
| type ensureFile struct { |
| pkg *packageDef |
| } |
| |
| func (ef *ensureFile) String() string { |
| return fmt.Sprintf("%s %s", ef.pkg.Name, ef.pkg.Version) |
| } |
| |
| // EnsureArgs is a representation of arguments to a cipd ensure call. |
| type ensureArgs struct { |
| installDir string |
| } |
| |
| // Return a string slice representation of ensure args. |
| func (ea *ensureArgs) ToArgs() []string { |
| return []string{"-root", ea.installDir, "-ensure-file", "-"} |
| } |
| |
| // Ensure recipe bundle from CIPD at a specified dir for a specified version. |
| func ensure(ctx context.Context, pkg *packageDef, installDir string) error { |
| ef := ensureFile{pkg: pkg} |
| ea := ensureArgs{installDir: installDir} |
| cmd := exec.CommandContext(ctx, "cipd", ea.ToArgs()...) |
| cmd.Stdin = strings.NewReader(ef.String()) |
| return cmd.Run() |
| } |