no.urls.needed
[tfs.git] / tools / tf / Driver.cs
blobadbf96e08f91886f4846ea599d851bf231a5b5a3
1 //
2 // Driver.cs
3 //
4 // Authors:
5 // Joel Reed (joelwreed@gmail.com)
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.Reflection;
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.Net;
34 using System.IO;
35 using System.Text;
36 using System.Security.Cryptography.X509Certificates;
37 using Mono.GetOptions;
38 using Microsoft.TeamFoundation;
39 using Microsoft.TeamFoundation.Client;
40 using Microsoft.TeamFoundation.VersionControl.Client;
41 using Microsoft.TeamFoundation.Server;
43 [assembly: AssemblyTitle ("tf.exe")]
44 [assembly: AssemblyVersion ("0.5.1")]
45 [assembly: AssemblyDescription ("Team Foundation Source Control Tool")]
46 [assembly: AssemblyCopyright ("(c) Joel W. Reed")]
48 [assembly: Mono.UsageComplement ("")]
50 [assembly: Mono.About("Team Foundation Source Control Tool")]
51 [assembly: Mono.Author ("Joel W. Reed")]
53 partial class Driver : ICertificatePolicy
55 private TeamFoundationServer _tfs;
56 private VersionControlServer _versionControl;
57 private DriverOptions Options = new DriverOptions();
58 private string[] Arguments;
60 private string domain;
61 private string username;
62 private string password;
63 private string serverUrl;
65 public string Domain
67 get {
68 GetUserCredentials();
69 return domain;
73 public string Username
75 get {
76 GetUserCredentials();
77 return username;
81 public string ServerUrl
83 get {
84 if (!String.IsNullOrEmpty(serverUrl)) return serverUrl;
86 if (!String.IsNullOrEmpty(Options.Server))
88 if (Options.Server.StartsWith("http://") || Options.Server.StartsWith("https://"))
89 return Options.Server;
90 else
91 return String.Format("http://{0}:8080/", Options.Server);
94 WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(Environment.CurrentDirectory);
95 if (info == null)
97 Console.WriteLine("Unable to determine the team foundation server");
98 Console.WriteLine(" hint: try adding /server:<ip|name>");
99 Environment.Exit(1);
102 serverUrl = info.ServerUri.ToString();
103 return serverUrl;
107 private string GetLogin()
109 if (!String.IsNullOrEmpty(Options.Login))
110 return Options.Login;
112 // check the keyring
113 string login = TfsKeyring.GetCredentials(ServerUrl);
114 if (!String.IsNullOrEmpty(login)) return login;
116 // finally prompt
117 return PromptForLogin();
120 private void GetUserCredentials()
122 if (! String.IsNullOrEmpty(username)) return;
124 string login = GetLogin();
125 string userinfo = "";
127 int comma = login.IndexOf(",");
128 if (comma != -1)
130 userinfo = login.Substring(0, comma);
131 password = login.Substring(comma+1);
133 else userinfo = login;
135 int slash = userinfo.IndexOf('\\');
136 if (-1 == slash) username = userinfo;
137 else
139 domain = userinfo.Substring(0, slash);
140 username = userinfo.Substring(slash+1);
144 public VersionControlServer VersionControlServer
146 get
148 if (null != _versionControl) return _versionControl;
149 GetUserCredentials();
151 if (String.IsNullOrEmpty(password))
153 Console.Write("Password: ");
154 password = Console.ReadLine();
157 _tfs = new TeamFoundationServer(ServerUrl, new NetworkCredential(username, password, domain));
158 _versionControl = (VersionControlServer) _tfs.GetService(typeof(VersionControlServer));
160 _versionControl.Conflict += ConflictEventHandler;
161 _versionControl.NonFatalError += ExceptionEventHandler;
163 // save credentials if passed
164 bool saveSetting = Settings.Current.GetAsBool("Credentials.Save");
165 if (saveSetting && !String.IsNullOrEmpty(Options.Login))
166 TfsKeyring.SetCredentials(ServerUrl, domain, username, password);
168 return _versionControl;
172 public Driver(string[] args)
174 Arguments = args;
175 Options.ProcessArgs(args);
178 static void ConflictEventHandler(Object sender, ConflictEventArgs e)
180 Console.Error.WriteLine(e.Message);
183 static void ExceptionEventHandler(Object sender, ExceptionEventArgs e)
185 // sometimes e.Failure is null, not sure why yet
186 if (e.Failure != null)
187 Console.Error.WriteLine(e.Failure.Message);
190 private bool IsOption(string arg)
192 if (String.IsNullOrEmpty(arg)) return false;
193 return (arg[0] == '-' || arg[0] == '/');
196 public void ProcessAllCommands()
198 List<string> cmdArgs = new List<string>();
200 for (int i=0; i < Arguments.Length; i++)
202 // unknown options are not sub commands
203 string scmd = Arguments[i].ToLower();
204 if (IsOption(scmd))
206 cmdArgs.Add(scmd);
207 continue;
210 // pull in the rest of the args for this subcommand
211 int j;
212 for (j=i+1; j < Arguments.Length; j++)
214 string arg = Arguments[j];
215 if (arg == "%") break;
216 cmdArgs.Add(arg);
219 ProcessCommand(scmd, cmdArgs.ToArray());
220 cmdArgs.Clear();
221 i = j;
225 public void ProcessCommand(string cmd, string[] cmdArgs)
227 Type commandType = CommandRegistry.GetCommandType(cmd);
228 if (commandType == null)
230 Console.WriteLine("Unknown command: " + cmd);
231 return;
234 Command command = (Command) Activator.CreateInstance(commandType, new object[]{ this, cmdArgs });
235 command.Run();
238 // ignoring certificate errors
239 public bool CheckValidationResult (ServicePoint sp,
240 X509Certificate certificate, WebRequest request, int error)
242 return true;
245 public static void Main(string[] args)
247 Driver driver = new Driver(args);
248 ServicePointManager.CertificatePolicy = driver;
250 if (args.Length == 0) {
251 HelpCommand cmd = new HelpCommand(driver, args);
252 cmd.Run();
253 return;
256 // basic auth doesn't seem to work well on mono when POSTing
257 // large data sets via a webservice.
258 AuthenticationManager.Unregister("Basic");
262 driver.ProcessAllCommands();
264 catch (TeamFoundationServerException e)
266 Console.Error.WriteLine(e.Message);
267 //Console.WriteLine(driver.Domain + "\\" + driver.Username);