blob: b2dd072d61a43d2449dac723e069d9b83e0cc042 [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"
"path"
"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) Directory {
bkt := c.client.Bucket(bucket)
root := ""
if build != "" {
root = path.Join("builds", build)
}
return &BuildDirectory{&directory{
bucket: bkt,
root: root,
}}
}