diff --git a/cmd/harbor/root/replication/executions/list.go b/cmd/harbor/root/replication/executions/list.go index 067ce3d41..77ce667c1 100644 --- a/cmd/harbor/root/replication/executions/list.go +++ b/cmd/harbor/root/replication/executions/list.go @@ -57,7 +57,11 @@ func ListCommand() *cobra.Command { return fmt.Errorf("invalid replication policy ID: %s, %v", args[0], err) } } else { - rpolicyID = prompt.GetReplicationPolicyFromUser() + var err error + rpolicyID, err = prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } } log.Debug("Fetching executions...") diff --git a/cmd/harbor/root/replication/executions/view.go b/cmd/harbor/root/replication/executions/view.go index 892dc890e..98660bb4b 100644 --- a/cmd/harbor/root/replication/executions/view.go +++ b/cmd/harbor/root/replication/executions/view.go @@ -65,8 +65,14 @@ func ViewCommand() *cobra.Command { return fmt.Errorf("invalid replication execution ID: %s, %v", args[0], err) } } else { - rpolicyID := prompt.GetReplicationPolicyFromUser() - execID = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + rpolicyID, err := prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } + execID, err = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + if err != nil { + return fmt.Errorf("failed to get replication execution: %w", err) + } } execution, err := api.GetReplicationExecution(execID) diff --git a/cmd/harbor/root/replication/logs.go b/cmd/harbor/root/replication/logs.go index 1d8442a4e..8bf7ea209 100644 --- a/cmd/harbor/root/replication/logs.go +++ b/cmd/harbor/root/replication/logs.go @@ -38,11 +38,24 @@ func LogsCommand() *cobra.Command { Args: cobra.MaximumNArgs(0), RunE: func(cmd *cobra.Command, args []string) error { if execID != 0 && taskID == 0 { - taskID = prompt.GetReplicationTaskIDFromUser(execID) + var err error + taskID, err = prompt.GetReplicationTaskIDFromUser(execID) + if err != nil { + return fmt.Errorf("failed to get replication task: %w", err) + } } else if execID == 0 && taskID == 0 { - rpolicyID := prompt.GetReplicationPolicyFromUser() - execID = prompt.GetReplicationExecutionIDFromUser(rpolicyID) - taskID = prompt.GetReplicationTaskIDFromUser(execID) + rpolicyID, err := prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } + execID, err = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + if err != nil { + return fmt.Errorf("failed to get replication execution: %w", err) + } + taskID, err = prompt.GetReplicationTaskIDFromUser(execID) + if err != nil { + return fmt.Errorf("failed to get replication task: %w", err) + } } else if execID == 0 && taskID != 0 { return fmt.Errorf("execution ID must be provided if task ID is specified") } diff --git a/cmd/harbor/root/replication/policies/delete.go b/cmd/harbor/root/replication/policies/delete.go index 4bcf60c90..9ccd92e9a 100644 --- a/cmd/harbor/root/replication/policies/delete.go +++ b/cmd/harbor/root/replication/policies/delete.go @@ -39,7 +39,11 @@ func DeleteCommand() *cobra.Command { return fmt.Errorf("invalid replication policy ID: %s, %v", args[0], err) } } else { - rpolicyID = prompt.GetReplicationPolicyFromUser() + var err error + rpolicyID, err = prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } } _, err := api.DeleteReplicationPolicy(rpolicyID) diff --git a/cmd/harbor/root/replication/policies/update.go b/cmd/harbor/root/replication/policies/update.go index ebda86057..a16f09095 100644 --- a/cmd/harbor/root/replication/policies/update.go +++ b/cmd/harbor/root/replication/policies/update.go @@ -40,7 +40,11 @@ func UpdateCommand() *cobra.Command { return fmt.Errorf("invalid replication policy ID: %s, %v", args[0], err) } } else { - policyID = prompt.GetReplicationPolicyFromUser() + var err error + policyID, err = prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } } existingPolicy, err := api.GetReplicationPolicy(policyID) diff --git a/cmd/harbor/root/replication/policies/view.go b/cmd/harbor/root/replication/policies/view.go index f252072c8..6e36aad05 100644 --- a/cmd/harbor/root/replication/policies/view.go +++ b/cmd/harbor/root/replication/policies/view.go @@ -41,7 +41,11 @@ func ViewCommand() *cobra.Command { return fmt.Errorf("invalid replication policy ID: %s, %v", args[0], err) } } else { - rpolicyID = prompt.GetReplicationPolicyFromUser() + var err error + rpolicyID, err = prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } } response, err := api.GetReplicationPolicy(rpolicyID) diff --git a/cmd/harbor/root/replication/start.go b/cmd/harbor/root/replication/start.go index f2eefc959..1c546e80d 100644 --- a/cmd/harbor/root/replication/start.go +++ b/cmd/harbor/root/replication/start.go @@ -41,7 +41,11 @@ func StartCommand() *cobra.Command { return fmt.Errorf("invalid replication policy ID: %s, %v", args[0], err) } } else { - rpolicyID = prompt.GetReplicationPolicyFromUser() + var err error + rpolicyID, err = prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } } response, err := api.StartReplication(rpolicyID) if err != nil { diff --git a/cmd/harbor/root/replication/stop.go b/cmd/harbor/root/replication/stop.go index 64a959782..d3c795202 100644 --- a/cmd/harbor/root/replication/stop.go +++ b/cmd/harbor/root/replication/stop.go @@ -41,10 +41,20 @@ func StopCommand() *cobra.Command { if err != nil { return fmt.Errorf("invalid replication policy ID: %s, %v", args[0], err) } - executionID = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + executionID, err = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + if err != nil { + return fmt.Errorf("failed to get replication execution: %w", err) + } } else { - rpolicyID = prompt.GetReplicationPolicyFromUser() - executionID = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + var err error + rpolicyID, err = prompt.GetReplicationPolicyFromUser() + if err != nil { + return fmt.Errorf("failed to get replication policy: %w", err) + } + executionID, err = prompt.GetReplicationExecutionIDFromUser(rpolicyID) + if err != nil { + return fmt.Errorf("failed to get replication execution: %w", err) + } } execution, err := api.GetReplicationExecution(executionID) diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go index f21932e5f..f7f86b8ce 100644 --- a/pkg/prompt/prompt.go +++ b/pkg/prompt/prompt.go @@ -381,52 +381,79 @@ func GetRobotIDFromUser(projectID int64) (int64, error) { return id, nil } -func GetReplicationPolicyFromUser() int64 { +func GetReplicationPolicyFromUser() (int64, error) { replicationPolicyID := make(chan int64) + errChan := make(chan error, 1) go func() { response, err := api.ListReplicationPolicies() if err != nil { - log.Fatal(err) + errChan <- err + return + } + if len(response.Payload) == 0 { + errChan <- fmt.Errorf("no replication policies found") + return } rpolicies.ReplicationPoliciesList(response.Payload, replicationPolicyID) }() - return <-replicationPolicyID + select { + case id := <-replicationPolicyID: + return id, nil + case err := <-errChan: + return 0, err + } } -func GetReplicationExecutionIDFromUser(rpolicyID int64) int64 { +func GetReplicationExecutionIDFromUser(rpolicyID int64) (int64, error) { executionID := make(chan int64) + errChan := make(chan error, 1) go func() { response, err := api.ListReplicationExecutions(rpolicyID) if err != nil { - log.Fatal(err) + errChan <- err + return } if len(response.Payload) == 0 { - log.Fatal("no replication executions found") + errChan <- fmt.Errorf("no replication executions found") + return } rexecutions.ReplicationExecutionList(response.Payload, executionID) }() - return <-executionID + select { + case id := <-executionID: + return id, nil + case err := <-errChan: + return 0, err + } } -func GetReplicationTaskIDFromUser(execID int64) int64 { +func GetReplicationTaskIDFromUser(execID int64) (int64, error) { executionID := make(chan int64) + errChan := make(chan error, 1) go func() { response, err := api.ListReplicationTasks(execID) if err != nil { - log.Fatal(err) + errChan <- err + return } if len(response.Payload) == 0 { - log.Fatal("no replication tasks found") + errChan <- fmt.Errorf("no replication tasks found") + return } rtasks.ReplicationTasksList(response.Payload, executionID) }() - return <-executionID + select { + case id := <-executionID: + return id, nil + case err := <-errChan: + return 0, err + } } // Get GetMemberIDFromUser choosing from list of members