blob: de3e4643aa9db79f8f80e58f03b452342e563668 [file] [log] [blame]
// 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 artifacts
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/storage"
"go.chromium.org/luci/auth"
"google.golang.org/api/option"
)
// Client provides access to the artifacts produced by Fuchsia CI tasks.
type Client struct {
client *storage.Client
}
// NewClient creates a new Client.
func NewClient(ctx context.Context, opts auth.Options) (*Client, error) {
opts.Scopes = append(opts.Scopes, storage.ScopeReadWrite)
authenticator := auth.NewAuthenticator(ctx, auth.SilentLogin, opts)
source, err := authenticator.TokenSource()
if err != nil {
return nil, fmt.Errorf("failed to create token source: %v", err)
}
client, err := storage.NewClient(ctx, option.WithTokenSource(source))
if err != nil {
return nil, fmt.Errorf("failed to create Cloud Storage client: %v", err)
}
return &Client{client: client}, nil
}
// GetBuildDir returns the BuildDirectory for the given build. bucket is the GCS bucket.
// build is the BuildBucket build ID.
func (c *Client) GetBuildDir(bucket, build string) *BuildDirectory {
bkt := c.client.Bucket(bucket)
return &BuildDirectory{&directory{
bucket: bkt,
root: strings.Join([]string{"builds", build}, "/"),
}}
}