public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-print-users", (e) => { commandQueue.Add(() => { var users = usr.GetUsers(); foreach (var entry in users.OrderBy(x => x.Uid)) { Console.WriteLine("{0}", entry); } }); }) .AddHandler("-print-user", (e) => { if (!e.MoveNext()) { throw new Exception("User ID is not specified."); } if (Char.IsDigit(((string)e.Current)[0])) { int uid = int.Parse((string)e.Current); commandQueue.Add(() => { var entry = usr.GetUser(uid); Console.WriteLine("{0}", entry); }); } else { var username = (string)e.Current; commandQueue.Add(() => { var entry = usr.GetUser(username); Console.WriteLine("{0}", entry); }); } }) .AddHandler("-create-user", (e) => { if (!e.MoveNext()) { throw new Exception("Username, password, and roles are not specified."); } var username = (string)e.Current; if (!e.MoveNext()) { throw new Exception("Password, and roles are not specified."); } var password = (string)e.Current; if (!e.MoveNext()) { throw new Exception("Role is not specified."); } var roles = new List <string>(); roles.MergeItems((string)e.Current); commandQueue.Add(() => { var entry = usr.CreateUser(username, password, roles.ToArray()); Console.WriteLine("Created. {0}", entry); }); }) .AddHandler("-update-user", (e) => { if (!e.MoveNext()) { throw new Exception("User ID is not specified."); } int uid = int.Parse((string)e.Current); string username = null; string password = null; var roles = new List <string>(); var commandLine2 = new CommandLine(); commandLine2 .AddHandler("-username", (ee) => { if (!ee.MoveNext()) { throw new Exception("Username is not specified."); } username = (string)ee.Current; }) .AddHandler("-password", (ee) => { if (!ee.MoveNext()) { throw new Exception("Password is not specified."); } password = (string)ee.Current; }) .AddHandler("-roles", (ee) => { if (!ee.MoveNext()) { throw new Exception("Roles are not specified."); } roles.MergeItems((string)ee.Current); }) .AddTranslation("-u", "-username") .AddTranslation("-user", "-username") .AddTranslation("-p", "-password") .AddTranslation("-pass", "-password") .AddTranslation("-r", "-roles") .AddTranslation("-role", "-roles"); commandLine2.Parse(e); commandQueue.Add(() => { var entry = usr.UpdateUser(uid, username, password, roles.ToArray()); Console.WriteLine("Updated. {0}", entry); }); }) .AddHandler("-delete-user", (e) => { if (!e.MoveNext()) { throw new Exception("User ID is not specified."); } int uid = int.Parse((string)e.Current); commandQueue.Add(() => { var entry = usr.DeleteUser(uid); Console.WriteLine("Deleted. {0}", entry); }); }) .AddTranslation("-pu", "-print-users") .AddUsageHeader("Usage <user>:") .AddUsage("{0} -print-users", Program.Name) .AddUsage("{0} -print-user UID", Program.Name) .AddUsage("{0} -print-user USERNAME", Program.Name) .AddUsage("{0} -create-user USERNAME PASSWORD [ROLE[,ROLE2...]]", Program.Name) .AddUsage("{0} -update-user UID [-username USERNAME] [-password PASSWORD] [-roles ROLE[,ROLE2...]]", Program.Name) .AddUsage("{0} -delete-user UID", Program.Name); }
public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-print-files", (e) => { string group = DEFAULT_GROUP; if (e.MoveNext()) { group = (string)e.Current; } else { commandLine.NoMoreArg = true; } commandQueue.Add(() => { var fi = file.GetFiles(group); foreach (var entry in fi.OrderBy(x => x.Fid)) { Console.WriteLine("[{0}] {1}", entry.Fid, entry.Path); } }); }) .AddHandler("-print-file-stats", (e) => { string group = DEFAULT_GROUP; if (e.MoveNext()) { group = (string)e.Current; } else { commandLine.NoMoreArg = true; } commandQueue.Add(() => { var stats = file.GetFileStats(group); Console.WriteLine("{0}", stats); }); }) .AddHandler("-print-file", (e) => { if (!e.MoveNext()) { throw new Exception("Group name is not specified."); } var group = (string)e.Current; if (!e.MoveNext()) { throw new Exception("Path is not specified."); } var path = (string)e.Current; commandQueue.Add(() => { var info = file.GetFile(group, path); var contents = FileContents.Find(info.Fid); if (contents == null) { contents = file.DownloadFile(info.Fid); } foreach (var line in contents.Lines) { Console.WriteLine(line); } }); }) .AddHandler("-index-files", (e) => { var forceIndexing = false; var commandLine2 = new CommandLine(); commandLine2 .AddHandler("-force", (ee) => { forceIndexing = true; }) .AddHandler("-concurrency", (ee) => { if (!e.MoveNext()) { throw new Exception("Concurrency level number is not specified."); } concurrencyLevel = int.Parse((string)ee.Current); if (concurrencyLevel < 1 || 255 < concurrencyLevel) { throw new Exception("Concurrency level number is out of the valid range."); } }) .AddHandler("-immediate", (ee) => { indexMode = IndexMode.Immediate; }) .AddTranslation("-f", "-force") .AddTranslation("-c", "-concurrency") .AddTranslation("-i", "-immediate"); e = commandLine2.Parse(e); if (e == null) { throw new Exception("Group name is not specified."); } var group = (string)e.Current; if (!e.MoveNext()) { throw new Exception("Path is not specified."); } List <string> paths = new List <string>() { (string)e.Current }; while (e.MoveNext()) { paths.Add((string)e.Current); } commandLine.NoMoreArg = true; commandQueue.Add(() => { IndexFiles(group, paths, forceIndexing); }); }) .AddHandler("-delete-files", (e) => { string group = DEFAULT_GROUP; if (e.MoveNext()) { group = (string)e.Current; } else { commandLine.NoMoreArg = true; } commandQueue.Add(() => { Console.WriteLine("Started deleting files..."); var t1 = DateTime.Now; file.DeleteFiles(group); var t2 = DateTime.Now; Console.WriteLine("Done. Elapsed time: {0}", t2 - t1); }); }) .AddHandler("-delete-stale-files", (e) => { string group = DEFAULT_GROUP; if (e.MoveNext()) { group = (string)e.Current; } else { commandLine.NoMoreArg = true; } commandQueue.Add(() => { Console.WriteLine("Started deleting stale files..."); var t1 = DateTime.Now; file.DeleteStaleFiles(group); var t2 = DateTime.Now; Console.WriteLine("Done. Elapsed time: {0}", t2 - t1); }); }) .AddTranslation("-pf", "-print-files") .AddTranslation("-pfs", "-print-file-stats") .AddTranslation("-i", "-index-files") .AddTranslation("-index", "-index-files") .AddUsageHeader("Usage <file>:") .AddUsage("{0} -print-files [GROUPNAME]", Program.Name) .AddUsage("{0} -print-file-stats [GROUPNAME]", Program.Name) .AddUsage("{0} -print-file GROUPNAME PATH", Program.Name) .AddUsage("{0} -index-files [-force] [-concurrency NUMBER] [-immediate] GROUPNAME PATH...", Program.Name) .AddUsage("{0} -delete-files [GROUPNAME]", Program.Name) .AddUsage("{0} -delete-stale-files [GROUPNAME]", Program.Name); }
public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-index-api", (e) => { if (!e.MoveNext()) { throw new Exception("URL is not specified."); } var a = (string)e.Current; if (!a.StartsWith("http://") && !a.StartsWith("https://")) { throw new Exception("URL does not look valid."); } IndexApiClient.Url = a; }) .AddHandler("-username", (e) => { if (!e.MoveNext()) { throw new Exception("Username is not specified."); } IndexApiClient.ChangeUser((string)e.Current); }) .AddHandler("-print-extensions", (e) => { commandQueue.Add(() => { var list = pref.GetExtensions(); foreach (string ext in list) { Console.WriteLine("{0}", ext); } }); }) .AddHandler("-add-extensions", (e) => { if (!e.MoveNext()) { throw new Exception("Extensions are not specified."); } var exts = (string)e.Current; commandQueue.Add(() => { Console.WriteLine("Adding extensions setting..."); var res = pref.AddExtensions(exts); Console.WriteLine("{0}", res); }); }) .AddHandler("-clear-extensions", (e) => { commandQueue.Add(() => { Console.WriteLine("Clearing extensions setting..."); pref.ClearExtensions(); Console.WriteLine("Done."); }); }) .AddHandler("-print-skip-dirs", (e) => { commandQueue.Add(() => { var list = pref.GetSkipDirs(); foreach (string dir in list) { Console.WriteLine("{0}", dir); } }); }) .AddHandler("-add-skip-dirs", (e) => { if (!e.MoveNext()) { throw new Exception("Directories are not specified."); } var dirs = (string)e.Current; commandQueue.Add(() => { Console.WriteLine("Adding skip-dirs setting..."); pref.AddSkipDirs(dirs); Console.WriteLine("Done."); }); }) .AddHandler("-clear-skip-dirs", (e) => { commandQueue.Add(() => { Console.WriteLine("Clearing skip-dirs setting..."); pref.ClearSkipDirs(); Console.WriteLine("Done."); }); }) .AddTranslation("-u", "-username") .AddTranslation("-pe", "-print-extensions") .AddTranslation("-print-ext", "-print-extensions") .AddTranslation("-ext", "-add-extensions") .AddTranslation("-add-ext", "-add-extensions") .AddTranslation("-pd", "-print-skip-dirs") .AddTranslation("-skip", "-add-skip-dirs") .AddTranslation("-skip-dirs", "-add-skip-dirs") .AddUsageHeader("Usage <configuration>:") .AddUsage("{0} -print-extensions", Program.Name) .AddUsage("{0} -add-extensions EXT[,EXT2...]", Program.Name) .AddUsage("{0} -clear-extensions", Program.Name) .AddUsage("{0} -print-skip-dirs", Program.Name) .AddUsage("{0} -add-skip-dirs DIR[,DIR2...]", Program.Name) .AddUsage("{0} -clear-skip-dirs", Program.Name) .AddOption("-index-api URL") .AddOption("-username USERNAME"); }
public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-print-id.next", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetIds(); task.Wait(); if (task.Result is IdStatus status) { Console.WriteLine("{0}", status); } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-reset-id.next", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.ResetIds(); task.Wait(); if (task.Result is IdStatus status) { Console.WriteLine("{0}", status); } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-print-unused-files", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetUnusedFiles(); task.Wait(); if (task.Result is FileInfo[] unused) { foreach (var info in unused) { Console.WriteLine("{0}", info); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-delete-unused-files", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.DeleteUnusedFiles(); task.Wait(); if (task.Result is FileInfo[] deleted) { if (deleted.Length > 0) { Console.Write("Deleted: "); foreach (var info in deleted) { Console.WriteLine("{0}", info); } } else { Console.WriteLine("Unused files not found."); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-print-unused-contents", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetUnusedContents(); task.Wait(); if (task.Result is int[] unused) { var sb = new StringBuilder(); foreach (int fid in unused) { sb.AppendFormat(" {0}", fid); } if (sb.Length > 0) { Console.WriteLine("{0}", sb.ToString(1, sb.Length - 1)); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-delete-unused-contents", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.DeleteUnusedContents(); task.Wait(); if (task.Result is int[] deleted) { if (deleted.Length > 0) { var sb = new StringBuilder(); foreach (int fid in deleted) { sb.AppendFormat(" {0}", fid); } Console.WriteLine("Deleted:{0}", sb.ToString()); } else { Console.WriteLine("Unused contents not found."); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-print-index-stats", (e) => { if (!e.MoveNext()) { throw new Exception("Group name is not specified."); } var groupname = (string)e.Current; commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetIndexStats(groupname); task.Wait(); if (task.Result is IndexStats stats) { Console.WriteLine("{0}", stats); } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddUsageHeader("Usage <diagnostics>:") .AddUsage("{0} -print-id.next", Program.Name) .AddUsage("{0} -reset-id.next", Program.Name) .AddUsage("{0} -print-unused-files", Program.Name) .AddUsage("{0} -delete-unused-files", Program.Name) .AddUsage("{0} -print-unused-contents", Program.Name) .AddUsage("{0} -delete-unused-contents", Program.Name) .AddUsage("{0} -print-index-stats GROUPNAME", Program.Name); }
public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-print-groups", (e) => { commandQueue.Add(() => { var values = grp.GetFileGroups(); foreach (FileGroupInfo entry in values.OrderBy(x => x.Gid)) { Console.WriteLine("{0}", entry); } }); }) .AddHandler("-create-group", (e) => { if (!e.MoveNext()) { throw new Exception("Group name is not specified."); } var groupname = (string)e.Current; commandQueue.Add(() => { var entry = grp.CreateFileGroup(groupname); Console.WriteLine("Created. {0}", entry); }); }) .AddHandler("-update-group", (e) => { if (!e.MoveNext()) { throw new Exception("Group ID is not specified."); } int gid = int.Parse((string)e.Current); if (!e.MoveNext()) { throw new Exception("Group name is not specified."); } string groupname = (string)e.Current; commandQueue.Add(() => { var entry = grp.UpdateFileGroup(gid, groupname); Console.WriteLine("Updated. {0}", entry); }); }) .AddHandler("-delete-group", (e) => { if (!e.MoveNext()) { throw new Exception("User ID is not specified."); } int gid = int.Parse((string)e.Current); commandQueue.Add(() => { var entry = grp.DeleteFileGroup(gid); Console.WriteLine("Deleted. {0}", entry); }); }) .AddTranslation("-pg", "-print-groups") .AddUsageHeader("Usage <group>:") .AddUsage("{0} -print-groups", Program.Name) .AddUsage("{0} -create-group GROUPNAME", Program.Name) .AddUsage("{0} -update-group GID GROUPNAME", Program.Name) .AddUsage("{0} -delete-group GID", Program.Name); }