Example #1
0
 public void Register(CommandLine commandLine, CommandQueue commandQueue)
 {
     commandLine
     .AddHandler("-query", (e) =>
     {
         if (!e.MoveNext())
         {
             throw new Exception("Group name is not specified.");
         }
         var group = (string)e.Current;
         if (!e.MoveNext())
         {
             throw new Exception("Expression is not specified.");
         }
         var sb = new StringBuilder();
         sb.Append((string)e.Current);
         bool formatHTML = false;
         while (e.MoveNext())
         {
             var a = (string)e.Current;
             if (a == "-html")
             {
                 formatHTML = true;
                 if (e.MoveNext())
                 {
                     throw new Exception(CommandLine.BAD_SYNTAX);
                 }
                 break;
             }
             else
             {
                 sb.Append(' ');
                 sb.Append(a);
             }
         }
         commandLine.NoMoreArg = true;
         commandQueue.Add(() =>
         {
             Query(group, sb.ToString(), formatHTML);
         });
     })
     .AddTranslation("-q", "-query")
     .AddUsageHeader("Usage <search>:")
     .AddUsage("{0} -query GROUPNAME EXPRESSION... [-html]", Program.Name);
 }
Example #2
0
 public void Register(CommandLine commandLine, CommandQueue commandQueue)
 {
     commandLine
     .AddHandler(AUTHENTICATE, (e) =>
     {
         if (!e.MoveNext())
         {
             throw new Exception("Username/Password are not specified.");
         }
         string username;
         string password;
         var s   = (string)e.Current;
         var pos = s.IndexOf('/');
         if (pos > 0)
         {
             username = s.Substring(0, pos);
             password = s.Substring(pos + 1);
         }
         else if (pos == 0 || s.Length == 0)
         {
             throw new Exception("Empty username is not allowed.");
         }
         else
         {
             username = s;
             Console.Write("Password: "******"OK. {0}", response.AccessToken);
         });
     })
     .AddHandler("-print-myself", (e) =>
     {
         Console.WriteLine("{0}", IndexApiClient.Credentials.Username ?? "Not specified.");
     })
     .AddTranslation("-a", AUTHENTICATE)
     .AddTranslation("-auth", AUTHENTICATE)
     .AddUsageHeader("Usage <authentication>:")
     .AddUsage("{0} {1} USERNAME[/PASSWORD]", Program.Name, AUTHENTICATE)
     .AddUsage("{0} -print-myself", Program.Name);
 }
Example #3
0
 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);
 }
Example #4
0
 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);
 }
Example #5
0
 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");
 }
Example #6
0
 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);
 }