go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/actions/cipd.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package actions 16 17 import ( 18 "context" 19 "os" 20 "path/filepath" 21 "strings" 22 23 "go.chromium.org/luci/cipkg/core" 24 "go.chromium.org/luci/common/exec" 25 "go.chromium.org/luci/common/system/environ" 26 ) 27 28 // ActionCIPDExportTransformer is the default transformer for 29 // core.ActionCIPDExport. 30 func ActionCIPDExportTransformer(a *core.ActionCIPDExport, deps []Package) (*core.Derivation, error) { 31 return ReexecDerivation(a, true) 32 } 33 34 // ActionCIPDExportExecutor is the default executor for core.ActionCIPDExport. 35 // It will use the cipd binary/script (or cipd.bat/cipd.exe on windows) on PATH 36 // to export the packages required. 37 func ActionCIPDExportExecutor(ctx context.Context, a *core.ActionCIPDExport, out string) error { 38 env := environ.FromCtx(ctx) 39 env.Update(environ.New(a.Env)) 40 cmd := cipdCommand(ctx, "export", "--root", out, "--ensure-file", "-") 41 cmd.Env = env.Sorted() 42 cmd.Stdin = strings.NewReader(a.EnsureFile) 43 cmd.Stdout = os.Stdout 44 cmd.Stderr = os.Stderr 45 return cmd.Run() 46 } 47 48 func cipdCommand(ctx context.Context, arg ...string) *exec.Cmd { 49 cipd := lookup("cipd") 50 51 // Use cmd to execute batch file on windows. 52 if filepath.Ext(cipd) == ".bat" { 53 return exec.CommandContext(ctx, lookup("cmd.exe"), append([]string{"/C", cipd}, arg...)...) 54 } 55 56 return exec.CommandContext(ctx, cipd, arg...) 57 } 58 59 func lookup(bin string) string { 60 if path, err := exec.LookPath(bin); err == nil { 61 return path 62 } 63 return bin 64 }