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 export command to create subfolders via file format #300

Merged
merged 1 commit into from
Jul 6, 2020

Conversation

simonfrey
Copy link
Contributor

@simonfrey simonfrey commented Jun 25, 2020

This change allows to create subfolders via the --format flag.

In order to allow controlledcreation of subfolders and not accidentally do it when there is a / in the format the new flag --allow-subfolder-creation needs to be set to enable this feature.

Without setting the flag the behavior of the command is equal to before.

Edit: Thanks to @captncraig this uses a template function for format now instead of the less secure way with the flag

Copy link
Member

@sh0rez sh0rez left a comment

Choose a reason for hiding this comment

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

Hey that's super cool! Thanks for you contribution.

I think a slightly shorter flag name would make this a bit easier to use (see above) :)

cmd/tk/export.go Outdated Show resolved Hide resolved
cmd/tk/export.go Outdated Show resolved Hide resolved
cmd/tk/export.go Outdated Show resolved Hide resolved
@captncraig
Copy link
Contributor

Hah! I did this myself in a local branch to test things. I like the idea of a flag to enable it. But I can also see why it is dangerous in the first place. Tanka runs the replace, probably to cover things like labels or whatnot having slashes and getting put into file paths.

So is there a way to allow explicit path breaks in a format sting, without accidentally getting path breaks from malformed data inside the templates?

My dirty solution was to use ~ as an indicator in a format string that you want a path break there, and do the replace like so:

name := strings.Replace(buf.String(), "/", "-", -1)
name = strings.Replace(name, "~", string(os.PathSeparator), -1)

To add safety, perhaps a template function to put in some even more unlikely string so you don't accidentally use it. Maybe something like that would allow this functionality without a new flag?

@simonfrey
Copy link
Contributor Author

Ah thats a great idea with the template function. Will check it out and report later on

@simonfrey
Copy link
Contributor Author

simonfrey commented Jun 25, 2020

Awesome that works even better and is super intuitive :D Just insert the {{mkdir}} into the template. I use rune(7) that is the no printable bel character and thereby should never be in in any string.

cmd/tk/export.go Outdated Show resolved Hide resolved
cmd/tk/export.go Outdated Show resolved Hide resolved
cmd/tk/export.go Outdated Show resolved Hide resolved
@simonfrey
Copy link
Contributor Author

Anything else left to do?

@sh0rez
Copy link
Member

sh0rez commented Jul 6, 2020

Hey @simonfrey, sorry for the delay here.

I talked about this with @malcolmholmes and we both feel the mkdir part feels a bit unnatural inside of the template.

What he suggested instead, is to replace all occurences of / in the template before evaluation with rune(7). Then running the template, you can replace all returned slashes with - (like it is already), making sure no directories are created accidentally.

Finally, you replace all rune(7) with / again. This way, explicit slashes are used as directories, all other are replaced by dashes.

Let me know if that sounds good to you as well, happy to discuss :)

@simonfrey
Copy link
Contributor Author

Tried it and it feels more natural. Added the change

Copy link
Member

@sh0rez sh0rez left a comment

Choose a reason for hiding this comment

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

Looking good! Some nits and we are good to go :)

cmd/tk/export.go Show resolved Hide resolved
docs/docs/exporting.md Outdated Show resolved Hide resolved
@sh0rez sh0rez merged commit 5bc160a into grafana:master Jul 6, 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.

None yet

3 participants