Dealing with passwords in NAnt build script

房东的猫 提交于 2020-01-13 08:57:12

问题


Is there a way to prompt the user for input during a NAnt build? I want to execute a command that takes a password, but I don't want to put the password into the build script.


回答1:


I'm using a script for now, but I'd love to hear if there's a prebuilt method already available. Many thanks to sundar for the ForegroundColor trick.

I'm not sure if it matters whether you use Project.Log or go direct to Console.WriteLine(), any NAnt ninjas want to educate me?

Here's the script and a sample target that uses it:

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



回答2:


A solution I have used many times is to have a local config file containing such things as passwords, connection strings etc. that are specific to each developer. The NAnt build script will include these settings when building.

The local config file does not exist in the version control system so passwords are not exposed. The first time a developer checks out a code base and tries to build he has to create this config file. To make it easy for him, there could be a template file available such as my.config.template containing all the properties that can be customized.




回答3:


Try this :

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

-->



回答4:


This displays asterisks as you type the password:

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


来源:https://stackoverflow.com/questions/297136/dealing-with-passwords-in-nant-build-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!