Skip to content

Commit

Permalink
[minor] Refactor restic.go to avoid repetition of code (#5822)
Browse files Browse the repository at this point in the history
* Refactor restic.go to avoid repitition of code

* Address reviews

* Address reviews
  • Loading branch information
SupriyaKasten authored and Ilya Kislenko committed Jun 18, 2019
1 parent 918ef18 commit f5f1b8d
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,37 +112,46 @@ const (
)

func resticArgs(profile *param.Profile, repository, encryptionKey string) []string {
var cmd []string
switch profile.Location.Type {
case crv1alpha1.LocationTypeS3Compliant:
s3Endpoint := awsS3Endpoint
if profile.Location.Endpoint != "" {
s3Endpoint = profile.Location.Endpoint
}
return []string{
fmt.Sprintf("export %s=%s\n", location.AWSAccessKeyID, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.AWSSecretAccessKey, profile.Credential.KeyPair.Secret),
fmt.Sprintf("export %s=%s\n", ResticPassword, encryptionKey),
fmt.Sprintf("export %s=s3:%s/%s\n", ResticRepository, s3Endpoint, repository),
ResticCommand,
}
cmd = resticS3Args(profile, repository)
case crv1alpha1.LocationTypeGCS:
return []string{
fmt.Sprintf("export %s=%s\n", location.GoogleProjectId, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.GoogleCloudCreds, GoogleCloudCredsFilePath),
fmt.Sprintf("export %s=%s\n", ResticPassword, encryptionKey),
fmt.Sprintf("export %s=gs:%s/\n", ResticRepository, strings.Replace(repository, "/", ":/", 1)),
ResticCommand,
}
cmd = resticGCSArgs(profile, repository)
case crv1alpha1.LocationTypeAzure:
return []string{
fmt.Sprintf("export %s=%s\n", location.AzureStorageAccount, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.AzureStorageKey, profile.Credential.KeyPair.Secret),
fmt.Sprintf("export %s=%s\n", ResticPassword, encryptionKey),
fmt.Sprintf("export %s=azure:%s/\n", ResticRepository, strings.Replace(repository, "/", ":/", 1)),
ResticCommand,
}
cmd = resticAzureArgs(profile, repository)
default:
return nil
}
return append(cmd, fmt.Sprintf("export %s=%s\n", ResticPassword, encryptionKey), ResticCommand)
}

func resticS3Args(profile *param.Profile, repository string) []string {
s3Endpoint := awsS3Endpoint
if profile.Location.Endpoint != "" {
s3Endpoint = profile.Location.Endpoint
}
return []string{
fmt.Sprintf("export %s=%s\n", location.AWSAccessKeyID, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.AWSSecretAccessKey, profile.Credential.KeyPair.Secret),
fmt.Sprintf("export %s=s3:%s/%s\n", ResticRepository, s3Endpoint, repository),
}
}

func resticGCSArgs(profile *param.Profile, repository string) []string {
return []string{
fmt.Sprintf("export %s=%s\n", location.GoogleProjectId, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.GoogleCloudCreds, GoogleCloudCredsFilePath),
fmt.Sprintf("export %s=gs:%s/\n", ResticRepository, strings.Replace(repository, "/", ":/", 1)),
}
}

func resticAzureArgs(profile *param.Profile, repository string) []string {
return []string{
fmt.Sprintf("export %s=%s\n", location.AzureStorageAccount, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.AzureStorageKey, profile.Credential.KeyPair.Secret),
fmt.Sprintf("export %s=azure:%s/\n", ResticRepository, strings.Replace(repository, "/", ":/", 1)),
}
return nil
}

// GetOrCreateRepository will check if the repository already exists and initialize one if not
Expand Down

0 comments on commit f5f1b8d

Please sign in to comment.