show the difference between two versions for a specified path
[tfs.git] / tools / opentf / WorkspacesCommand.cs
blob4a33836401381b505f48fb1186cb7acc5cfbe687
1 //
2 // WorkspacesCommand.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("workspaces", "List workspaces in server repository.", "[<workspace name>]")]
39 class WorkspacesCommand : Command
41 private string workspaceName;
43 [Option("Computer name", "M", "computer")]
44 public string OptionComputer;
46 [Option("Format \"brief\" or \"detailed\".", "F", "format")]
47 public string OptionFormat = "";
49 [Option("Owner name", "O", "owner")]
50 public string OptionOwner;
52 public WorkspacesCommand(Driver driver, string[] args): base(driver, args)
56 public void BriefOutput(Workspace[] workspaces)
58 int maxName = 9, maxComputer = 8, maxOwner = 5;
59 foreach (Workspace workspace in workspaces)
61 if ((!String.IsNullOrEmpty(workspaceName)) &&
62 (workspaceName != workspace.Name)) continue;
64 if (workspace.Name.Length > maxName) maxName = workspace.Name.Length;
65 if (workspace.Computer.Length > maxComputer) maxComputer = workspace.Computer.Length;
67 // domain is stripped on output
68 int ownerNameLen = workspace.OwnerName.Length;
69 int slash = workspace.OwnerName.IndexOf('\\');
70 if (-1 != slash) ownerNameLen = workspace.OwnerName.Length - slash;
72 if (ownerNameLen > maxOwner)
74 maxOwner = ownerNameLen;
78 int maxComment = WindowWidth - maxName - maxComputer - maxOwner - 3;
79 if (maxComment < 0) maxComment = 0;
81 string line = String.Format("{0} {1} {2} {3}",
82 "Workspace".PadRight(maxName),
83 "Owner".PadRight(maxOwner),
84 "Computer".PadRight(maxComputer),
85 "Comment");
86 Console.WriteLine(line);
88 line = String.Format("{0} {1} {2} {3}",
89 "-".PadRight(maxName, '-'),
90 "-".PadRight(maxOwner, '-'),
91 "-".PadRight(maxComputer, '-'),
92 "-".PadRight(maxComment, '-'));
93 Console.WriteLine(line);
95 foreach (Workspace workspace in workspaces)
97 if ((!String.IsNullOrEmpty(workspaceName)) &&
98 (workspaceName != workspace.Name)) continue;
100 string comment;
101 if (workspace.Comment.Length > maxComment)
102 comment = workspace.Comment.Remove(maxComment);
103 else
104 comment = workspace.Comment;
106 // domain is stripped on output
107 string ownerName = workspace.OwnerName;
108 int slash = workspace.OwnerName.IndexOf('\\');
109 if (-1 != slash)
111 ownerName = workspace.OwnerName.Substring(slash+1);
114 line = String.Format("{0} {1} {2} {3}",
115 workspace.Name.PadRight(maxName),
116 ownerName.PadRight(maxOwner),
117 workspace.Computer.PadRight(maxComputer),
118 comment);
119 Console.WriteLine(line);
123 public void DetailedOutput(Workspace[] workspaces)
125 bool first = true;
127 foreach (Workspace workspace in workspaces)
129 if ((!String.IsNullOrEmpty(workspaceName)) &&
130 (workspaceName != workspace.Name)) continue;
132 if (!first) Console.WriteLine();
133 else first = false;
135 Console.WriteLine("=".PadRight(WindowWidth, '='));
136 Console.WriteLine("Workspace: " + workspace.Name);
137 Console.WriteLine("Owner : " + workspace.OwnerName);
138 Console.WriteLine("Computer : " + workspace.Computer);
139 Console.WriteLine("Comment : " + workspace.Comment);
140 Console.WriteLine("Server : " + Driver.GetServerUrl());
142 Console.WriteLine();
143 Console.WriteLine("Working folders:");
145 foreach (WorkingFolder folder in workspace.Folders)
147 Console.WriteLine(" " + folder.ServerItem + " " + folder.LocalItem);
152 public string Computer
154 get {
155 if (String.IsNullOrEmpty(OptionComputer)) return Environment.MachineName;
156 if (OptionComputer == "*") return null;
157 return OptionComputer;
161 public override void Run()
163 if (Arguments.Length > 0) workspaceName = Arguments[0];
164 Workspace[] workspaces = VersionControlServer.QueryWorkspaces(workspaceName, OwnerFromString(OptionOwner), Computer);
166 if (workspaces.Length == 0)
168 Console.WriteLine("No workspace matching {0} on computer {1} found in Team Foundation Server.",
169 OwnerFromString(OptionOwner), Computer);
170 Environment.Exit((int)ExitCode.Failure);
173 bool detailed = OptionFormat.Equals("detailed", StringComparison.InvariantCultureIgnoreCase);
174 if (detailed) DetailedOutput(workspaces);
175 else BriefOutput(workspaces);
177 Workstation.Current.UpdateWorkspaceInfoCache(VersionControlServer, RepositoryConstants.AuthenticatedUser);