-
Notifications
You must be signed in to change notification settings - Fork 379
/
storage_gcs.go
44 lines (34 loc) · 1.41 KB
/
storage_gcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cli
import (
"context"
"encoding/json"
"os"
"github.com/alecthomas/kingpin"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/blob/gcs"
)
type storageGCSFlags struct {
options gcs.Options
embedCredentials bool
}
func (c *storageGCSFlags) setup(_ storageProviderServices, cmd *kingpin.CmdClause) {
cmd.Flag("bucket", "Name of the Google Cloud Storage bucket").Required().StringVar(&c.options.BucketName)
cmd.Flag("prefix", "Prefix to use for objects in the bucket").StringVar(&c.options.Prefix)
cmd.Flag("read-only", "Use read-only GCS scope to prevent write access").BoolVar(&c.options.ReadOnly)
cmd.Flag("credentials-file", "Use the provided JSON file with credentials").ExistingFileVar(&c.options.ServiceAccountCredentialsFile)
cmd.Flag("embed-credentials", "Embed GCS credentials JSON in Kopia configuration").BoolVar(&c.embedCredentials)
commonThrottlingFlags(cmd, &c.options.Limits)
}
func (c *storageGCSFlags) connect(ctx context.Context, isCreate bool, formatVersion int) (blob.Storage, error) {
if c.embedCredentials {
data, err := os.ReadFile(c.options.ServiceAccountCredentialsFile)
if err != nil {
return nil, errors.Wrap(err, "unable to open service account credentials file")
}
c.options.ServiceAccountCredentialJSON = json.RawMessage(data)
c.options.ServiceAccountCredentialsFile = ""
}
// nolint:wrapcheck
return gcs.New(ctx, &c.options)
}