mizipzor avatar mizipzor committed dcd6030

Added prompt for credentials if they were not supplied.

Comments (0)

Files changed (3)

 {
     internal sealed class Options : CommandLineOptionsBase
     {
-        [Option("b", "build", Required = true,
-            HelpText = "The build to download artifacts from (required)")]
+        [Option("b", "build",
+            HelpText = "The build to download artifacts from")]
         public string BuildType { get; set; }
 
         [Option("i", "build-id", DefaultValue = "lastFinished",

TeamCityArtifactDownloader.cs

     {
         private readonly Options options;
         private UriBuilder uriBuilder;
-        private string userName;
-        private string password;
         private string outdir;
 
         public TeamCityArtifactDownloader(Options options)
                 outdir += Path.DirectorySeparatorChar;
             }
 
-            uriBuilder = new UriBuilder(options.Url);
-            userName = uriBuilder.UserName;
-            password = uriBuilder.Password;
+            // if no protocol is specified the uribuilder gets confused
+            uriBuilder = new UriBuilder(options.Url.Contains("://")
+                ? options.Url :
+                "http://" + options.Url);
 
-            // TODO if password is not supplied, go interactive
+            // if credentials is not supplied, go interactive
+            if (!EnsureCredentials())
+            {
+                return 0;
+            }
 
-            // go through every artifact
-            foreach (string fileName in GetArtifacts())
+            try
             {
-                // filter on glob
-                if (string.IsNullOrEmpty(options.Glob) ||
-                    fileName.Glob(options.Glob))
+                // go through every artifact
+                foreach (string fileName in GetArtifacts())
                 {
-                    DownloadArtifact(fileName);
+                    // filter on glob
+                    if (string.IsNullOrEmpty(options.Glob) ||
+                        fileName.Glob(options.Glob))
+                    {
+                        DownloadArtifact(fileName);
+                    }
                 }
             }
+            catch (WebException ex)
+            {
+                Console.Error.WriteLine(ex.Message);
+                return -1;
+            }
             return 0;
         }
 
         /// <summary>
+        /// Prompts user for username and password.
+        /// </summary>
+        /// <returns>
+        /// False if input operation was aborted, True otherwise
+        /// </returns>
+        private bool EnsureCredentials()
+        {
+            if (string.IsNullOrEmpty(uriBuilder.UserName))
+            {
+                string input = GetInput("username");
+                if (input == null)
+                {
+                    return false;
+                }
+                uriBuilder.UserName = input;
+            }
+            if (string.IsNullOrEmpty(uriBuilder.Password))
+            {
+                string input = GetInput("password");
+                if (input == null)
+                {
+                    return false;
+                }
+                uriBuilder.Password = input;
+            }
+            return true;
+        }
+
+        private static string GetInput(string what)
+        {
+            string s = null;
+            while (string.IsNullOrEmpty(s))
+            {
+                Console.Write(what + ": ");
+                s = Console.ReadLine();
+                if (s == null)
+                {
+                    break;
+                }
+                s = s.Trim();
+            }
+            return s;
+        }
+
+        /// <summary>
         /// Download a single artifact, respecting output options.
         /// </summary>
         /// <param name="artifactPath">The full path of the artifact.</param>
             // download the file
             if (!options.Dry)
             {
-                string filePath = CreateWebRequestUri(artifactPath);
+                string filePath = CreateFileRequest(artifactPath);
                 HttpWebRequest request = CreateWebRequest(filePath);
                 Utils.DownloadFile(request, localPath);
             }
             List<string> list = new List<string>();
             using (MemoryStream stream = new MemoryStream())
             {
-                string path = CreateWebRequestUri("teamcity-ivy.xml");
+                string path = CreateFileRequest("teamcity-ivy.xml");
                 HttpWebRequest request = CreateWebRequest(path);
                 Utils.DownloadFile(request, stream);
                 stream.Seek(0, SeekOrigin.Begin);
             return list;
         }
 
-        private string CreateWebRequestUri(string file)
+        private string CreateFileRequest(string file)
         {
-            string path = string.Format(
+            uriBuilder.Path = string.Format(
                 "httpAuth/repository/download/{0}/{1}/{2}",
                 options.BuildType,
                 options.BuildId,
                 file);
-            return uriBuilder.Uri + path;
+            return uriBuilder.ToString();
         }
 
         private string CreateAuthInfo()
         {
-            string authInfo = userName + ":" + password;
+            string authInfo = uriBuilder.UserName + ":" + uriBuilder.Password;
             authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
             return authInfo;
         }
                 //var myStr = sr.ReadToEnd();
                 //return myStr;
             }
-            catch (Exception e)
-            {
-                Console.WriteLine(e.Message);
-            }
             finally
             {
                 // Close the response and streams objects here 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.