Jump to content
Ian Robert Douglas

Trying to construct a command with two input variables

Recommended Posts

First, I really apologise for how incompetent I am. Without Google, I'd never be able to do anything. But even with Google, I fail. Basic principles, var, query, just fly right over my head. Doesn't matter how much I concentrate, and how much I read, I just never seem to get it :(

 

I'm trying to construct an Alfred workflow to do the following:

 

I want to create a command consisting of three elements:

1. wp-clone

2. site1

3. site2

 

#1 is constant. #2 and #3 are variables. They are basically source-folder, destination-folder. The names would change in real use. I'm just using "site1" and "site2" here to illustrate.

 

The final command, which I'd like to pass to iTerm.app, would look like this:

 

wp-clone site1 site2

 

So what would be ideal is if I can:

1. Pull up the Alfred window (option-space) and start typing "Valet Clone". Press return and it auto-completes (like a keyword would)

2. On return a space inserts and I enter the first argument, where I input "site1" (without quotes: it's basically a directory name)

3. Return again and I get to insert another input, "site2" (again, without quotes: the second directory)

4. Return again and these two variables or arguments are put together and formed as the command:

 

wp-clone site1 site2

It would be great, at that point, if I can run this in the background.

 

Something like:

export PATH=/usr/local/bin:$PATH

cd /Users/Decline/SERVER/ && wp-clone {query1} {query2}

(I would need PATH included as "wp-clone" is a command Alfred's command line wouldn't recognise.)

However, I don't mind if the next stage is simply to open iTerm.app and paste the combined command there.

 

The big problem I have is knowing how on earth to get those two variables (site1 and site1) into a single command, and in an elegant way in Alfred's GUI.

 

I just can't wrap my head around it :(

 

Attached is an attempt, which doesn't work at all, to hack something together in a workflow.

 

Can someone point me in the right direction?

 

Please, any input, imagine you're speaking to a small child, or a Golden Retriever.


Many thanks in advance,
ian

 

My lame attempt (junk, doesn't work): Valet Clone Alfred Workflow

 

Share this post


Link to post

Welcome @Ian Robert Douglas,

 

Don’t beat yourself up so hard; programming is already hard enough. There are moments when concepts click in our heads, but even then it’s still hard.


Try this modified Workflow. It’s untested but it should work (famous last words). It’s simple enough that you can prod at will and may be able to understand everything, but if you don’t, ask!

Share this post


Link to post
1 hour ago, Ian Robert Douglas said:

(I would need PATH included as "wp-clone" is a command Alfred's command line wouldn't recognise.)

 

In most cases, you  can use the full path, i.e. /usr/local/bin/wp-clone, instead of tinkering with PATH.

Share this post


Link to post

@vitor Thank you for your kind words. The modified workflow is perfect! Thank you so very much! It's exactly what I needed.

 

@deanishe Thank you for the reply. The "wp-clone" script I'm using includes commands for Valet+, a fork of Valet for Laravel. It also includes commands for other parts of my usual CLI environment, including mysql. I don't really understand the hierarchy involved, and how to get Alfred's CLI to digest and understand the script to include its parameters if the full path to the script is referenced, instead of PATH. The wp-clone script is in my PATH, so when I run that script in iTerm.app, valet commands, for example, are already pre-loaded or accessible. But running the same script under Alfred's CLI, and only referencing the full path to the script, would that not leave Alfred's CLI unable to understand valet commands?

 

In fact, I'm not even sure how to reference the full path to the script alone. Yep, I'm that ignorant! :(

 

Right now, in the workflow, I'm using:

source /Users/Decline/.zshrc
cd /Users/Decline/SERVER/ && wp-clone "${source_dir}" "${destination_dir}"

But as you said, I think, on another thread I was reading, this is not exactly efficient, as it is including all parts of my PATH for Alfred to digest, slowing the process down from an optimal speed.

 

The workflow does work, nonetheless.

 

I tried this:

source /Users/Decline/local/bin/wp-clone/wp-clone
cd /Users/Decline/SERVER/ && wp-clone "${source_dir}" "${destination_dir}"

Not sure if that is correct or not, in terms of the first line.

 

It errors out with the following:

[16:53:09.566] ERROR: Valet Clone - COPY[Run Script] usage: mkdir [-pv] [-m mode] directory ...
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
/Users/Decline/local/bin/wp-clone/wp-clone:14: command not found: wp
sed: wp-config.php: No such file or directory
/Users/Decline/local/bin/wp-clone/wp-clone:25: command not found: wp
/Users/Decline/local/bin/wp-clone/wp-clone:28: command not found: wp
rm: /Users/Decline/SERVER//.sql: No such file or directory
/Users/Decline/local/bin/wp-clone/wp-clone:34: command not found: wp
/Users/Decline/local/bin/wp-clone/wp-clone:37: command not found: valet
/Users/Decline/local/bin/wp-clone/wp-clone:40: command not found: valet
/Users/Decline/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/DEF04494-FD56-4A95-B679-46FDE3659389:3: command not found: wp-clone

 

 

 

 

 

Share this post


Link to post
2 minutes ago, Ian Robert Douglas said:

It also includes commands for other parts of my usual CLI environment, including mysql.

 

Yeah. Hence my "in most cases" rider. If you're running a script that calls other scripts that also aren't on the standard PATH, you will have to change PATH.

 

4 minutes ago, Ian Robert Douglas said:

But as you said, I think, on another thread I was reading, this is not exactly efficient

 

Importing your shell rc file may be inefficient, and it may not work. The reason is that a lot of config files have a clause like this that causes them to not do anything when they aren't loaded in an interactive shell (which is the case here):

[[ -z "$PS1" ]] && return

 

If you're using zsh, it bears mentioning that a common thing to do is to set PATH in ~/.zshenv, which is always loaded by zsh.

 

But all in all, the safest solution is to make sure any changes to the default environment that your script requires (adding /usr/local/bin to PATH, setting config variables etc.) are done in your workflow's script.

 

9 minutes ago, Ian Robert Douglas said:

source /Users/Decline/local/bin/wp-clone/wp-clone

 

10 minutes ago, Ian Robert Douglas said:

Not sure if that is correct or not, in terms of the first line.

 

Usually not, no. Don't do this unless you know it's a file that's meant to be sourced. source basically includes the file in the one that's currently running. If you source files that are meant to be run as commands, apart from not working, you might also get some nasty surprises.

 

Share this post


Link to post

As a separate issue, when using this code:

source /Users/Decline/.zshrc
cd /Users/Decline/SERVER/ && wp-clone "${source_dir}" "${destination_dir}"

everything works except one thing. wp-clone should be generating OpenSSL Certificates for the cloned site, but it doesn't. It does in iTerm.app.

 

I see this in the debug window of the Alfred Workflows interface:

sudo: no tty present and no askpass program specified
sudo: no tty present and no askpass program specified

It's referenced twice, I think, because the last two commands of the script are valet commands, each of which in iTerm.app asks for your user password.

 

The first, "valet secure", generates OpenSSL Certificates for the newly created site, for use with https.

 

The second, "valet open", opens the new site in the default web browser.

 

A little unnecessary to ask the user password for either operation, and I will look into that and see if that can be disabled or pre-authorised somewhere, but in the meantime, is there anything I can add to the workflow script that would authorise everything, so that part of the script didn't fail?

Share this post


Link to post
16 minutes ago, Ian Robert Douglas said:

It's referenced twice, I think, because the last two commands of the script are valet commands, each of which in iTerm.app asks for your user password.

 

That's exactly why. That's what "no tty present" means.

 

18 minutes ago, Ian Robert Douglas said:

A little unnecessary to ask the user password for either operation

 

It may not be strictly necessary, but it is correct to default to secure behaviour.

 

19 minutes ago, Ian Robert Douglas said:

but in the meantime, is there anything I can add to the workflow script that would authorise everything, so that part of the script didn't fail?

 

No. Either add the appropriate options to your Valet command to suppress the password request, or use an Alfred Terminal Command, so the command is run interactively in Terminal.app/iTerm2.

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...