/tools/webdav/WebDavConnection.cs
C# | 193 lines | 172 code | 21 blank | 0 comment | 29 complexity | 7d5de4d80c14dbd40cf78d1621708f6e MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Xml.Linq;
- using Mono.Options;
- using Cadenza.Net;
- namespace Cadenza.Tools.WebDav {
- [AttributeUsage (AttributeTargets.Method, AllowMultiple=true)]
- class HelpAttribute : Attribute {
- public HelpAttribute (string helpText)
- {
- if (helpText == null)
- throw new ArgumentNullException ("helpText");
- HelpText = helpText;
- }
- public string HelpText {get; private set;}
- }
- class WebDavConnection
- {
- public WebDavMethodBuilder Builder {get; private set;}
- public WebDavConnection ()
- {
- Builder = new WebDavMethodBuilder ();
- }
- public static Action<WebDavConnection, string> GetCommand (string command)
- {
- Action<WebDavConnection, string> action;
- if (Commands.TryGetValue (command, out action))
- return action;
- return null;
- }
- static readonly Dictionary<string, Action<WebDavConnection, string>> Commands = new Dictionary<string, Action<WebDavConnection, string>>() {
- { "help", ShowHelp },
- { "get", Download },
- { "exit", Exit },
- { "server", Server },
- { "ls", ListPath },
- { "verbose", SetLogging },
- };
- [Help ("`help`; List all commands")]
- static void ShowHelp (WebDavConnection state, string args)
- {
- Console.WriteLine ("webdav commands--");
- foreach (var name in Commands.Keys.OrderBy (k => k)) {
- var d = Commands [name];
- var h = (HelpAttribute[]) d.Method.GetCustomAttributes (typeof (HelpAttribute), false);
- var prefix = " " + name + ":";
- if (h == null || h.Length == 0) {
- Console.WriteLine (prefix);
- continue;
- }
- if (prefix.Length > 10) {
- Console.WriteLine (prefix);
- prefix = new string (' ', 10);
- } else {
- prefix = prefix.PadRight (10);
- }
- foreach (var a in h)
- foreach (var l in Mono.Options.StringCoda.WrappedLines (a.HelpText, 50, 48)) {
- Console.WriteLine ("{0}{1}", prefix, l);
- prefix = new string (' ', 12);
- }
- }
- }
- [Help ("`exit`; Quit the program.")]
- static void Exit (WebDavConnection state, string ignored)
- {
- Environment.Exit (0);
- }
- [Help ("`server URI`; Connect to WebDAV server URI.")]
- static void Server (WebDavConnection state, string server)
- {
- if (!string.IsNullOrEmpty (server))
- state.Builder.Server = new Uri (server);
- else
- Console.WriteLine ("Server: {0}", state.Builder.Server);
- }
- [Help ("`ls [PATH [DEPTH [XML-REQUEST]]`;\n" +
- "List collection contents.\n" +
- "PATH: collection name to list; default=''\n" +
- "DEPTH: -1: infinity; 0: item; 1: children\n" +
- "XML-REQUEST: XML request to send to server.")]
- static void ListPath (WebDavConnection state, string args)
- {
- string[] v = Parse (args, 3);
- string p = v [0];
- int? d = v [1] == null ? null : (int?) int.Parse (v [1]);
- string x = v [2];
- XElement r = null;
- if (x != null) {
- try {
- r = XElement.Parse (x);
- } catch (Exception e) {
- Console.Error.WriteLine ("Invalid XML in '{0}': {1}", x, e.Message);
- return;
- }
- }
- using (var t = state.Builder.CreateFileStatusMethodAsync (p, d, r)) {
- try {
- t.Wait ();
- } catch (Exception e) {
- Console.Error.WriteLine ("webdav: {0}", e);
- return;
- }
- if (t.IsFaulted) {
- Console.Error.WriteLine ("webdav: {0}", t.Exception.Flatten ());
- return;
- }
- foreach (var e in t.Result.GetResponses ()) {
- Console.WriteLine ("{0} {1,10} {2,-12} {3}",
- e.ResourceType == null ? " " : e.ResourceType == WebDavResourceType.Collection ? "d" : "-",
- e.ContentLength,
- e.CreationDate == null ? "" : e.CreationDate.Value.ToString ("MMM d HH:MM"),
- e.Href);
- }
- }
- }
- static string[] Parse (string source, int max)
- {
- string[] values = new string [max];
- int i = 0;
- foreach (string s in ArgumentSource.GetArguments (new StringReader (source))) {
- if (i < (values.Length-1))
- values [i++] = s;
- else
- values [i] = values [i] == null ? s : values [i] + " " + s;
- }
- return values;
- }
- [Help ("`get SOURCE [TARGET]`: Download SOURCE.\n" +
- "If TARGET not specified, use stdout.")]
- static void Download (WebDavConnection state, string args)
- {
- string[] v = Parse (args, 2);
- string s = v [0];
- string d = v [1];
- Stream dest = d != null ? File.OpenWrite (d) : Console.OpenStandardOutput ();
- try {
- using (var t = state.Builder.CreateDownloadMethodAsync (s, dest)) {
- try {
- t.Wait ();
- } catch (Exception e) {
- Console.Error.WriteLine ("webdav: {0}", e);
- return;
- }
- if (t.IsFaulted) {
- Console.Error.WriteLine ("webdav: {0}", t.Exception.Flatten ());
- return;
- }
- Console.WriteLine ("StatusCode: {0}", t.Result.ResponseStatusCode);
- }
- } finally {
- if (d != null)
- dest.Close ();
- }
- }
- [Help ("`verbose [ENABLE]`: Show logging status.\n" +
- "ENABLE: 1/true=enables; 0/false=disables logging.")]
- static void SetLogging (WebDavConnection state, string args)
- {
- if (string.IsNullOrEmpty (args)) {
- Console.WriteLine (state.Builder.Log != null);
- return;
- }
- if (args == "1" || args == "true")
- state.Builder.Log = Console.Out;
- else
- state.Builder.Log = null;
- }
- }
- }