Go command line tool : how to launch a text editor and grab the user input ?

1,484 views
Skip to first unread message

joseph...@gmail.com

unread,
Mar 10, 2014, 6:03:11 PM3/10/14
to golan...@googlegroups.com
Hello

I would like to create a command line tool where the user can add comment "the git way", ie the workflow for a command line tool named "tool" would be the following:
1. The user types the following on the command line:
$> tool answer
2. The software opens the default text editor (or vi as a fallback)
3. the user types his text
4. on the user leaving the text editor, the input is taken by the software

It's basically a simple "git commit"

Is it possible and if so how ? My trials using the package Exec did all failed...

Thanks in advance

Joseph

Gabriel Comeau

unread,
Mar 10, 2014, 6:12:18 PM3/10/14
to joseph...@gmail.com, golan...@googlegroups.com
While I'm not familiar with the git internals I've seen enough evidence of this to suspect this is how they do it:

1)  exec the text editor with a filename in place (vim $PROJECT_ROOT/.git/COMMIT_EDITMSG is what git looks to be doing, evidenced by the fact that there's a COMMIT_EDITMSG.swp while the message is still open)
2)  Once the exec is over (the user saved and quit from the editor) read the file and get your input from there.  Handle errors where the file is empty / doesn't exist (aborted commit)
3)  Once you've validated the input, you should probably delete the temp file.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joseph Pachod

unread,
Mar 10, 2014, 6:13:29 PM3/10/14
to Gabriel Comeau, golan...@googlegroups.com
Hi all

Stupid me, that was my original plan but somehow I forgot on the way while digging into Go...

Thanks a lot, back to coding ! :)

joseph...@gmail.com

unread,
Mar 10, 2014, 7:01:15 PM3/10/14
to golan...@googlegroups.com
Hi

End of coding for tonight, current state at https://gist.github.com/cluelessjoe/9476219
doesn't work currently :
./main
vim is available at /usr/bin/vim
Calling it with file /var/folders/4r/b51g0pl93sv6gdgx7vxh7r8m0000gp/T/tempFilePrefix283772469
Waiting for command to finish...Command finished with error: exit status 1

If anyone can help, great, otherwise digging in again tomorrow night :)

++
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Martin Hilton

unread,
Mar 10, 2014, 7:14:20 PM3/10/14
to joseph...@gmail.com, golang-nuts
You need to wire in the standard file descriptors.

e.g. http://play.golang.org/p/ixS9g4RveQ


To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

joseph...@gmail.com

unread,
Mar 11, 2014, 4:23:42 PM3/11/14
to golan...@googlegroups.com, joseph...@gmail.com
It works, thanks a lot :)
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages