I was curious why the new "edit" function in the shell wasn't supported in Windows, so I tried taking out the #ifndef _WIN32 code that is hiding the function and making minor adjustments for Windows. It took 5 minutes to get it working. Windows isn't crazy about editing a file that is still open by the shell, but notepad let me overwrite the file anyway and I got my edited values back in the shell just fine. I think that closing and reopening the file (at least in Windows) is the better way to go rather than letting the editor operate on a file we still have open. I tweaked the filename generation slightly for Windows:
GetTempPathA(sizeof tempFolder, tempFolder);
sb << tempFolder << "mongo_edit" << time(0)+i << ".js";
sb << "/tmp/mongo_edit" << time(0)+i << ".js";
and I had to comment out the use of File to do the delete because "fd" is private in the Windows build, so my hack needs more work. The file isn't deleted with my quick hack, but that's easy to fix if we want to enable this feature in Windows.
// just to make sure this gets closed no matter what
holder.fd = fd; // can't do this in windows because holder.fd is private
I was thinking that I might need to use ShellExecute() to run the program, but ::system() works fine. No #ifdef required.
sb << editor << " " << filename;
int ret = ::system(sb.str().c_str());
int systemErrno = errno;
I set Visual Studio to pass --nodb on the command line and EDITOR=notepad.exe as an environment variable and except for the complaint from notepad that "The file is open in another process" it just worked.
MongoDB shell version: 2.1.0-pre-
> edit f // notepad runs, complains about open file, brings up Save As dialog, asks permission to overwrite, then writes file
> // it worked
- related to
SERVER-3787 'edit' command in shell