Skip to content

Commit

Permalink
Uses the new debug endpoint (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
TAGraves authored Feb 15, 2024
1 parent a5acf2f commit 17772c3
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.20
4 changes: 2 additions & 2 deletions cmd/mint/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 9 additions & 4 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions internal/api/debug_connection_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ type DebugConnectionInfo struct {
PublicHostKey string `json:"public_host_key"`
PrivateUserKey string `json:"private_user_key"`
}

type DebugConnectionInfoError struct {
Error string
}
6 changes: 3 additions & 3 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 2 additions & 13 deletions internal/cli/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 17772c3

Please sign in to comment.