powershell 参数
same way as Bash Here Document/Heredoc, an example with Databricks CLI using Bash and PowerShell
与Bash Here Document / Heredoc相同的方式,使用Bash和PowerShell的Databricks CLI示例
Originally published at https://mertsenel.tech on May 29, 2020.
最初于 2020年5月29日 在 https://mertsenel.tech 上 发布 。
Hi, if you ever worked with a CLI tool you probably know that almost all of them comes with a method for initial configuration, that allows you to save your default settings and also your credentials. This is usually called a profile or a context.
嗨,如果您曾经使用过CLI工具,则可能知道它们几乎都带有用于初始配置的方法,该方法可让您保存默认设置以及凭据。 这通常称为配置文件或上下文。
However, what if you need to configure this tool on the fly with arguments you are retrieving from an environment or a CI/CD pipeline variable.
但是,如果需要使用从环境或CI / CD管道变量中检索的参数快速配置此工具,该怎么办。
There are multiple ways a CLI tool can achieve this. Some cli tools directly accepts arguments so you can use the tool’s authentication command in an automation scenario.
CLI工具可以通过多种方式实现这一目标。 一些cli工具直接接受参数,因此您可以在自动化方案中使用该工具的authentication命令。
> & myclitool login -url “URL” -username “USERNAME” -secret “PASSWORDorSECRET”
This allows us to authenticate the tool in an automation scenario passing all arguments at once with respective flags such as `-url` or `-secret`
这使我们能够在自动化方案中对工具进行身份验证,以自动传递带有相应标志(例如-url或-secret)的所有参数。
However, some CLI tools doesn’t come with that option and ask your for it’s arguments when you call them and expects you to enter them on the fly.
但是,某些CLI工具没有该选项,因此在调用它们时要求您提供参数,并希望您即时输入。
If you know the path, format, filename format of the configuration file your CLI tool will eventually save, then you can bypass this initial configuration process by simply generating the that configuration file the tool expects in the path it expects it, and that’s it. Your CLI tool should read the the content of the file and use the values stored in the file.
如果您知道CLI工具最终将保存的配置文件的路径,格式和文件名格式,则可以通过简单地在工具期望的路径中生成该工具期望的配置文件,就可以跳过此初始配置过程。 您的CLI工具应读取文件内容并使用文件中存储的值。
However, this method relies on a lot of factors, and puts you in a platform dependent state, as Linux and Windows hosts (either full Operating System or a Container) have different filesystem, and user profile paths. But if you script your inputs, you can just mimic a user input in an automation pipeline. I use PowerShell and Bash in combination on Ubuntu based Azure DevOps hosted agents with no issues at all, while I can develop in Windows.
但是,由于Linux和Windows主机(完整的操作系统或容器)具有不同的文件系统和用户配置文件路径,因此该方法取决于许多因素,并使您进入依赖于平台的状态。 但是,如果您编写输入脚本,则只能模仿自动化管道中的用户输入。 我可以在基于Ubuntu的Azure DevOps托管代理上结合使用PowerShell和Bash,完全没有问题,而我可以在Windows中进行开发。
我们通常如何为开发计算机手动配置Databricks CLI (How do we normally configure Databricks CLI manually for our Development machine)
Once we installed databricks cli via pip3 install databricks-cli
command then we can get into configuring our tool with databricks configure — token
通过pip3 install databricks-cli
命令pip3 install databricks-cli
之后,我们就可以使用databricks configure — token
配置工具了databricks configure — token
databricks configure --tokenDatabricks Host (should begin with https://): https://somedatabricksworkspace.urlToken: somedatabricksapitoken
Then CLI will ask us for 2 arguments, a URL for the Databricks Workspace and a secret, one by one.
然后,CLI将要求我们两个参数,一个是Databricks工作区的URL,另一个是一个秘密。
Once we enter the hostname and press enter, we will be asked for our token, once we type the token and press enter, then Databricks CLI saves the file under ~\.databricks.cfg
in the format:
输入主机名并按Enter后,将要求我们提供令牌,一旦键入令牌并按Enter,Databricks CLI将文件保存为~\.databricks.cfg
,格式为:
# Example databricks.cfg file[DEFAULT]host = https://somedatabricksworkspace.urltoken = somedatabricksapitoken
如何实现CI / CD自动化的相同结果 (How to achieve same results for CI/CD automation)
重击 (Bash)
The Bash method is most likely known to most bash coder and linux admin, it’s called `heredoc` or `here document` and used to pass multiple arguments to a bash via stdin input stream. To find out more about it you can read it here.
Bash方法是大多数bash编码器和linux管理员最有可能知道的方法,它称为“ heredoc”或“ here document” ,用于通过stdin输入流将多个参数传递给bash。 要了解更多信息,可以在这里阅读。
So by making use of it, assume we have can retrieve the endpoint url and the secret values during runtime and can assign them to two variables. Then we can construct the heredoc by having them separated by a newline hence will pass them one by one as input to our cli tool, Databricks CLI.
因此,通过使用它,假设我们可以在运行时检索端点url和秘密值并将它们分配给两个变量。 然后,我们可以通过用一个换行符分隔它们来构造heredoc,从而将它们一个接一个地传递给我们的cli工具Databricks CLI。
dbrclidemo.sh:
dbrclidemo.sh:
#!/bin/bash
DatabricksUrl='https://#{AZURE-DATABRICKS-TENANT-URL}#'
dapiToken='#{AZURE-DATABRICKS-TENANT-DAPI-TOKEN}#'
databricks configure --token <<EOF
${DatabricksUrl}
${dapiToken}
EOF
echo -e "\nDatabricks workspace list:"
databricks workspace list
![Image for post](https://miro.medium.com/max/9999/1*4cpUDpPBqpdHMI4xThsB3w.png)
电源外壳 (PowerShell)
In PowerShell there is something similar, that allows us to define multi-line string inputs called Here-String
在PowerShell中,有一些类似的东西,它允许我们定义称为Here-String
多行字符串输入
So, again assume we can retrieve the values we want during runtime from a secret vault. Then we can construct a Here-String, print it out with Write-Output
cmdlet and pipe the output to our external cli command with the &
PowerShell call operator.
因此,再次假设我们可以在运行时从机密保管库中检索所需的值。 然后,我们可以构造一个Here-String ,使用Write-Output
cmdlet将其打印出来,然后使用&
PowerShell调用运算符将输出通过管道传递到我们的外部cli命令。
pwshclidemo.ps1:
pwshclidemo.ps1:
#Pass Arguments to Input prompt received from external CLI tool inside script
$DatabricksUrl = 'https://#{AZURE-DATABRICKS-TENANT-URL}#'
$dapiToken ='#{AZURE-DATABRICKS-TENANT-DAPI-TOKEN}#'
$args = @"
$DatabricksUrl
$dapiToken
"@
Write-Output $args | & databricks configure --token
Write-Output "`nDatabricks workspace list:"
& databricks workspace list
![Image for post](https://miro.medium.com/max/9999/1*oFqLAfOuXdeDnlwWf70rtg.png)
摘要 (Summary)
You can use above methods in Linux and Windows environments to pass multiple arguments to an external tool. I’ve covered the example of configuring a CLI tool but this scenario may apply to whole bunch of applications I cannot think of. I hope this helped some PowerShell coders needing and looking for a way to workaround the limitations of the external tool they need to work with.
您可以在Linux和Windows环境中使用上述方法将多个参数传递给外部工具。 我已经介绍了配置CLI工具的示例,但是这种情况可能适用于我想不到的所有应用程序。 我希望这可以帮助一些需要一些PowerShell编码器的人,并寻找一种方法来解决他们需要使用的外部工具的局限性。
powershell 参数