rework credcache for multiple repo support
[tfs.git] / tools / opentf / WorkfoldCommand.cs
bloba63dc60d90a58ef4640d6878a80aec5685e221b4
1 //
2 // WorkfoldCommand.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.Collections.Generic;
31 using System.IO;
32 using System.Text;
33 using Microsoft.TeamFoundation.Client;
34 using Microsoft.TeamFoundation.VersionControl.Common;
35 using Microsoft.TeamFoundation.VersionControl.Client;
36 using Mono.GetOptions;
38 [Command("workfold", "Map and unmap server paths to local folders.", "<server path> [ <local path> ]")]
39 class WorkfoldCommand : Command
41 [Option("Unmap", "", "unmap")]
42 public bool OptionUnmap;
44 public WorkfoldCommand(Driver driver, string[] args): base(driver, args)
48 public void CurrentWorkfolders(Workspace workspace)
50 Console.WriteLine("=".PadRight(WindowWidth, '='));
51 Console.WriteLine("Workspace: " + workspace.Name);
52 Console.WriteLine("Server : " + Driver.GetServerUrl());
54 foreach (WorkingFolder folder in workspace.Folders)
56 Console.WriteLine(" " + folder.ServerItem + " " + folder.LocalItem);
60 public void MapWorkfolder(Workspace workspace, string serverItem, string localItem)
62 List<WorkingFolder> folders = new List<WorkingFolder>(workspace.Folders);
63 folders.Add(new WorkingFolder(serverItem, localItem));
64 workspace.Update(workspace.Name, workspace.Comment, folders.ToArray());
67 public void UnmapWorkfolder(Workspace workspace, string item)
69 if (! VersionControlPath.IsServerItem(item))
70 item = Path.GetFullPath(item);
72 List<WorkingFolder> folders = new List<WorkingFolder>(workspace.Folders);
73 string msg = String.Empty;
75 for (int i = 0; i < folders.Count; ++i)
77 WorkingFolder folder = folders[i];
79 if (item == folder.ServerItem || item == folder.LocalItem)
81 msg = String.Format("Removed: {0} => {1}",
82 folder.ServerItem, folder.LocalItem);
83 folders.RemoveAt(i);
84 break;
88 workspace.Update(workspace.Name, workspace.Comment, folders.ToArray());
89 if (!String.IsNullOrEmpty(msg))
90 Console.WriteLine(msg);
93 public override void Run()
95 Workspace workspace = GetWorkspaceFromServer();
97 if (Arguments.Length < 1)
98 CurrentWorkfolders(workspace);
99 else if (Arguments.Length < 2)
101 if (!OptionUnmap)
102 Console.WriteLine("No-op");
103 else
104 UnmapWorkfolder(workspace, Arguments[0]);
106 else if (Arguments.Length == 2)
107 MapWorkfolder(workspace, Arguments[0], Arguments[1]);