Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve open cmd #788

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

Smarticles101
Copy link
Member

This pull request aims to improve the open command.

I start by solving #460, while diving into the open command, I found that it could be improved in a few various ways.

Copy link
Member Author

@Smarticles101 Smarticles101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready for review now, I've made the changes I feel best for consistency with the download command. I'll add tests after it's reviewed to make sure everything is how we want.

cmd/open.go Outdated
continue
}

if meta.Team != teamID {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I tried teamID != "" && meta.Team != teamID, but the current boolean statement is more in line with how the api handles team exercises.

@@ -0,0 +1 @@
package cmd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add tests after this is reviewed, to check that functionality is how we want it

@@ -57,6 +57,16 @@ func (ws Workspace) PotentialExercises() ([]Exercise, error) {
continue
}

if topInfo.Name() == "teams" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add support for teams to PotentialExercises as before this change, it doesn't find them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind extracting this change into a separate PR? (We should probably include tests for the change).

@Smarticles101 Smarticles101 changed the title [WIP] improve open cmd improve open cmd Jan 10, 2019
@kytrinyx
Copy link
Member

I completely failed to keep up with my Exercism notifications, and I'm inexcusably late to the party. To add to this, my laptop crashed and is now in for repair, and as of today I have a loaner that I've not quite gotten set up to be usable. So. My apologies (first) and (second)... my goal is to get through all the open PRs in the CLI track over the course of the next week. So sorry to leave you hanging!

@Smarticles101
Copy link
Member Author

I completely failed to keep up with my Exercism notifications, and I'm inexcusably late to the party. To add to this, my laptop crashed and is now in for repair, and as of today I have a loaner that I've not quite gotten set up to be usable. So. My apologies (first) and (second)... my goal is to get through all the open PRs in the CLI track over the course of the next week. So sorry to leave you hanging!

Don't worry about it too much! Things happen and I'm pretty sure most people here are guilty of ignoring notifications for a long time. Completely understandable

@kytrinyx
Copy link
Member

Hey @Smarticles101 -- overall it looks like what you have here is much better.

In order to help me review this properly, would you summarize exactly what changed, and how this will change the usage of the command?

@Smarticles101
Copy link
Member Author

Smarticles101 commented Feb 26, 2019

@kytrinyx
The old open command worked in a way similar to this:

exercism open ./path/to/exercise/folder

The new command, rather than using an exercise path, follows a style similar to the other commands. Now instead of passing an exercise directory, you pass a slug.

exercism open --exercise slug

I also added support for --team, --track, and --remote from #460 .

@kytrinyx
Copy link
Member

Thank you, that's really helpful!

In order to not break people's work flow we'll need to support the original usage as well, though... at least until we do a major version bump.

@Smarticles101
Copy link
Member Author

@kytrinyx That sounds good, I can add that back in when I have free time tomorrow

@Smarticles101
Copy link
Member Author

@kytrinyx original usage is supported now :)

@Smarticles101
Copy link
Member Author

No idea why the build is failing, I haven't changed anything that should make it fail, and I've rebased against master and compiled it locally and it seems to work?

@kytrinyx
Copy link
Member

original usage is supported now :)

w000p!!!!

No idea why the build is failing

I'll take a look.

cmd/open.go Outdated
return err
}

if meta.Exercise != exerciseID {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be meta.ExerciseSlug to pass the tests (see #815)

Copy link
Member

@kytrinyx kytrinyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good. I have a few suggestions, but overall I think this is doing the right thing!

cmd/open.go Outdated
usrCfg := cfg.UserViperConfig
trackID, _ := flags.GetString("track")
exerciseID, _ := flags.GetString("exercise")
teamID, _ := flags.GetString("team")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know error checking seems tedious, but I'd rather we did it for completion. If it gets really bad we can extract it into a validator type, but for now let's just do it inline.

cmd/open.go Outdated
var url string
usrCfg := cfg.UserViperConfig
trackID, _ := flags.GetString("track")
exerciseID, _ := flags.GetString("exercise")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this exerciseSlug for consistency with the rest of the codebase (we don't currently call it exerciseID anywhere).

cmd/open.go Outdated
usrCfg := cfg.UserViperConfig
trackID, _ := flags.GetString("track")
exerciseID, _ := flags.GetString("exercise")
teamID, _ := flags.GetString("team")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cat we go with just team as the variable name? We don't call this teamID anywhere else.

cmd/open.go Outdated
metadata, err := workspace.NewExerciseMetadata(args[0])
if err != nil {
return err
}
browser.Open(metadata.URL)
return nil
},
//return fmt.Errorf("Must provide an `--exercise`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ✂️ the commented out stuff.

cmd/open.go Outdated
teamID, _ := flags.GetString("team")

if exerciseID == "" {
// if no --exercise is given, use original functionality
metadata, err := workspace.NewExerciseMetadata(args[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about extracting args[0] into a variable path up at the start where we're also extracting from flags?

var path string
if args.length > 0 {
  path = args[0]
}

That way down here we can say if path != "" {

If we want to be really explicit, we could also have a validation on path != "" and exerciseSlug != "" and explain that you need one or the other.

cmd/open.go Outdated
//return fmt.Errorf("Must provide an `--exercise`")
}

if remote, _ := flags.GetBool("remote"); remote {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract the remote flag up top as well, and check the error.

var payload openPayload
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
return fmt.Errorf("unable to parse API response - %s", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use the api error decoder helper?

cli/cmd/cmd.go

Line 78 in c3525cf

func decodedAPIError(resp *http.Response) error {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to decode the json anyways for the solution url. The helper only returns the error. Would it be beneficial to introduce a helper that decodes the entire api response?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Maybe, but let's hold off on that for now.


if res.StatusCode != http.StatusOK {
switch payload.Error.Type {
case "track_ambiguous":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can make the error decoder helper smarter and add this logic into it.

cli/cmd/cmd.go

Line 78 in c3525cf

func decodedAPIError(resp *http.Response) error {

(If so, let's do that in a separate PR and get that into master quickly so we can use it elsewhere).

}
}

url = payload.Solution.URL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's open the URL and return here so that we can out-dent the else.

@@ -57,6 +57,16 @@ func (ws Workspace) PotentialExercises() ([]Exercise, error) {
continue
}

if topInfo.Name() == "teams" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind extracting this change into a separate PR? (We should probably include tests for the change).

@Smarticles101 Smarticles101 force-pushed the development branch 3 times, most recently from 0ef90c5 to 4f9bb94 Compare February 28, 2019 17:42
@Smarticles101
Copy link
Member Author

I made a mess of my git tree today and ended up having to cherry-pick my commits out.

I updated it to use ExerciseSlug now so the build passes. I'll see when I have time to look into your other comments. I should get some time early next week

@kytrinyx
Copy link
Member

kytrinyx commented Mar 3, 2019

@Smarticles101 just a quick note to say that I think the next step here is to rebase onto master before I do another review. If I'm wrong about that please let me know :-)

@kytrinyx
Copy link
Member

@Smarticles101 Is this ready for another round of review?

Copy link
Member

@kytrinyx kytrinyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current logic around path or slug is a bit confusing, so I've suggested an improvement there.

We're also going to need to fix the long description for the command.

return fmt.Errorf("must provide an --exercise slug or an exercise path")
}
// if no --exercise is given, use original functionality
metadata, err := workspace.NewExerciseMetadata(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this would be easier to understand if we did something like this:

if exerciseSlug == "" && path == "" {
  return fmt.Errorf...
}

if path != "" {
  // get metadata and open the url
}

// etc

@Smarticles101
Copy link
Member Author

I know it's been nearly two years, but I thought I would get this finished up. I've addressed the last changes requested. If everything looks good I will write some tests.

@Smarticles101
Copy link
Member Author

@ekingery if you'd like to take a look at this and let me know if you see anything that's off, I'd appreciate it :)

@ekingery
Copy link
Contributor

@ekingery if you'd like to take a look at this and let me know if you see anything that's off, I'd appreciate it :)

Thanks @Smarticles101! We're fully focused on getting configlet updated for V3 right now, but this change looks like something we should be able to get merged as soon as we can bring some focus back to the CLI and get a new version out. It looks like the build is broken at the moment, any ideas on that?

@Smarticles101
Copy link
Member Author

@ekingery yes, like in #957, it fails on older versions of go presumably due to outdated modules. It builds correctly on the latest version

Base automatically changed from master to main January 28, 2021 22:32
@ErikSchierboom
Copy link
Member

This is quite an old PR and has some merge conflicts. @Smarticles101 do you still want to work on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants