Skip to content

Commit

Permalink
Refactor kopia common command wrapper (#1665)
Browse files Browse the repository at this point in the history
* Refactor kopia common command wrapper

* Move repository server client to different package

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
ankitjain235 and mergify[bot] committed Oct 12, 2022
1 parent 1b6ab8f commit c584f22
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pkg/kopia/command/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type BlobListCommandArgs struct {

// BlobList returns the kopia command for listing blobs in the repository with their sizes
func BlobList(cmdArgs BlobListCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(blobSubCommand, listSubCommand)

return stringSliceCommand(args)
Expand All @@ -32,7 +32,7 @@ type BlobStatsCommandArgs struct {

// BlobStats returns the kopia command to get the blob stats
func BlobStats(cmdArgs BlobStatsCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag)

return stringSliceCommand(args)
Expand Down
18 changes: 9 additions & 9 deletions pkg/kopia/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type CommandArgs struct {
EncryptionKey string
RepoPassword string
ConfigFilePath string
LogDirectory string
}
Expand All @@ -36,21 +36,21 @@ func stringSliceCommand(args logsafe.Cmd) []string {
return args.StringSliceCMD()
}

func commonArgs(password, configFilePath, logDirectory string, requireInfoLevel bool) logsafe.Cmd {
func commonArgs(cmdArgs *CommandArgs, requireInfoLevel bool) logsafe.Cmd {
c := logsafe.NewLoggable(kopiaCommand)
if requireInfoLevel {
c = c.AppendLoggable(logLevelInfoFlag)
} else {
c = c.AppendLoggable(logLevelErrorFlag)
}
if configFilePath != "" {
c = c.AppendLoggableKV(configFileFlag, configFilePath)
if cmdArgs.ConfigFilePath != "" {
c = c.AppendLoggableKV(configFileFlag, cmdArgs.ConfigFilePath)
}
if logDirectory != "" {
c = c.AppendLoggableKV(logDirectoryFlag, logDirectory)
if cmdArgs.LogDirectory != "" {
c = c.AppendLoggableKV(logDirectoryFlag, cmdArgs.LogDirectory)
}
if password != "" {
c = c.AppendRedactedKV(passwordFlag, password)
if cmdArgs.RepoPassword != "" {
c = c.AppendRedactedKV(passwordFlag, cmdArgs.RepoPassword)
}
return c
}
Expand All @@ -65,5 +65,5 @@ func addTags(tags []string, args logsafe.Cmd) logsafe.Cmd {

// ExecKopiaArgs returns the basic Argv for executing kopia with the given config file path.
func ExecKopiaArgs(configFilePath string) []string {
return commonArgs("", configFilePath, "", false).StringSliceCMD()
return commonArgs(&CommandArgs{ConfigFilePath: configFilePath}, false).StringSliceCMD()
}
6 changes: 3 additions & 3 deletions pkg/kopia/command/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type MaintenanceInfoCommandArgs struct {

// MaintenanceInfo returns the kopia command to get maintenance info
func MaintenanceInfo(cmdArgs MaintenanceInfoCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand)
if cmdArgs.GetJsonOutput {
args = args.AppendLoggable(jsonFlag)
Expand All @@ -37,7 +37,7 @@ type MaintenanceSetOwnerCommandArgs struct {

// MaintenanceSetOwner returns the kopia command for setting custom maintenance owner
func MaintenanceSetOwner(cmdArgs MaintenanceSetOwnerCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(maintenanceSubCommand, setSubCommand)
args = args.AppendLoggableKV(ownerFlag, cmdArgs.CustomOwner)
return stringSliceCommand(args)
Expand All @@ -49,7 +49,7 @@ type MaintenanceRunCommandArgs struct {

// MaintenanceRunCommand returns the kopia command to run manual maintenance
func MaintenanceRunCommand(cmdArgs MaintenanceRunCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(maintenanceSubCommand, runSubCommand)

return stringSliceCommand(args)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kopia/command/policy_set_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type PolicySetGlobalCommandArgs struct {

// PolicySetGlobal returns the kopia command for modifying the global policy
func PolicySetGlobal(cmdArgs PolicySetGlobalCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag)
for field, val := range cmdArgs.Modifications {
args = args.AppendLoggableKV(field, val)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kopia/command/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type RestoreCommandArgs struct {

// Restore returns the kopia command for restoring root of a snapshot with given root ID
func Restore(cmdArgs RestoreCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(restoreSubCommand, cmdArgs.RootID, cmdArgs.TargetPath)

return stringSliceCommand(args)
Expand Down
12 changes: 6 additions & 6 deletions pkg/kopia/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ServerStartCommandArgs struct {

// ServerStart returns the kopia command for starting the Kopia API Server
func ServerStart(cmdArgs ServerStartCommandArgs) []string {
args := commonArgs("", cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(&CommandArgs{ConfigFilePath: cmdArgs.ConfigFilePath, LogDirectory: cmdArgs.LogDirectory}, false)

if cmdArgs.AutoGenerateCert {
args = args.AppendLoggable(serverSubCommand, startSubCommand, tlsGenerateCertFlag)
Expand Down Expand Up @@ -65,7 +65,7 @@ type ServerRefreshCommandArgs struct {
// ServerRefresh returns the kopia command for refreshing the Kopia API Server
// This helps allow new users to be able to connect to the Server instead of waiting for auto-refresh
func ServerRefresh(cmdArgs ServerRefreshCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(serverSubCommand, refreshSubCommand)
args = args.AppendRedactedKV(serverCertFingerprint, cmdArgs.Fingerprint)
args = args.AppendLoggableKV(addressFlag, cmdArgs.ServerAddress)
Expand All @@ -85,7 +85,7 @@ type ServerStatusCommandArgs struct {

// ServerStatus returns the kopia command for checking status of the Kopia API Server
func ServerStatus(cmdArgs ServerStatusCommandArgs) []string {
args := commonArgs("", cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(&CommandArgs{ConfigFilePath: cmdArgs.ConfigFilePath, LogDirectory: cmdArgs.LogDirectory}, false)
args = args.AppendLoggable(serverSubCommand, statusSubCommand)
args = args.AppendLoggableKV(addressFlag, cmdArgs.ServerAddress)
args = args.AppendRedactedKV(serverCertFingerprint, cmdArgs.Fingerprint)
Expand All @@ -101,7 +101,7 @@ type ServerListUserCommmandArgs struct {

// ServerListUser returns the kopia command to list users from the Kopia API Server
func ServerListUser(cmdArgs ServerListUserCommmandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag)

return stringSliceCommand(args)
Expand All @@ -115,7 +115,7 @@ type ServerSetUserCommandArgs struct {

// ServerSetUser returns the kopia command setting password for existing user for the Kopia API Server
func ServerSetUser(cmdArgs ServerSetUserCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, cmdArgs.NewUsername)
args = args.AppendRedactedKV(userPasswordFlag, cmdArgs.UserPassword)

Expand All @@ -130,7 +130,7 @@ type ServerAddUserCommandArgs struct {

// ServerAddUser returns the kopia command adding a new user to the Kopia API Server
func ServerAddUser(cmdArgs ServerAddUserCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, cmdArgs.NewUsername)
args = args.AppendRedactedKV(userPasswordFlag, cmdArgs.UserPassword)

Expand Down
14 changes: 7 additions & 7 deletions pkg/kopia/command/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type SnapshotCreateCommandArgs struct {
// TODO: Have better mechanism to apply global flags
func SnapshotCreate(cmdArgs SnapshotCreateCommandArgs) []string {
parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload))
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, requireLogLevelInfo)
args := commonArgs(cmdArgs.CommandArgs, requireLogLevelInfo)
args = args.AppendLoggable(snapshotSubCommand, createSubCommand, cmdArgs.PathToBackup, jsonFlag)
args = args.AppendLoggableKV(parallelFlag, parallelismStr)
args = args.AppendLoggableKV(progressUpdateIntervalFlag, longUpdateInterval)
Expand All @@ -56,7 +56,7 @@ type SnapshotRestoreCommandArgs struct {

// SnapshotRestore returns kopia command restoring snapshots with given snap ID
func SnapshotRestore(cmdArgs SnapshotRestoreCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, cmdArgs.SnapID, cmdArgs.TargetPath)
if cmdArgs.SparseRestore {
args = args.AppendLoggable(sparseFlag)
Expand All @@ -72,7 +72,7 @@ type SnapshotDeleteCommandArgs struct {

// SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID
func SnapshotDelete(cmdArgs SnapshotDeleteCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, cmdArgs.SnapID, unsafeIgnoreSourceFlag)

return stringSliceCommand(args)
Expand All @@ -86,7 +86,7 @@ type SnapshotExpireCommandArgs struct {

// SnapshotExpire returns the kopia command for removing snapshots with given root ID
func SnapshotExpire(cmdArgs SnapshotExpireCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, cmdArgs.RootID)
if cmdArgs.MustDelete {
args = args.AppendLoggable(deleteFlag)
Expand All @@ -101,7 +101,7 @@ type SnapshotGCCommandArgs struct {

// SnapshotGC returns the kopia command for issuing kopia snapshot gc
func SnapshotGC(cmdArgs SnapshotGCCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(snapshotSubCommand, gcSubCommand, deleteFlag)

return stringSliceCommand(args)
Expand All @@ -113,7 +113,7 @@ type SnapListAllCommandArgs struct {

// SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes
func SnapListAll(cmdArgs SnapListAllCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(
snapshotSubCommand,
listSubCommand,
Expand All @@ -132,7 +132,7 @@ type SnapListAllWithSnapIDsCommandArgs struct {

// SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs
func SnapListAllWithSnapIDs(cmdArgs SnapListAllWithSnapIDsCommandArgs) []string {
args := commonArgs(cmdArgs.EncryptionKey, cmdArgs.ConfigFilePath, cmdArgs.LogDirectory, false)
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag)
args = args.AppendLoggableKV(filterFlag, kopia.ManifestTypeSnapshotFilter)

Expand Down
4 changes: 2 additions & 2 deletions pkg/kopia/maintenance/get_maintenance_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func GetMaintenanceOwnerForConnectedRepository(
namespace,
pod,
container,
encryptionKey,
repoPassword,
configFilePath,
logDirectory string,
) (string, error) {
args := command.MaintenanceInfoCommandArgs{
CommandArgs: &command.CommandArgs{
EncryptionKey: encryptionKey,
RepoPassword: repoPassword,
ConfigFilePath: configFilePath,
LogDirectory: logDirectory,
},
Expand Down
File renamed without changes.

0 comments on commit c584f22

Please sign in to comment.