Jump to content
corsonr

WP CLI Workflow nearly working (WordPress) ;-)

Recommended Posts

Hi guys and gals!

 

First of all I'd like to thank you all for the past workflows I downloaded and for all the help I found on this forum! ;-) Please note i'm a newbie in workflows and bash stuff!

 

I am working on a workflow for WP-CLI (http://wp-cli.org/). The aim of that script is to easily install a WordPress site in the following folder Applications/MAMP/htdocs/Dropbox. It has two parameters, the site slug and the site name.

 

The problem I'm facing is that if I run the script from the Terminal, it works fine, but from the workflow it fails.

 

What I have done:

- installed WP-CLI (it works from the Terminal)

- created a bash script

- placed that .sh file in Applications/MAMP/htdocs/Dropbox

- placed a .bash_profile file in users/remi/

- created the workflow to load the .sh file

 

The .bash_profile file content is (required when using MAMP):

 

# FIX PHP MAMP for WP-CLI
export PATH=/Applications/MAMP/bin/php/php5.6.1/bin:$PATH
export PATH=$PATH:/Applications/MAMP/Library/bin/

 

What works:

- in the terminal the following command does work:

cd Applications/MAMP/htdocs/Dropbox
bash myscript.sh sitename "My WP Blog"

It downloads latest WordPress version, create the sitename folder in Applications/MAMP/htdocs/Dropbox, etc... in other words, that works!

 

What fails:

- launching the myscript.sh file from Alfred 2 workflow.

- the error message that I get in the debug console of Alfred is:

[ERROR: alfred.workflow.action.script] Code 127: /Applications/MAMP/htdocs/Dropbox/myscript.sh: line 97: wp: command not found

Here is my workflow content:

bash /Applications/MAMP/htdocs/Dropbox/myscript.sh {query}

And here is the beginning of my myscript.sh file:

 

#!/bin/bash
#
# How to launch script ?
# bash myscript.sh sitename "My WP Blog"
# $1 = folder name & database name
# $2 = Site title


# VARS 
# admin email
email="my@email.com"


# local url login
# --> Change to fit your server URL model (eg: http://localhost:8888/my-project)
url="http://"$1"/"


# admin login
admin="admin-$1"


# path to install your WPs
pathtoinstall="/Applications/MAMP/htdocs/Dropbox/"


# path to plugins.txt (not accurate, but the script fails before it's used!)
pluginfilepath="/Desktop/plugins.txt"


# end VARS ---








#  ===============
#  = Fancy Stuff =
#  ===============
# not mandatory at all


# Stop on error
set -e


# colorize and formatting command line
# You need iTerm and activate 256 color mode in order to work : http://kevin.colyar.net/wp-content/uploads/2011/01/Preferences.jpg
green='\x1B[0;32m'
cyan='\x1B[1;36m'
blue='\x1B[0;34m'
grey='\x1B[1;30m'
red='\x1B[0;31m'
bold='\033[1m'
normal='\033[0m'


# Jump a line
function line {
  echo " "
}


# Sript has something to say
function bot {
  line
  echo -e "${blue}${bold}( script says )${normal}  $1"
}




#  ==============================
#  = The show is about to begin =
#  ==============================


# Welcome !
bot "${blue}${bold}Hi!${normal}"
echo -e "         I am about to install the following WordPress site : ${cyan}$2${normal}"


# CHECK :  Directory doesn't exist
# go to wordpress installs folder
# --> Change : to wherever you want
cd $pathtoinstall


# check if provided folder name already exists
if [ -d $1 ]; then
  bot "${red}Folder ${cyan}$1${red} already exists${normal}."
  echo "         I am not going any further!"
  line


  # quit script
  exit 1
fi


# create directory
bot "Creating folder : ${cyan}$1${normal}"
mkdir -p -m 777 $1
cd $1


# Download WP
bot "I am downloading WordPress..."
wp core download --force


// and the script continues, but it fails at 1st wp call

If I run the workflow, only the sitename folder is created in Applications/MAMP/htdocs/Dropbox and the first wp occurence breaks the process.

 

So, i'm definitely missing something, but I don't know why. Any inputs on this would be highly appreciated. Of course I'll share the full workflow here, once it will be working. I'm open to any ideas and criticism! ;-)

Edited by corsonr

Share this post


Link to post

"If it works in a script but doesn't work in Alfred, it's always something about the Path settings. Every time."

 

http://www.alfredforum.com/topic/4959-enoent-path-issues-bash-nodejs/?hl=%2Bbash+%2Bpath

 

I'd take a look at the above - and search this forum for bash path - this comes up a lot and is probably a candidate for a FAQ.

 

From the error message you're getting I'd be pretty sure that the script when run from Alfred doesn't know the path to wp.

Edited by dfay

Share this post


Link to post

@dfay is 100% correct. When you try to run the wp command in line 97 of your script from Alfred, Alfred is running the script in a totally sanitized environment, where installed external command line tools don't exist.

To make it work, you can try to different typically approaches. First, export $PATH:... before the bash /Applications/MAMP/htdocs/Dropbox/myscript.sh {query} call in Alfred. This will set the PATH variable to whatever you put, and then allow for the relative call to wp that you make in line 97. Second, (my preference), make the call to wp explicit. So it would be something like this in line 97: /usr/bin/local/wp core download --force. This is assuming that the wp executable lives in /usr/bin/local. Wherever it lives, put the full explicit path.

Either way, you need to avoid any $PATH magic when working with Alfred.

Share this post


Link to post

Hi @dfay and @smarg19,

 

Yes it seems that you are right, after a talk on Twitter with Jonas Nordstrom (@windyjonas) I found the answer. So, I added the following lines to my myscript.sh:

 

export PATH=/Applications/MAMP/bin/php/php5.6.1/bin:$PATH
export PATH=$PATH:/usr/local/bin:/Applications/MAMP/Library/bin
 
And that worked. So, yes you were right, it was a matter of path.
 
Many thanks for your participation in this topic, it's much appreciated. If you need any help on WordPress/WooCommerce related stuff just let me know, i'll be pleased to help you ;-)

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...