Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'all' Command for Sequential Execution of All Commands #435

Closed
m18unet opened this issue Sep 6, 2023 · 3 comments
Closed

Implement 'all' Command for Sequential Execution of All Commands #435

m18unet opened this issue Sep 6, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@m18unet
Copy link

m18unet commented Sep 6, 2023

Description

Hello,

I have 3 commands named pod, service, pvc. I want to add a fourth command called all. When I execute all I want the other 3 commands to be executed in order.

Is that possible?

@m18unet m18unet added the enhancement New feature or request label Sep 6, 2023
@DannyBen
Copy link
Owner

DannyBen commented Sep 6, 2023

Yes it is possible, but probably not in the way you think.

Implementing such a solution in a generic way, would be problematic. There are arguments, and flags, and possibly other requirements for each of your commands - so what will the all command do? How will it supply these arguments? How do you ensure that the requirements for all the "child" commands are met?

So - you have several options:

Option 1: Call your other commands by re-running the CLI

Your all command can simply run your own command, as you run any other linux command. Assuming we have build and deploy command, and now a new all command:

# src/all_command.sh
./cli build --force
./cli deploy eu-west
# or $0
$0 build
$0 deploy eu-west

In real life, your script will probably be installed in the PATH (/usr/local/bin/your-script), so you should call it as you call any other linux command.

Option 2: Use functions

If you are familiar with Model-View-Controller architecture, treat the code that you write in the src/*_command.sh as if it is a controller code. It means, it should be a "dumb" short piece of code, that just calls other functions (a "model" stand in). Then, your all command can also call these functions.

For example:

The basic commands

# src/build_comnmand.sh
build_project

# src/deploy_command.sh
region=${args[region]}
deploy_project "$region"

Functions

# src/lib/build_project.sh
build_project() {
  echo "BUILDING..."
}

# src/lib/deploy_project.sh
deploy_project() {
  region="$1"
  echo "DEPLOYING TO $region..."
}

The all command

# src/all_command.sh
build_project
deploy_project "eu"
# or, if your `all` command also has arguments
deploy_project "${args[region]}"

Does this help?

@m18unet
Copy link
Author

m18unet commented Sep 6, 2023

Excellent!

Thank you for your quick and detailed response @DannyBen.

@m18unet m18unet closed this as completed Sep 6, 2023
@DannyBen
Copy link
Owner

DannyBen commented Sep 6, 2023

My pleasure. Enjoy, and feel free to open an issue or a discussion if you have any problems or questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants