Local Community

How do I run wp-cli without clicking Open Site Shell?

Issue Summary

I realize I can open a project on the command line by clicking Open Site Shell in the Local GUI window; however, how can I do the same from the command line directly?

My use case: I like to open my theme in VS Code, then when I want to run a wp-cli command I click control+~ to bring up VS Code’s terminal. Right now when I want to do that, I’m literally opening the Local GUI, clicking Open Site Shell which opens a new iTerm window, copying the command it runs (something like /Users/myusername/Library/Application\ Support/Local/ssh-entry/0A1vMAn2f.sh; exit), closing the iTerm window Local opened, going back into VS Code, pasting and executing that command, after which wp-cli commands work within VS Code. Another common use case is when I’m using an iTerm window Local opened, but want to split the terminal into several windows with command+d or command+shift+d: it’d be nice to run a command that set’s up Local in that new window pane, but currently I just scroll all the way up in the original pane looking for that ssh-entry command and re-run it so I can execute wp-cli in the new pane.

Obviously, that’s a bit cumbersome: is there a command I can run from within a theme folder in any terminal window that executes Local’s command line setup?

Or, even if there’s a way to detect that “0A1vMAn2f” Local server name from the command line, I could create a command myself to accomplish what I’m after—I’ve just been unable to find “0A1vMAn2f” written anywhere within the app, conf, or logs directories that I could create a shell command to grab and use.

Thanks for a great product!

All the environment setup script functionally does is export the path to the lightning services in Local so those tools are available. The alternative I came up with is to just install the tools globally on my Mac. I use Homebrew, so these were…

brew install php
brew install mysql
brew install wp-cli

You can then teach the wp-cli what WP site to go at in certain folder structures by including these files at the root of your Local site:

wp-cli.local.yml

path: app/public
require:
  - wp-cli.local.php

wp-cli.local.php

<?php
define('DB_HOST', 'localhost:/Users/myusername/Library/Application Support/Local/run/gC3BGRlZX/mysql/mysqld.sock');

You get the socket path, which also does include the site ID, from the Database tab in the Local GUI.

The biggest downside of this is that without careful version management the versions of, say, PHP, will likely be out of sync. But that hasn’t really bitten me. It is much nicer to be able to just use any old Terminal (yes, lots of the time for my work that is also VS Code’s Integrated Terminal) to perform anything on my Local sites.

I also have built my own wrapper for the local-cli, which can show you how to retrieve the site ID based on folder you are in.

Hi @alexclst! Though I’m not sure that’s a great solution for myself (I sometimes switch back/forth between PHP versions to work on old sites, and have several local server tools other than Local that may be affected by this process), I think you put me on the right track with your local-cli wrapper’s access of /Users/myusername/Library/Application\ Support/Local/sites.json to determine the ID from the folder path—bet I can build something for myself that does similar. Thanks!

@aquamarine I’m glad that my wrapper may be useful sample code for you!

I’ll refine this later, but for now I installed jq and I’m just running:

ids=(cat /Users/"$USER"/Library/Application\ Support/Local/sites.json | jq -r '.[].id’);domain=(cat /Users/"$USER"/Library/Application\ Support/Local/sites.json | jq -r ‘.[].domain’);length=${#ids[@]};for (( j=0; j<length; j++ )); do printf "\n%s:\nbash /Users/"$USER"/Library/Application\ Support/Local/ssh-entry/%s.sh;\n" "${domain[$j]}" "${ids[$j]}"; done;

which gives me a big list of script paths I can copy/paste. Not elegant but it saves me from leaving the command line :+1: