Work with passwords in NAnt build script

Is there a way to ask the user for input during NAnt assembly? I want to execute a command that takes a password, but I do not want to embed the password in the assembly script.

+7
passwords nant
source share
4 answers

I am using a script now, but I would like to hear if there is a ready-made method. Thanks a lot to sundar for the ForegroundColor trick.

I'm not sure if using Project.Log or going directly to Console.WriteLine () matters, do any NAnt ninjas want to train me?

Here is the script and the target using 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> 
+7
source share

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

The local configuration file does not exist in the version control system, so passwords are not displayed. The first time a developer checks the code base and tries to build, he must create this configuration file. To make it easier, a template file may be available, such as my.config.template, containing all the properties that you can configure.

+6
source share

Try the following:

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

When entering the password, an asterisk is displayed:

  <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> 
+3
source share

All Articles