-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathProgram.cs
207 lines (187 loc) · 7.59 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Linq;
using SharpBucket.V2;
using SharpBucket.V2.EndPoints;
using SharpBucket.V2.Pocos;
namespace SharpBucketCli
{
/// <summary>
/// This program is both a sample and a tool to help SharpBucket developers to maintain their test account.
/// When developing on SharpBucket you may quickly generate a lot of repositories which are not cleaned up because
/// due to broken unit test execution during a debug session, or writing new unit tests that leak, and so on.
/// And deleting a lot of repositories with the web interface is ungrateful...
/// </summary>
public class Program
{
public static int Main(string[] args)
{
try
{
if (args.Length == 0)
{
var program = new Program();
program.ListenToInteractiveCommands();
return 0;
}
Console.Error.WriteLine("Non interactive mode is not yet implemented");
return -1;
}
catch (Exception e)
{
Console.Error.WriteLine(e);
return -1;
}
}
private SharpBucketV2 SharpBucket { get; }
/// <summary>
/// The account on which I am currently logged.
/// </summary>
private User Me { get; set; }
/// <summary>
/// The workspace On which I am currently working on.
/// </summary>
private Workspace Workspace { get; set; }
private Program()
{
this.SharpBucket = new SharpBucketV2();
}
private void ListenToInteractiveCommands()
{
Console.WriteLine("Welcome to the interactive mode of the SharpBucket Command Line Interface");
this.UseEnvironmentCredentials();
while (true)
{
Console.Write($"{this.Me?.nickname}:{this.Workspace?.slug}> ");
var command = Console.ReadLine() ?? string.Empty;
var args = command.Split(' ');
var verb = args[0];
var options = args.Skip(1).ToArray();
try
{
switch (verb)
{
case "help": Help(); break;
case "clean": Clean(); break;
case "list": List(options); break;
case "switch": Switch(options); break;
case "exit": return;
default: Console.WriteLine("Unrecognized command. Type help to get help about existing commands"); break;
}
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
}
private void UseEnvironmentCredentials()
{
var consumerKey = Environment.GetEnvironmentVariable("SB_CONSUMER_KEY");
var consumerKeySecret = Environment.GetEnvironmentVariable("SB_CONSUMER_SECRET_KEY");
if (!string.IsNullOrEmpty(consumerKey) && !string.IsNullOrEmpty(consumerKeySecret))
{
this.SharpBucket.OAuth2ClientCredentials(consumerKey, consumerKeySecret);
this.Me = this.SharpBucket.UserEndPoint().GetUser();
this.Workspace = this.SharpBucket.WorkspacesEndPoint()
.EnumerateWorkspaces(new EnumerateWorkspacesParameters {PageLen = 1})
.First();
Console.WriteLine($"You have been automatically logged as {Me.display_name}");
}
}
private static void Help()
{
Console.WriteLine("Available commands are:");
Console.WriteLine(" clean : Delete all the repositories in the current workspace.");
Console.WriteLine(" Useful to clean up a test account overwhelmed by repositories not");
Console.WriteLine(" correctly cleaned up by the unit tests.");
Console.WriteLine();
Console.WriteLine(" list : List workspaces or repositories in the current workspace.");
Console.WriteLine();
Console.WriteLine(" switch : Switch to another workspace.");
Console.WriteLine();
Console.WriteLine(" exit : Exit the interactive mode.");
Console.WriteLine();
Console.WriteLine(" help : Print this help.");
}
private void Clean()
{
if (this.Me == null)
{
Console.Error.WriteLine("You must be logged to execute that command");
return;
}
var repositoriesResource = this.SharpBucket.RepositoriesEndPoint().RepositoriesResource(this.Workspace.slug);
var repositories = repositoriesResource.ListRepositories();
foreach (var repository in repositories)
{
var repositoryResource = repositoriesResource.RepositoryResource(repository.slug);
repositoryResource.DeleteRepository();
Console.WriteLine($"Repository {this.Workspace.slug}/{repository.slug} has been deleted");
}
}
private void Switch(string[] args)
{
if (args.Length != 2)
{
Console.Error.WriteLine("Invalid command arguments");
return;
}
switch (args[0])
{
case "--workspace":
SwitchWorkspace(args[1]);
break;
default:
Console.Error.WriteLine("Invalid command arguments");
break;
}
}
private void SwitchWorkspace(string workspaceSlugOrUuid)
{
this.Workspace = SharpBucket.WorkspacesEndPoint().WorkspaceResource(workspaceSlugOrUuid).GetWorkspace();
}
private void List(string[] args)
{
if (args.Length != 1)
{
Console.Error.WriteLine("Invalid command arguments");
return;
}
switch (args[0])
{
case "--repositories":
ListRepositories();
break;
case "--workspaces":
ListWorkspaces();
break;
default:
Console.Error.WriteLine("Invalid command arguments");
break;
}
}
private void ListRepositories()
{
var repositoriesResource = this.SharpBucket.RepositoriesEndPoint().RepositoriesResource(this.Workspace.slug);
var repositories = repositoriesResource.EnumerateRepositories();
foreach (var repository in repositories)
{
Console.WriteLine(repository.slug);
Console.WriteLine(" name: " + repository.name);
Console.WriteLine(" uuid: " + repository.uuid);
Console.WriteLine(" is_private: " + repository.is_private);
}
}
private void ListWorkspaces()
{
var workspaces = SharpBucket.WorkspacesEndPoint().EnumerateWorkspaces();
foreach (var workspace in workspaces)
{
Console.WriteLine(workspace.slug);
Console.WriteLine(" name: " + workspace.name);
Console.WriteLine(" uuid: " + workspace.uuid);
Console.WriteLine(" is_private: " + workspace.is_private);
}
}
}
}