github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/cmd/bacalhau/cancel.go (about) 1 package bacalhau 2 3 import ( 4 "fmt" 5 6 "github.com/filecoin-project/bacalhau/pkg/bacerrors" 7 "github.com/filecoin-project/bacalhau/pkg/util/templates" 8 "github.com/spf13/cobra" 9 "k8s.io/kubectl/pkg/util/i18n" 10 ) 11 12 var ( 13 cancelLong = templates.LongDesc(i18n.T(` 14 Cancel a previously submitted job. 15 `)) 16 17 //nolint:lll // Documentation 18 cancelExample = templates.Examples(i18n.T(` 19 # Cancel a previously submitted job 20 bacalhau cancel 51225160-807e-48b8-88c9-28311c7899e1 21 22 # Cancel a job, with a short ID. 23 bacalhau cancel ebd9bf2f 24 `)) 25 ) 26 27 type CancelOptions struct{} 28 29 func NewCancelOptions() *CancelOptions { 30 return &CancelOptions{} 31 } 32 33 func newCancelCmd() *cobra.Command { 34 cancelOptions := NewCancelOptions() 35 36 cancelCmd := &cobra.Command{ 37 Use: "cancel [id]", 38 Short: "Cancel a previously submitted job", 39 Long: cancelLong, 40 Example: cancelExample, 41 Args: cobra.ExactArgs(1), 42 PreRun: applyPorcelainLogLevel, 43 RunE: func(cmd *cobra.Command, cmdArgs []string) error { 44 return cancel(cmd, cmdArgs, cancelOptions) 45 }, 46 } 47 48 return cancelCmd 49 } 50 51 func cancel(cmd *cobra.Command, cmdArgs []string, options *CancelOptions) error { 52 ctx := cmd.Context() 53 54 var err error 55 56 requestedJobID := cmdArgs[0] 57 if requestedJobID == "" { 58 var byteResult []byte 59 byteResult, err = ReadFromStdinIfAvailable(cmd, cmdArgs) 60 if err != nil { 61 Fatal(cmd, fmt.Sprintf("Unknown error reading from file: %s\n", err), 1) 62 return err 63 } 64 requestedJobID = string(byteResult) 65 } 66 67 apiClient := GetAPIClient() 68 69 // Submit a request to cancel the specified job. It is the responsibility of the 70 // requester to decide if we are allowed to do that or not. 71 jobState, err := apiClient.Cancel(ctx, requestedJobID, "Canceled at user request") 72 if err != nil { 73 if er, ok := err.(*bacerrors.ErrorResponse); ok { 74 Fatal(cmd, er.Message, 1) 75 return nil 76 } else { 77 Fatal(cmd, fmt.Sprintf("Unknown error trying to cancel job (ID: %s): %+v", requestedJobID, err), 1) 78 return nil 79 } 80 } 81 82 cmd.Printf("Job successfully canceled. Job ID: %s\n", jobState.JobID) 83 84 return nil 85 }