2 // WorkspacesCommand.cs
5 // Joel Reed (joelwreed@gmail.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System
.Collections
.Generic
;
33 using Microsoft
.TeamFoundation
.Client
;
34 using Microsoft
.TeamFoundation
.VersionControl
.Client
;
35 using Mono
.GetOptions
;
37 [Command("workspaces", "List workspaces in server repository.")]
38 class WorkspacesCommand
: Command
40 [Option("Format", "F", "format")]
41 public string OptionFormat
= "";
43 [Option("Owner name", "O", "owner")]
44 public string OptionOwner
;
46 public WorkspacesCommand(Driver driver
, string[] args
): base(driver
, args
)
50 public void BriefOutput(Workspace
[] workspaces
)
52 int maxName
= 9, maxComputer
= 8, maxOwner
= 5;
53 foreach (Workspace workspace
in workspaces
)
55 if (workspace
.Name
.Length
> maxName
) maxName
= workspace
.Name
.Length
;
56 if (workspace
.Computer
.Length
> maxComputer
) maxComputer
= workspace
.Computer
.Length
;
58 // domain is stripped on output
59 int ownerNameLen
= workspace
.OwnerName
.Length
;
60 int slash
= workspace
.OwnerName
.IndexOf('\\');
61 if (-1 != slash
) ownerNameLen
= workspace
.OwnerName
.Length
- slash
;
63 if (ownerNameLen
> maxOwner
)
65 maxOwner
= ownerNameLen
;
69 int maxComment
= WindowWidth
- maxName
- maxComputer
- maxOwner
- 3;
70 if (maxComment
< 0) maxComment
= 0;
72 string line
= String
.Format("{0} {1} {2} {3}",
73 "Workspace".PadRight(maxName
),
74 "Owner".PadRight(maxOwner
),
75 "Computer".PadRight(maxComputer
),
77 Console
.WriteLine(line
);
79 line
= String
.Format("{0} {1} {2} {3}",
80 "-".PadRight(maxName
, '-'),
81 "-".PadRight(maxOwner
, '-'),
82 "-".PadRight(maxComputer
, '-'),
83 "-".PadRight(maxComment
, '-'));
84 Console
.WriteLine(line
);
86 foreach (Workspace workspace
in workspaces
)
89 if (workspace
.Comment
.Length
> maxComment
)
90 comment
= workspace
.Comment
.Remove(maxComment
);
92 comment
= workspace
.Comment
;
94 // domain is stripped on output
95 string ownerName
= workspace
.OwnerName
;
96 int slash
= workspace
.OwnerName
.IndexOf('\\');
99 ownerName
= workspace
.OwnerName
.Substring(slash
+1);
102 line
= String
.Format("{0} {1} {2} {3}",
103 workspace
.Name
.PadRight(maxName
),
104 ownerName
.PadRight(maxOwner
),
105 workspace
.Computer
.PadRight(maxComputer
),
107 Console
.WriteLine(line
);
111 public void DetailedOutput(Workspace
[] workspaces
)
115 foreach (Workspace workspace
in workspaces
)
117 if (!first
) Console
.WriteLine();
120 Console
.WriteLine("=".PadRight(WindowWidth
, '='));
121 Console
.WriteLine("Workspace: " + workspace
.Name
);
122 Console
.WriteLine("Owner : " + workspace
.OwnerName
);
123 Console
.WriteLine("Computer : " + workspace
.Computer
);
124 Console
.WriteLine("Comment : " + workspace
.Comment
);
125 Console
.WriteLine("Server : " + driver
.GetServerUrl());
128 Console
.WriteLine("Working folders:");
130 foreach (WorkingFolder folder
in workspace
.Folders
)
132 Console
.WriteLine(" " + folder
.ServerItem
+ " " + folder
.LocalItem
);
137 public override void Run()
139 Workspace
[] workspaces
= VersionControlServer
.QueryWorkspaces(null, OwnerFromString(OptionOwner
), Computer
);
141 if (workspaces
.Length
== 0)
143 Console
.WriteLine("No workspace matching {0} on computer {1} found in Team Foundation Server.",
144 OwnerFromString(OptionOwner
), Computer
);
145 Environment
.Exit(-1);
148 bool detailed
= OptionFormat
.Equals("detailed", StringComparison
.InvariantCultureIgnoreCase
);
149 if (detailed
) DetailedOutput(workspaces
);
150 else BriefOutput(workspaces
);