在NAnt构建脚本中处理密码

8

在NAnt构建期间,是否有一种方法可以提示用户输入?我想执行一个需要密码的命令,但不想将密码放入构建脚本中。

4个回答

8

目前我正在使用一个脚本,但如果有现成的预构建方法,我很乐意听取。非常感谢sundar提供的ForegroundColor技巧。

我不确定是使用Project.Log还是直接使用Console.WriteLine()是否重要,任何NAnt高手想教育我吗?

以下是脚本和使用它的示例目标:

<target name="input">
    <script language="C#" prefix="password" >
        <code><![CDATA[
            [Function("ask")]
            public string AskPassword(string prompt) {
                Project.Log(Level.Info, prompt);
                ConsoleColor oldColor = Console.ForegroundColor;
                Console.ForegroundColor = Console.BackgroundColor;
                try
                {
                    return Console.ReadLine();
                }
                finally
                {
                    Console.ForegroundColor = oldColor;
                }
            }
        ]]></code>
    </script>

    <echo message="Password is ${password::ask('What is the password?')}"/>
</target>

6
我曾经多次使用的解决方案是,使用一个本地配置文件来存储特定于每个开发人员的密码、连接字符串等信息。在构建时,NAnt构建脚本将包含这些设置。
本地配置文件不会存在于版本控制系统中,因此密码不会被泄露。当开发人员首次检出代码库并尝试构建时,他必须创建此配置文件。为了让他更容易,可以提供一个模板文件,例如my.config.template,其中包含所有可自定义的属性。

我考虑过这个,但对于每个开发人员来说似乎比必要的工作还要多。如果最终有几个设置,我可能会转而使用它。 - Don Kirkby
这是你在第一次构建时只需要做一次的事情。我肯定更喜欢不必每次构建时都输入密码。 - Jonas Kongslund

4

这将在您输入密码时显示星号:

    <code><![CDATA[
        [Function("ask")]
        public string AskPassword(string prompt) {
            Project.Log(Level.Info, prompt);
            string password = "";

            // get the first character of the password
            ConsoleKeyInfo nextKey = Console.ReadKey(true);

            while (nextKey.Key != ConsoleKey.Enter)
            {
                if (nextKey.Key == ConsoleKey.Backspace)
                {
                    if (password.Length > 0)
                    {
                        password = password.Substring(0, password.Length - 1);

                        // erase the last * as well
                        Console.Write(nextKey.KeyChar);
                        Console.Write(" ");
                        Console.Write(nextKey.KeyChar);
                    }
                }
                else
                {
                    password += nextKey.KeyChar;
                    Console.Write("*");
                }

                nextKey = Console.ReadKey(true);
            }

            Console.WriteLine();

            return password;
        }
    ]]></code>

4

试试这个:

<script language="C#" prefix="test" >
          <code>
            <![CDATA[
              [Function("get-password")]
              public static string GetPassword(  ) {
                  Console.WriteLine("Please enter the password");
                  ConsoleColor oldForegroundColor = Console.ForegroundColor;           
             Console.ForegroundColor = Console.BackgroundColor;
                  string password = Console.ReadLine();
             Console.ForegroundColor = oldForegroundColor;
        return password;
              }
            ]]>
          </code>
</script>

<target name="test.password">
 <echo message='${test::get-password()}'/>
</target>

-->

我之前也在尝试类似的东西,但改变ForegroundColor是个好主意! - Don Kirkby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接