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

tink template create now accepts data from STDIN #133

Merged
merged 3 commits into from
Jun 10, 2020
Merged

tink template create now accepts data from STDIN #133

merged 3 commits into from
Jun 10, 2020

Conversation

gauravgahlot
Copy link
Contributor

@gauravgahlot gauravgahlot commented May 26, 2020

At present, we create a template as shown below:

$ tink template create --name example --path /tmp/example.tmpl 

With the code changes in place, a user can now pipe the template data from a file. For example, if the data is saved in /tmp/example.tmpl, you can create a template with either of the following:

$ cat /tmp/example.tmpl | tink template create -n example
$ tink template create --name example --path /tmp/example.tmpl

Signed-off-by: Gaurav Gahlot [email protected]

@gauravgahlot gauravgahlot requested a review from grahamc May 26, 2020 05:11
@gauravgahlot gauravgahlot self-assigned this May 26, 2020
@gauravgahlot gauravgahlot linked an issue May 26, 2020 that may be closed by this pull request
@gauravgahlot gauravgahlot added the kind/feature Categorizes issue or PR as related to a new feature. label May 26, 2020
@@ -36,40 +57,55 @@ func addFlags() {
flags.StringVarP(&filePath, "path", "p", "", "path to the template file")
flags.StringVarP(&templateName, "name", "n", "", "unique name for the template (alphanumeric)")

createCmd.MarkPersistentFlagRequired(fPath)
// createCmd.MarkPersistentFlagRequired(fPath)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should just remove this commented line as its not used anymore.

@rgl
Copy link
Contributor

rgl commented May 26, 2020

--path /dev/stdin

You should remove this from the example that is on this PR description as there is no need to specify the path when it reads from stdin.

@gauravgahlot
Copy link
Contributor Author

--path /dev/stdin

You should remove this from the example that is on this PR description as there is no need to specify the path when it reads from stdin.

I would prefer to keep it to make sure that people know about this.

@rgl
Copy link
Contributor

rgl commented May 27, 2020

--path /dev/stdin

You should remove this from the example that is on this PR description as there is no need to specify the path when it reads from stdin.

I would prefer to keep it to make sure that people know about this.

If you want to be able to use --path with stdin you should also implement the de-facto-standard way of configuring the application to read from stdin or write to stdout by using - as a path. For example, in wget (and tar etc) you do wget -O - example.com to write to stdout.

This because I'm not sure if all unices have a /dev/stdin and /dev/stdout devices, but if Tinkerbell is Linux only, we are good with that :-)

}
return data
}

func createTemplate(c *cobra.Command, args []string) {
req := template.WorkflowTemplate{Name: templateName, Data: readTemplateData()}
func createTemplate(c *cobra.Command, data []byte) {
Copy link
Contributor

Choose a reason for hiding this comment

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

While we're at it, I think we should reduce the globals used in this function and pass templateName as a parameter to the function to reduce spooky action. Could we do the same for filePath in readTemplateData?

Copy link
Contributor Author

@gauravgahlot gauravgahlot Jun 2, 2020

Choose a reason for hiding this comment

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

AFIK, since templateName and filePath are value receivers for the flags, they have to be global. But if there is a better way to do that, please do let me know.

Comment on lines 40 to 44
if isInputFromPipe() {
data = readDataFromStdin()
} else {
data = readTemplateData()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we reversed this logic a bit, and instead got an io reader:

path, _: c.Flags().GetString(fPath)


if path != "" {
  reader, err = os.Open(filePath)
  ...
} else {
  reader = os.Stdin
}

and then just readAll(reader)? As it stands, it will fairly eagerly read the input from stdin even if I've passed a file.

Signed-off-by: Gaurav Gahlot <[email protected]>
Signed-off-by: Gaurav Gahlot <[email protected]>
Signed-off-by: Gaurav Gahlot <[email protected]>
@gauravgahlot gauravgahlot requested a review from parauliya June 10, 2020 06:28
@gauravgahlot gauravgahlot removed the request for review from parauliya June 10, 2020 14:47
@gianarb
Copy link
Contributor

gianarb commented Jun 10, 2020

It looks good to me I don't have anything to add other what got already adressed

@grahamc grahamc added the ready-to-merge Signal to Mergify to merge the PR. label Jun 10, 2020
@mergify mergify bot merged commit 6a9ed8a into tinkerbell:master Jun 10, 2020
@mmlb mmlb removed the ready-to-merge Signal to Mergify to merge the PR. label Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/tink-cli kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tink template create should also accept STDIN
5 participants