mirror of
https://github.com/kopia/kopia.git
synced 2026-05-15 02:05:39 -04:00
* refactor: move from io/ioutil to io and os package The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * chore: remove //nolint:gosec for os.ReadFile At the time of this commit, the G304 rule of gosec does not include the `os.ReadFile` function. We remove `//nolint:gosec` temporarily until https://github.com/securego/gosec/pull/706 is merged. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
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("max-download-speed", "Limit the download speed.").PlaceHolder("BYTES_PER_SEC").IntVar(&c.options.MaxDownloadSpeedBytesPerSecond)
|
|
cmd.Flag("max-upload-speed", "Limit the upload speed.").PlaceHolder("BYTES_PER_SEC").IntVar(&c.options.MaxUploadSpeedBytesPerSecond)
|
|
cmd.Flag("embed-credentials", "Embed GCS credentials JSON in Kopia configuration").BoolVar(&c.embedCredentials)
|
|
}
|
|
|
|
func (c *storageGCSFlags) connect(ctx context.Context, isNew bool) (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)
|
|
}
|