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

Allow specifying a StringSlice destination for StringSliceFlag #1078

Merged
merged 1 commit into from
Feb 29, 2020
Merged

Allow specifying a StringSlice destination for StringSliceFlag #1078

merged 1 commit into from
Feb 29, 2020

Conversation

davidsbond
Copy link
Contributor

@davidsbond davidsbond commented Feb 28, 2020

What type of PR is this?

  • bug
  • cleanup
  • documentation
  • feature

What this PR does / why we need it:

Adds a Destination field to the StringSliceFlag of type StringSlice so that we can use a variable to access the value rather than calling c.StringSlice. This adds some additional type safety and can help to avoid scenarios where you have typos calling the c.StringSlice("name") method.

Which issue(s) this PR fixes:

Fixes #1075

Testing

By running the code I've seen that the Destination gets set, I've also added in some tests for setting the destination value from env and from flags

Release Notes

Adds destination field to StringSliceFlag

@davidsbond davidsbond requested a review from a team as a code owner February 28, 2020 11:20
@codecov
Copy link

codecov bot commented Feb 28, 2020

Codecov Report

Merging #1078 into master will increase coverage by 0.07%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1078      +/-   ##
==========================================
+ Coverage   72.83%   72.91%   +0.07%     
==========================================
  Files          33       33              
  Lines        2474     2481       +7     
==========================================
+ Hits         1802     1809       +7     
  Misses        565      565              
  Partials      107      107              

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 46c380b...e19b156. Read the comment docs.

@davidsbond
Copy link
Contributor Author

I've found a bug in this code so far. The problem is that when you provide both environment variables and flags, you end up appending to the array instead of replacing one with the other. For example:

Environment: [a, b, c]
Flags: [1, 2, 3]
Result: [a,b,c,1,2,3]

@davidsbond
Copy link
Contributor Author

I think I've solved it, but I'm going to do some more testing.

@davidsbond
Copy link
Contributor Author

Based on my local testing this now works as expected, flags will override the slice set by env vars entirely.

@davidsbond
Copy link
Contributor Author

package main

import (
	"fmt"
	"github.com/davidsbond/cli"
	"log"
	"os"
)

var (
	dest = &cli.StringSlice{}
)

func main() {
	os.Setenv("TEST_THIS_THING", "1,2,3")

	app := cli.NewApp()
	app.Action = run
	app.Flags = []cli.Flag{
		&cli.StringSliceFlag{
			Name:        "test",
			Destination: dest,
			EnvVars:     []string{"TEST_THIS_THING"},
		},
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}

func run(c *cli.Context) error {
	fmt.Println(dest.Value())
	return nil
}
go run main.go --test=a,b,c
[a,b,c]
go run main.go  
[1 2 3]

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.

I don't have any particular opinions about the env var handling, but everything else LGTM! 🚢

@coilysiren coilysiren merged commit 573004a into urfave:master Feb 29, 2020
@coilysiren coilysiren mentioned this pull request Mar 9, 2020
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.

v2 feature: Add Destination for StringSliceFlag
3 participants