コード例 #1
0
        static void Main(string[] args)
        {
            var options = new GoogleDriveCmdOptions();

            try
            {
                options = Utils.ParseCommandLineArgs(args);

                //string j = JsonConvert.SerializeObject(options, Formatting.Indented);
                //Console.Write(j);
                //Console.Read();
                //return;

                if (options.ErrorMessage != "")
                {
                    throw new Exception(options.ErrorMessage);
                }

                if (options.GetItem == true)
                {
                    var file = GoogleDrive.GetItem(options.Source);

                    if (file == null)
                    {
                        throw new Exception($"Error in getting Google Drive Item '{options.Source}'");
                    }

                    GoogleDriveItem gdriveItem = Utils.FileToGoogleDriveItem(file);

                    string msg = $"Information of Google Drive Item '{options.Source}'";

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, gdriveItem);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                        Console.WriteLine();
                        string json = JsonConvert.SerializeObject(gdriveItem, Formatting.Indented);
                        Console.WriteLine(json);
                    }
                }
                else if (options.GetItemPath == true)
                {
                    string path = GoogleDrive.GetItemPath(options.Source);

                    if (path == null)
                    {
                        throw new Exception($"Error in getting Item Path '{options.Source}'");
                    }

                    string msg = $"Path of '{options.Source}' is '{path}'";

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, path);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                        Console.WriteLine();
                    }
                }
                else if (options.Dir == true)
                {
                    var    ItemList = GoogleDrive.Dir(options.Source);
                    string msg;

                    if (ItemList != null && ItemList.Count > 0)
                    {
                        msg = $"Directory of '{options.Source}'";
                    }
                    else
                    {
                        msg = $"Directory '{options.Source}' is empty";
                    }

                    if (options.Json == true)
                    {
                        List <GoogleDriveItem> gdriveItemList = Utils.FileListToGoogleDriveItemList(ItemList);

                        JsonReturn jr   = new JsonReturn("OK", msg, gdriveItemList);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                        Console.WriteLine();

                        if (ItemList != null && ItemList.Count > 0)
                        {
                            foreach (var item in ItemList)
                            {
                                if (item.Trashed == false)
                                {
                                    if (item.MimeType == "application/vnd.google-apps.folder")
                                    {
                                        Console.Write("<DIR>  ");
                                    }
                                    else
                                    {
                                        Console.Write("       ");
                                    }

                                    Console.WriteLine(item.Name);
                                }
                            }
                        }
                    }
                }

                else if (options.CreateFolder == true)
                {
                    var file = GoogleDrive.CreateFolder(options.Source, options.Destination);

                    if (file == null)
                    {
                        throw new Exception($"Error in Creating Folder '{options.Source}' in '{options.Destination}'");
                    }

                    string msg = $"Folder '{options.Source}' created in '{options.Destination}', ID = {file.Id}";

                    if (options.Json == true)
                    {
                        GoogleDriveItem gdriveItem = Utils.FileToGoogleDriveItem(file);
                        JsonReturn      jr         = new JsonReturn("OK", msg, gdriveItem);
                        string          json       = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                    }
                }

                else if (options.CreateFolderStructure == true)
                {
                    var file = GoogleDrive.CreateFolderStructure(options.Source);

                    if (file == null)
                    {
                        throw new Exception($"Error in Creating Folder Structure '{options.Source}'");
                    }

                    string msg = $"Folder Structure '{options.Source}' created successfully, ID = {file.Id}";

                    GoogleDriveItem gdriveItem = Utils.FileToGoogleDriveItem(file);

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, gdriveItem);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                    }
                }

                else if (options.UploadFile == true)
                {
                    var    file = GoogleDrive.UploadFile(options.Source, options.Destination);
                    string msg  = $"File '{options.Source}' uploaded successfully, ID = {file.Id}";

                    if (options.Json == true)
                    {
                        GoogleDriveItem item = Utils.FileToGoogleDriveItem(file);
                        JsonReturn      jr   = new JsonReturn("OK", msg, item);
                        string          json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                    }
                }
                else if (options.DownloadFile == true)
                {
                    string FilePath = GoogleDrive.DownloadFile(options.Source, options.Destination);

                    if (string.IsNullOrWhiteSpace(FilePath))
                    {
                        throw new Exception("Error in Downloading File");
                    }

                    string msg = $"File '{options.Source}' downloaded successfully, Path = {FilePath}";

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, FilePath);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                    }
                }
                else if (options.Delete == true)
                {
                    GoogleDrive.Delete(options.Source);
                    string msg = $"Item '{options.Source}' deleted successfully";

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, null);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                    }
                }
                else if (options.GetPermission == true)
                {
                    IList <Permission> permissions = GoogleDrive.GetPermission(options.Source);
                    string             msg         = $"Permissions for '{options.Source}'";

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, permissions);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                        string json = JsonConvert.SerializeObject(permissions, Formatting.Indented);
                        Console.WriteLine(json);
                    }
                }
                else if (options.CreatePermission == true)
                {
                    IList <Permission> permissions = GoogleDrive.CreatePermission(options.Source, options.EmailAddress, options.PermissionID, options.Role, options.Type);

                    string msg = $"Permissions for '{options.Source}' Created Successfully";

                    if (options.Json == true)
                    {
                        JsonReturn jr   = new JsonReturn("OK", msg, permissions);
                        string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                        Console.Write(json);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine(msg);
                        string json = JsonConvert.SerializeObject(permissions, Formatting.Indented);
                        Console.WriteLine(json);
                    }
                }
                else
                {
                    throw new Exception($"Invalid Command Line Arguments '{args}'");
                }
            }
            catch (AlreadyExistException Ex)
            {
                if (options.Json == true)
                {
                    GoogleDriveItem item = Utils.FileToGoogleDriveItem(Ex.file);
                    JsonReturn      jr   = new JsonReturn("Warning", Ex.Message, item);
                    string          json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                    Console.Write(json);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(Ex.Message);
                }
            }
            catch (Exception Ex)
            {
                if (options.Json == true)
                {
                    JsonReturn jr   = new JsonReturn("ERROR", Ex.Message, null);
                    string     json = JsonConvert.SerializeObject(jr, Formatting.Indented);
                    Console.Write(json);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(Ex.Message);
                }
            }
            Console.Read();
        }
コード例 #2
0
ファイル: Utils.cs プロジェクト: softsingh/GoogleApi
        public static GoogleDriveCmdOptions ParseCommandLineArgs(string[] args)
        {
            var options = new GoogleDriveCmdOptions();
            int index   = 0;
            int length  = args.Length;

            foreach (string arg in args)
            {
                if (string.IsNullOrWhiteSpace(arg))
                {
                    index++;
                    continue;
                }

                switch (arg.ToLower())
                {
                case "--json":     // Json Output Flag

                    options.Json = true;
                    break;

                case "--getitem":     // Get File Object from ID/Path

                    options.GetItem = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.ErrorMessage = $"Invalid Path : '{args[index + 1]}'";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = $"Invalid Path : ''";
                    }
                    break;

                case "--getitempath":     // Get Item Path from ID

                    options.GetItemPath = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.ErrorMessage = $"Invalid ID '{args[index + 1]}'";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = $"Invalid ID ''";
                    }
                    break;

                case "--dir":     // List Directory Contents
                case "--ls":

                    options.Dir = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.Source = "root";
                        }
                    }
                    else
                    {
                        options.Source = "root";
                    }
                    break;

                case "--createfolder":     // Create Directory
                case "--cf":

                    options.CreateFolder = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[++index];

                            if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                            {
                                if (!IsKeyword(args[index + 1]))
                                {
                                    options.Destination = args[index + 1];
                                }
                                else
                                {
                                    options.Destination = "root";
                                }
                            }
                            else
                            {
                                options.Destination = "root";
                            }
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Folder Name ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Folder Name ''";
                    }

                    break;

                case "--createfolderstructure":     // Create Directories and Sub-Directories
                case "--cfs":

                    options.CreateFolderStructure = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Folder Path ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Folder Path ''";
                    }

                    break;

                case "--uploadfile":     // Upload File

                    options.UploadFile = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[++index];

                            if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                            {
                                if (!IsKeyword(args[index + 1]))
                                {
                                    options.Destination = args[index + 1];
                                }
                                else
                                {
                                    options.Destination = "root";
                                }
                            }
                            else
                            {
                                options.Destination = "root";
                            }
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Source File Path ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Source File Path ''";
                    }

                    break;

                case "--downloadfile":     // Download File

                    options.DownloadFile = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[++index];

                            if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                            {
                                if (!IsKeyword(args[index + 1]))
                                {
                                    options.Destination = args[index + 1];
                                }
                                else
                                {
                                    options.ErrorMessage = "Invalid Destination Folder ''";
                                }
                            }
                            else
                            {
                                options.ErrorMessage = "Invalid Destination Folder ''";
                            }
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Source File ID/Path ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Source File ID/Path ''";
                    }

                    break;

                case "--deletefile":     // Delete File/Folder
                case "--delete":
                case "--del":

                    options.Delete = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Item ID/Path ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Item ID/Path ''";
                    }

                    break;

                case "--getpermission":     // Get Permission

                    options.GetPermission = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Item ID/Path ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Item ID/Path ''";
                    }

                    break;

                case "--createpermission":     // Create (Set) Permission

                    options.CreatePermission = true;

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Source = args[index + 1];
                        }
                        else
                        {
                            options.ErrorMessage = "Invalid Item ID/Path ''";
                        }
                    }
                    else
                    {
                        options.ErrorMessage = "Invalid Item ID/Path ''";
                    }

                    break;

                case "--emailaddress":     // Email Address for CreatePermission
                case "--email":

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.EmailAddress = args[index + 1];
                        }
                        else
                        {
                            options.EmailAddress = "";
                        }
                    }
                    else
                    {
                        options.EmailAddress = "";
                    }

                    break;

                case "--permissionid":     // Permission ID for CreatePermission

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.PermissionID = args[index + 1];
                        }
                        else
                        {
                            options.PermissionID = "";
                        }
                    }
                    else
                    {
                        options.PermissionID = "";
                    }

                    break;

                case "--role":     // Role for CreatePermission

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Role = args[index + 1];
                        }
                        else
                        {
                            options.Role = "";
                        }
                    }
                    else
                    {
                        options.Role = "";
                    }

                    break;

                case "--type":     // Role for CreatePermission

                    if (length - 1 > index && !string.IsNullOrWhiteSpace(args[index + 1]))
                    {
                        if (!IsKeyword(args[index + 1]))
                        {
                            options.Type = args[index + 1];
                        }
                        else
                        {
                            options.Type = "";
                        }
                    }
                    else
                    {
                        options.Type = "";
                    }

                    break;
                }

                index++;
            }

            return(options);
        }