corso/src/cli/flags/s3.go
Abhishek Pandey d2c73827cb
Add CLI for local storage (#4243)
<!-- PR description-->

New commands to initialize & connect to a repo on local or network attached storage.
* `repo init filesystem --path /tmp/repo`
* `repo connect filesystem --path /tmp/repo`

Includes basic unit & e2e tests. More coverage to be added in a following PR to keep the size contained.

**Updates:**
* Added Repo path sanitization i.e. handle relative paths, make paths cross platform compatible, etc. 
* Removed retention artifacts, not supported for filesystem storage.
* cli docs - auto updated.
* Manually tested with all corso backup/restore/export commands.

**Doesn't include** 
1. Symlinks
2. User ids wiring into repo.
3. Repos documentation update - in an upcoming PR.
4. Prefix support -> kopia doesn't support prefixes for `filesystem` storage
5. More E2E tests. 

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [x] 🕐 Yes, but in a later PR
- [ ]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* #1416 

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [x]  Unit test
- [x] 💚 E2E
2023-09-19 09:46:54 +00:00

92 lines
2.6 KiB
Go

package flags
import (
"strconv"
"github.com/spf13/cobra"
"github.com/alcionai/corso/src/pkg/credentials"
"github.com/alcionai/corso/src/pkg/storage"
)
// S3 bucket flags
const (
BucketFN = "bucket"
EndpointFN = "endpoint"
PrefixFN = "prefix"
DoNotUseTLSFN = "disable-tls"
DoNotVerifyTLSFN = "disable-tls-verification"
)
// S3 bucket flag values
var (
BucketFV string
EndpointFV string
PrefixFV string
DoNotUseTLSFV bool
DoNotVerifyTLSFV bool
)
// S3 bucket flags
func AddS3BucketFlags(cmd *cobra.Command) {
fs := cmd.Flags()
// Flags addition ordering should follow the order we want them to appear in help and docs:
// More generic and more frequently used flags take precedence.
fs.StringVar(&BucketFV, BucketFN, "", "Name of S3 bucket for repo. (required)")
fs.StringVar(&PrefixFV, PrefixFN, "", "Repo prefix within bucket.")
fs.StringVar(&EndpointFV, EndpointFN, "", "S3 service endpoint.")
fs.BoolVar(&DoNotUseTLSFV, DoNotUseTLSFN, false, "Disable TLS (HTTPS)")
fs.BoolVar(&DoNotVerifyTLSFV, DoNotVerifyTLSFN, false, "Disable TLS (HTTPS) certificate verification.")
// In general, we don't want to expose this flag to users and have them mistake it
// for a broad-scale idempotency solution. We can un-hide it later the need arises.
fs.BoolVar(&SucceedIfExistsFV, SucceedIfExistsFN, false, "Exit with success if the repo has already been initialized.")
cobra.CheckErr(fs.MarkHidden("succeed-if-exists"))
}
func S3FlagOverrides(cmd *cobra.Command) map[string]string {
fs := GetPopulatedFlags(cmd)
return PopulateS3Flags(fs)
}
func PopulateS3Flags(flagset PopulatedFlags) map[string]string {
s3Overrides := map[string]string{
storage.StorageProviderTypeKey: storage.ProviderS3.String(),
}
if _, ok := flagset[AWSAccessKeyFN]; ok {
s3Overrides[credentials.AWSAccessKeyID] = AWSAccessKeyFV
}
if _, ok := flagset[AWSSecretAccessKeyFN]; ok {
s3Overrides[credentials.AWSSecretAccessKey] = AWSSecretAccessKeyFV
}
if _, ok := flagset[AWSSessionTokenFN]; ok {
s3Overrides[credentials.AWSSessionToken] = AWSSessionTokenFV
}
if _, ok := flagset[BucketFN]; ok {
s3Overrides[storage.Bucket] = BucketFV
}
if _, ok := flagset[PrefixFN]; ok {
s3Overrides[storage.Prefix] = PrefixFV
}
if _, ok := flagset[DoNotUseTLSFN]; ok {
s3Overrides[storage.DoNotUseTLS] = strconv.FormatBool(DoNotUseTLSFV)
}
if _, ok := flagset[DoNotVerifyTLSFN]; ok {
s3Overrides[storage.DoNotVerifyTLS] = strconv.FormatBool(DoNotVerifyTLSFV)
}
if _, ok := flagset[EndpointFN]; ok {
s3Overrides[storage.Endpoint] = EndpointFV
}
return s3Overrides
}