options cleanup
[tfs.git] / tools / tf / Driver.cs
blob651c9bd4f01e42d93a6344f2775eabd53b3c8f4a
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 ("1.0")]
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[] Commands;
59 public string[] Arguments;
61 private string domain;
62 private string username;
64 public string Domain
66 get { return domain; }
69 public string Username
71 get { return username; }
74 public Driver(string[] args)
76 Arguments = args;
77 Options.ProcessArgs(args);
78 Commands = Options.RemainingArguments;
81 static void ConflictEventHandler(Object sender, ConflictEventArgs e)
83 Console.Error.WriteLine(e.Message);
86 static void ExceptionEventHandler(Object sender, ExceptionEventArgs e)
88 // sometimes e.Failure is null, not sure why yet
89 if (e.Failure != null)
90 Console.Error.WriteLine(e.Failure.Message);
93 public VersionControlServer ConnectToServer()
95 if (null != _versionControl) return _versionControl;
96 string server = GetServerUrl();
97 string login = GetLogin(server);
99 string userinfo = "";
100 string pwd = "";
102 int comma = login.IndexOf(",");
103 if (comma != -1)
105 userinfo = login.Substring(0, comma);
106 pwd = login.Substring(comma+1);
108 else
110 userinfo = login;
111 Console.Write("Password: ");
112 pwd = Console.ReadLine();
115 int slash = userinfo.IndexOf('\\');
116 if (-1 == slash) username = userinfo;
117 else
119 domain = userinfo.Substring(0, slash);
120 username = userinfo.Substring(slash+1);
123 _tfs = new TeamFoundationServer(server, new NetworkCredential(username, pwd, domain));
124 _versionControl = (VersionControlServer) _tfs.GetService(typeof(VersionControlServer));
126 _versionControl.Conflict += ConflictEventHandler;
127 _versionControl.NonFatalError += ExceptionEventHandler;
129 // save credentials if passed
130 bool saveSetting = Settings.Current.GetAsBool("Credentials.Save");
131 if (saveSetting && !String.IsNullOrEmpty(Options.Login))
132 TfsKeyring.SetCredentials(server, Domain, Username, pwd);
134 return _versionControl;
137 static public void ShowHelp()
139 Console.WriteLine("usage: tf [subcommand] [arguments]");
140 Console.WriteLine();
141 Console.WriteLine("Available subcommands:");
142 Console.WriteLine();
144 CommandRegistry.ShowHelp();
147 public void ProcessAllCommands()
149 // some commands don't need to connect to TFS server
150 string firstCmd = Commands[0].ToLower();
151 Command cmd = null;
153 switch (firstCmd)
155 case "cache":
156 cmd = new CacheCommand(this);
157 break;
158 case "config":
159 case "configure":
160 cmd = new ConfigureCommand(this);
161 break;
162 case "help":
163 ShowHelp();
164 Environment.Exit(0);
165 break;
168 if (cmd != null)
170 cmd.Run();
171 Environment.Exit(0);
174 VersionControlServer vcs = ConnectToServer();
176 for (int i=0; i < Commands.Length; i++)
178 string scmd = Commands[i].ToLower();
179 List<string> cmdArgs = new List<string>();
180 cmdArgs.Add(scmd);
182 int j;
183 for (j=i+1; j < Commands.Length; j++)
185 string arg = Commands[j];
186 if (arg == "%") break;
187 cmdArgs.Add(arg);
190 ProcessCommand(vcs, scmd, cmdArgs.ToArray());
191 i = j;
195 public void ProcessCommand(VersionControlServer vcs, string cmd,
196 string[] cmdArgs)
198 Type commandType = CommandRegistry.GetCommandType(cmd);
199 if (commandType == null)
201 Console.WriteLine("Unknown command: " + cmd);
202 return;
205 Command command = (Command) Activator.CreateInstance(commandType, new object[]{ this });
206 command.Run();
209 public string GetServerUrl()
211 if (!String.IsNullOrEmpty(Options.Server))
213 if (Options.Server.StartsWith("http://") || Options.Server.StartsWith("https://"))
214 return Options.Server;
215 else
216 return String.Format("http://{0}:8080/", Options.Server);
219 WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(Environment.CurrentDirectory);
220 if (info == null)
222 Console.WriteLine("Unable to determine the team foundation server");
223 Console.WriteLine(" hint: try adding /server:<ip|name>");
224 Environment.Exit(1);
227 return info.ServerUri.ToString();
230 public string GetLogin(string server)
232 if (!String.IsNullOrEmpty(Options.Login))
233 return Options.Login;
235 // check the keyring
236 string login = TfsKeyring.GetCredentials(server);
237 if (!String.IsNullOrEmpty(login)) return login;
239 // finally prompt
240 return PromptForLogin();
243 public VersionControlServer VersionControlServer
245 get { return _versionControl; }
248 // ignoring certificate errors
249 public bool CheckValidationResult (ServicePoint sp,
250 X509Certificate certificate, WebRequest request, int error)
252 return true;
255 public static void Main(string[] args)
257 if (args.Length == 0) {
258 ShowHelp();
259 return;
262 Driver driver = new Driver(args);
263 ServicePointManager.CertificatePolicy = driver;
265 // basic auth doesn't seem to work well on mono when POSTing
266 // large data sets via a webservice.
267 AuthenticationManager.Unregister("Basic");
271 driver.ProcessAllCommands();
273 catch (TeamFoundationServerException e)
275 Console.Error.WriteLine(e.Message);
276 // Console.WriteLine(Domain + "\\" + Username);