diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..f3ab914 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.20 diff --git a/cmd/mint/debug.go b/cmd/mint/debug.go index 6ab8374..112af61 100644 --- a/cmd/mint/debug.go +++ b/cmd/mint/debug.go @@ -12,10 +12,10 @@ var debugCmd = &cobra.Command{ return requireAccessToken() }, RunE: func(cmd *cobra.Command, args []string) error { - return service.DebugTask(cli.DebugTaskConfig{RunURL: args[0]}) + return service.DebugTask(cli.DebugTaskConfig{DebugKey: args[0]}) }, Short: "Debug a task on Mint", - Use: "debug [flags] [runURL]", + Use: "debug [flags] [debugKey]", } func init() { diff --git a/internal/api/client.go b/internal/api/client.go index ce37a88..a954d2a 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/url" "github.com/rwx-research/mint-cli/cmd/mint/config" "github.com/rwx-research/mint-cli/internal/accesstoken" @@ -45,14 +46,14 @@ func NewClient(cfg Config) (Client, error) { return Client{roundTrip}, nil } -func (c Client) GetDebugConnectionInfo(runID string) (DebugConnectionInfo, error) { +func (c Client) GetDebugConnectionInfo(debugKey string) (DebugConnectionInfo, error) { connectionInfo := DebugConnectionInfo{} - if runID == "" { - return connectionInfo, errors.New("missing runID") + if debugKey == "" { + return connectionInfo, errors.New("missing debugKey") } - endpoint := fmt.Sprintf("/mint/api/runs/%s/debug_connection_info", runID) + endpoint := fmt.Sprintf("/mint/api/debug_connection_info?debug_key=%s", url.QueryEscape(debugKey)) req, err := http.NewRequest(http.MethodGet, endpoint, nil) if err != nil { return connectionInfo, errors.Wrap(err, "unable to create new HTTP request") @@ -68,6 +69,10 @@ func (c Client) GetDebugConnectionInfo(runID string) (DebugConnectionInfo, error case 200: break case 400: + connectionError := DebugConnectionInfoError{} + if err := json.NewDecoder(resp.Body).Decode(&connectionError); err == nil { + return connectionInfo, errors.New(connectionError.Error) + } return connectionInfo, errors.ErrBadRequest case 404: return connectionInfo, errors.ErrNotFound diff --git a/internal/api/debug_connection_info.go b/internal/api/debug_connection_info.go index 8ce49e0..4bce28b 100644 --- a/internal/api/debug_connection_info.go +++ b/internal/api/debug_connection_info.go @@ -5,3 +5,7 @@ type DebugConnectionInfo struct { PublicHostKey string `json:"public_host_key"` PrivateUserKey string `json:"private_user_key"` } + +type DebugConnectionInfoError struct { + Error string +} diff --git a/internal/cli/config.go b/internal/cli/config.go index 0dc6d7e..5717503 100644 --- a/internal/cli/config.go +++ b/internal/cli/config.go @@ -31,12 +31,12 @@ func (c Config) Validate() error { } type DebugTaskConfig struct { - RunURL string + DebugKey string } func (c DebugTaskConfig) Validate() error { - if c.RunURL == "" { - return errors.New("missing Mint run URL") + if c.DebugKey == "" { + return errors.New("you must specify a run ID, a task ID, or a Mint Cloud URL") } return nil diff --git a/internal/cli/interfaces.go b/internal/cli/interfaces.go index 47e6765..6ccc61b 100644 --- a/internal/cli/interfaces.go +++ b/internal/cli/interfaces.go @@ -7,7 +7,7 @@ import ( ) type APIClient interface { - GetDebugConnectionInfo(runID string) (api.DebugConnectionInfo, error) + GetDebugConnectionInfo(debugKey string) (api.DebugConnectionInfo, error) InitiateRun(api.InitiateRunConfig) (*api.InitiateRunResult, error) ObtainAuthCode(api.ObtainAuthCodeConfig) (*api.ObtainAuthCodeResult, error) AcquireToken(tokenUrl string) (*api.AcquireTokenResult, error) diff --git a/internal/cli/service.go b/internal/cli/service.go index 4036bec..a80f9a1 100644 --- a/internal/cli/service.go +++ b/internal/cli/service.go @@ -39,20 +39,9 @@ func (s Service) DebugTask(cfg DebugTaskConfig) error { return errors.Wrap(err, "validation failed") } - runId := filepath.Base(cfg.RunURL) - - connectionInfo, err := s.APIClient.GetDebugConnectionInfo(runId) + connectionInfo, err := s.APIClient.GetDebugConnectionInfo(cfg.DebugKey) if err != nil { - switch { - case errors.Is(err, errors.ErrBadRequest): - return errors.New(fmt.Sprintf("Run %q doesn't appear to have a task that's ready to debug. Please check the run status in your browser.", runId)) - case errors.Is(err, errors.ErrNotFound): - return errors.New(fmt.Sprintf("Unknown run %q. Please invoke 'mint debug' with a URL to a run.", runId)) - case errors.Is(err, errors.ErrGone): - return errors.New("Unable to locate a server instance for your run. Please retry the execution of the entire run and contact us if the issues persits.") - default: - return errors.Wrapf(err, "unable to fetch connection info for run %s", runId) - } + return err } privateUserKey, err := ssh.ParsePrivateKey([]byte(connectionInfo.PrivateUserKey)) diff --git a/internal/cli/service_test.go b/internal/cli/service_test.go index d8dd6dd..9cb0d63 100644 --- a/internal/cli/service_test.go +++ b/internal/cli/service_test.go @@ -835,7 +835,7 @@ AAAEC6442PQKevgYgeT0SIu9zwlnEMl6MF59ZgM+i0ByMv4eLJPqG3xnZcEQmktHj/GY2i runID = fmt.Sprintf("run-%d", GinkgoRandomSeed()) debugConfig = cli.DebugTaskConfig{ - RunURL: fmt.Sprintf("https://cloud.rwx.com/mint/rwx/runs/%s", runID), + DebugKey: runID, } mockAPI.MockGetDebugConnectionInfo = func(runId string) (api.DebugConnectionInfo, error) {