go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/ftxtest/cas.go (about) 1 // Copyright 2023 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 package main 5 6 import ( 7 "context" 8 "fmt" 9 "os" 10 11 rbeClient "github.com/bazelbuild/remote-apis-sdks/go/pkg/client" 12 "github.com/bazelbuild/remote-apis-sdks/go/pkg/digest" 13 "github.com/bazelbuild/remote-apis-sdks/go/pkg/filemetadata" 14 "go.chromium.org/luci/auth" 15 "go.chromium.org/luci/client/casclient" 16 ) 17 18 const ( 19 casAddr = "remotebuildexecution.googleapis.com:443" 20 ) 21 22 type CAS struct { 23 client *rbeClient.Client 24 } 25 26 func NewCAS(ctx context.Context, authOpts auth.Options, luciInstance string) (*CAS, error) { 27 casInstance := fmt.Sprintf("projects/%s/instances/default_instance", luciInstance) 28 client, err := casclient.NewLegacy(ctx, casAddr, casInstance, authOpts, true) 29 if err != nil { 30 return nil, fmt.Errorf("casclient.NewLegacy: %v", err) 31 } 32 return &CAS{ 33 client: client, 34 }, nil 35 } 36 37 func (c *CAS) Download(ctx context.Context, hash string, size int64) (string, error) { 38 outDir, err := os.MkdirTemp("", "casOut") 39 if err != nil { 40 return "", fmt.Errorf("os.MkdirTemp: %v", err) 41 } 42 d := digest.Digest{ 43 Hash: hash, 44 Size: size, 45 } 46 _, _, err = c.client.DownloadDirectory(ctx, d, outDir, filemetadata.NewNoopCache()) 47 if err != nil { 48 return "", fmt.Errorf("cas DownloadDirectory: %v", err) 49 } 50 return outDir, nil 51 }