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

add context.Context to cli.Context #840

Merged
merged 8 commits into from
Aug 6, 2019
Merged

Conversation

marwan-at-work
Copy link
Contributor

This is a naive implementation of attaching context.Context to the *cli.Context

The goroutine that waits for a kill signal hangs forever. I imagine that's okay since running a cli command does not hang forever. Otherwise, I'd be happy to add a way to end the goroutine.

Fixes #839

go.mod Outdated
@@ -0,0 +1,3 @@
module gopkg.in/urfave/fli.v2
Copy link
Contributor

Choose a reason for hiding this comment

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

Fli? Also, why gopkg.in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius ops, will fix that. What import path should it be? The README for v2 says use gopkg.in which is why I added that

Copy link
Contributor

@AudriusButkevicius AudriusButkevicius Aug 5, 2019

Choose a reason for hiding this comment

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

I guess this needs a wider discussion on how we deal with this going forward. With go.mod support I think gopkg.in is obsolete, and people just go get github.com/urfave/cli@2.0.0

We can keep releasing 1.0.0 and 2.0.0 idependently.

ping @lynncyrin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius github.com/urfave/cli@2.0.0 would not work. Per "Semantic Import Versioning" it would have to be github.com/urfave/cli/v2 and similarly go get github.com/urfave/cli/v2@v2.1.9 and so on.

Note that Go Modules has a special handling of Gopkg.in so it should work okay. That said, I'm happy to remove the go.mod file from this PR and open a new issue for what the import path should be/

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would @2.0.0 not work, I was under the assumption that just resolves the release tag. Not sure why it needs to move to a separate subpackage to work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius go get github.com/urfave/cli will always fetch v1, and go get github.com/urfave/cli/v2 will always fetch v2 and so on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Use, unless we merge v2 to master and roll with that. I don't think two versions of the library should exist in the same repo basically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius Go Modules does not care about what's in master, when you have semver tags. It will basically look at git tags and determine what version to get you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius for this PR however, should I just remove the go.mod file? Or should I change it to be github.com/urfave/cli/v2?

Copy link
Contributor

Choose a reason for hiding this comment

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

No idea, thats why I want someone else from @urfave/cli to comment and drive the direction of the migration.

context.go Outdated Show resolved Hide resolved
context.go Outdated Show resolved Hide resolved
context.go Outdated
if parentCtx != nil {
if parentCtx.Context != nil {
parentCtx.Context = context.Background()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This has a side effect on the parentCtx passed in, which I as a user would find suprising.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I think the intended condition in the if was == nil it seems?

I think this needs tests, as we can't rely on reviewers to prove this works

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius this is just to make sure the context is never nil, so that we never panic. Should I remove it and leave the risk up to the user?

Copy link
Contributor

@AudriusButkevicius AudriusButkevicius Aug 6, 2019

Choose a reason for hiding this comment

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

It's not true, the if statement is wrong and it always overrides it if its NOT nil, and leaves it nil otherwise. I think we should not cause side effects on objects given to us, so if its nil, create a new one locally, and then validate this behaviour with a few tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius great catch, that was a mistake on my part. I will change != nil to == nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius i also added some propagation tests to ensure the parent context does not get overridden.

Copy link
Contributor 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 sigterm tests, but i'm not sure how I can send a sigterm signal to the process, without killing the tests themselves.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still think we should not mutate the parent context.

c.Context = parentCtx.Context

Then outside the if (oppose to else):

If c.Context == nil {
  // Create cancel context, spin up signal handler ctx.
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

app_test.go Outdated
@@ -503,7 +503,6 @@ func TestApp_Float64Flag(t *testing.T) {
}

func TestApp_ParseSliceFlags(t *testing.T) {
var parsedOption, firstArg string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the Go command complained that these were declared and not used

Copy link
Contributor

Choose a reason for hiding this comment

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

These are not used but they do have side effects/assert evaluation logic so should probably be called?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius the code didn't even compile though, so im not sure how that's supposed to work? In any case, I can just add var _ = parsedOption below

Copy link
Contributor

Choose a reason for hiding this comment

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

Its not the variable declarations, its the calls that assign to the unused variables is what matters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius again, the code doesn't even compile and tests don't run :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because we're just re-assigning those variables, but not really doing anything with them. It could be that go1.8 (what travis is using) didn't care, but Go 1.13 is smarter.

ctx := NewContext(nil, nil, nil)
if ctx.Context == nil {
t.Fatal("expected a non nil context when no parent is present")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be a separate test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius I made this into a separate test

context_test.go Outdated
}
parent := NewContext(nil, nil, nil)
parent.Context = context.WithValue(context.Background(), "key", "val")
ctx = NewContext(nil, nil, parent)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about a test where parent.Context is nil, but parent not nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AudriusButkevicius i added that scenario to the bottom of this test.

go.mod Outdated Show resolved Hide resolved
@AudriusButkevicius
Copy link
Contributor

lgtm

@coilysiren coilysiren mentioned this pull request Aug 6, 2019
Copy link
Member

@coilysiren coilysiren left a comment

Choose a reason for hiding this comment

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

👍 LGTM!

@coilysiren
Copy link
Member

Oh rather, LGTM pending the tests passing 🙂

@marwan-at-work
Copy link
Contributor Author

@lynncyrin I'm not sure why the build is failing. It has to do with the python script which im not familiar with. Can you please point me in the right direction to fix this? Thanks

@AudriusButkevicius
Copy link
Contributor

I think the test is a red herring, we need to bump travis version.

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.

3 participants