python - 如何将自定义选项传递给 fab 命令
问题描述
我想为多个项目重用 fabfile。
配置文件
[project1]
git_repo = git@github/project1
project_path = '/path/project1'
[project2]
git_repo = git@github/project22
project_path = '/path/project2'
工厂文件.py
from fabric import task
config = configparser.ConfigParser()
config.read("conf.ini")
@task
def getcode(connection, project, git_repo):
args = config['project]
connection.run("git clone {}".format(git_repo))
@task
def pushcode(connection, project, git_repo):
args = config['project]
connection.run("git push {}".format(git_repo))
我怎样才能避免args = config['project]
在每种方法中使用。我可以使用 fab 命令传递自定义参数吗fab -H web1 --project=project1 pushcode
?需要帮忙。
解决方案
当然,您可以将参数传递给从invoke.task
. 我将举例说明如何做到这一点:
fabfile.py
from fabric import task
@task
def sampleTask(connection, name, laste_name, age):
print("The firstname is ", name)
print("The lastname is ", laste_name)
print("The age is ", age)
然后你像这样从命令行调用它: 命令行
fab sampleTask -n peshmerge -l Mo -a 28
输出应该是这样的:
[vagrant@localhost fabric]$ fab sampleTask -n peshmerge -l Mo -a 28
The firstname is Peshmerge
The lastname is Mo
The age is 28
注意:为您的任务命名包含下划线 (_) 将导致错误
No idea what 'sample_task' is!
命名任务参数也是如此。
推荐阅读
- javascript - React Google Charts 烛台颜色和灯芯未显示
- c# - 当我的数据库出现错误时,如何使用 Visual Studio 运行我的代码?
- python - 如何将keras后端从tenserflow切换到theano?
- matlab - 比较两个符号表达式
- php - 如何为 PHP 8 设置 VS Code 扩展 Intelephense?
- node.js - 使用 multer memoryStorage 时如何重命名我的原始名称?
- spring-boot - 将 Redis 用作 Spring Session 缓存时如何最小化与 Redis 的交互?
- vue.js - Vue.js 3 卸载和内存泄漏
- amazon-web-services - Terraform 使用 json 状态文件销毁
- git - git上意外硬重置分支