use CommandRegistry in Driver do command routine
[tfs.git] / tools / tf / Driver.cs
blobd0b37edbbf615da5e05058189d813e83949ca608
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 TfOptions Options;
58 private string[] Commands;
60 public Driver(TfOptions options, string[] commands)
62 Options = options;
63 Commands = commands;
66 static void ConflictEventHandler(Object sender, ConflictEventArgs e)
68 Console.Error.WriteLine(e.Message);
71 static void ExceptionEventHandler(Object sender, ExceptionEventArgs e)
73 // sometimes e.Failure is null, not sure why yet
74 if (e.Failure != null)
75 Console.Error.WriteLine(e.Failure.Message);
78 public VersionControlServer ConnectToServer()
80 if (null != _versionControl) return _versionControl;
81 string server = GetServerUrl();
82 string login = GetLogin(server);
84 string userinfo = "";
85 string pwd = "";
87 int comma = login.IndexOf(",");
88 if (comma != -1)
90 userinfo = login.Substring(0, comma);
91 pwd = login.Substring(comma+1);
93 else
95 userinfo = login;
96 Console.Write("Password: ");
97 pwd = Console.ReadLine();
100 int slash = userinfo.IndexOf('\\');
101 if (-1 == slash) Options.Username = userinfo;
102 else
104 Options.Domain = userinfo.Substring(0, slash);
105 Options.Username = userinfo.Substring(slash+1);
108 _tfs = new TeamFoundationServer(server, new NetworkCredential(Options.Username, pwd, Options.Domain));
109 _versionControl = (VersionControlServer) _tfs.GetService(typeof(VersionControlServer));
111 _versionControl.Conflict += ConflictEventHandler;
112 _versionControl.NonFatalError += ExceptionEventHandler;
114 // save credentials if passed
115 bool saveSetting = Settings.Current.GetAsBool("Credentials.Save");
116 if (saveSetting && !String.IsNullOrEmpty(Options.Login))
117 TfsKeyring.SetCredentials(server, Options.Domain, Options.Username, pwd);
119 return _versionControl;
122 static public void ShowHelp()
124 Console.WriteLine("usage: tf [subcommand] [arguments]");
125 Console.WriteLine();
126 Console.WriteLine("Available subcommands:");
127 Console.WriteLine();
129 CommandRegistry.ShowHelp();
132 public void ProcessBranch() {}
133 public void ProcessBranches() {}
134 public void ProcessDifference() {}
135 public void ProcessLock() {}
136 public void ProcessMerge() {}
137 public void ProcessMerges() {}
138 public void ProcessResolve() {}
139 public void ProcessShelve() {}
140 public void ProcessShelvesets() {}
141 public void ProcessUndelete() {}
142 public void ProcessUnlabel() {}
143 public void ProcessUnshelve() {}
145 public void ProcessAllCommands()
147 // some commands don't need to connect to TFS server
148 string firstCmd = Commands[0].ToLower();
149 Command cmd = null;
151 switch (firstCmd)
153 case "cache":
154 cmd = new CacheCommand(Commands, Options);
155 break;
156 case "config":
157 case "configure":
158 cmd = new ConfigureCommand(Commands, Options);
159 break;
160 case "help":
161 ShowHelp();
162 Environment.Exit(0);
163 break;
166 if (cmd != null)
168 cmd.Run();
169 Environment.Exit(0);
172 VersionControlServer vcs = ConnectToServer();
174 for (int i=0; i < Commands.Length; i++)
176 string scmd = Commands[i].ToLower();
177 List<string> cmdArgs = new List<string>();
178 cmdArgs.Add(scmd);
180 int j;
181 for (j=i+1; j < Commands.Length; j++)
183 string arg = Commands[j];
184 if (arg == "%") break;
185 cmdArgs.Add(arg);
188 ProcessCommand(vcs, scmd, cmdArgs.ToArray());
189 i = j;
193 public void ProcessCommand(VersionControlServer vcs, string cmd,
194 string[] cmdArgs)
196 Type commandType = CommandRegistry.GetCommandType(cmd);
197 if (commandType == null)
199 Console.WriteLine("Unknown command: " + cmd);
200 return;
203 Command command = (Command) Activator.CreateInstance(commandType, new object[]{cmdArgs, Options, vcs});
204 command.Run();
207 public string GetServerUrl()
209 if (!String.IsNullOrEmpty(Options.Server))
211 if (Options.Server.StartsWith("http://") || Options.Server.StartsWith("https://"))
212 return Options.Server;
213 else
214 return String.Format("http://{0}:8080/", Options.Server);
217 WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(Environment.CurrentDirectory);
218 if (info == null)
220 Console.WriteLine("Unable to determine the team foundation server");
221 Console.WriteLine(" hint: try adding /server:<ip|name>");
222 Environment.Exit(1);
225 return info.ServerUri.ToString();
228 public string GetLogin(string server)
230 if (!String.IsNullOrEmpty(Options.Login))
231 return Options.Login;
233 // check the keyring
234 string login = TfsKeyring.GetCredentials(server);
235 if (!String.IsNullOrEmpty(login)) return login;
237 // finally prompt
238 return PromptForLogin();
241 public VersionControlServer VersionControlServer
243 get { return _versionControl; }
246 // ignoring certificate errors
247 public bool CheckValidationResult (ServicePoint sp,
248 X509Certificate certificate, WebRequest request, int error)
250 return true;
253 public static void Main(string[] args)
255 if (args.Length == 0) {
256 ShowHelp();
257 return;
260 TfOptions options = new TfOptions();
261 options.ProcessArgs(args);
263 Driver driver = new Driver(options, options.RemainingArguments);
264 ServicePointManager.CertificatePolicy = driver;
266 // basic auth doesn't seem to work well on mono when POSTing
267 // large data sets via a webservice.
268 AuthenticationManager.Unregister("Basic");
272 driver.ProcessAllCommands();
274 catch (TeamFoundationServerException e)
276 Console.Error.WriteLine(e.Message);
277 // Console.WriteLine(options.Domain + "\\" + options.Username);