internal static async Task GetVersions(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (options.ContainsKey(Constants.VERSION_ID))
     {
         OnshapeVersion version = await context.Client.GetVersion(options[Constants.DOCUMENT_ID][0], options[Constants.VERSION_ID][0]);
         Console.WriteLine(JsonConvert.SerializeObject(version));
     }
     else
     {
         List<OnshapeVersion> versions = await context.Client.GetVersions(options[Constants.DOCUMENT_ID][0]);
         Console.WriteLine(JsonConvert.SerializeObject(versions));
     }
 }
 internal static async Task DeleteVersion(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     await context.Client.DeleteVersion(options[Constants.DOCUMENT_ID][0], options[Constants.VERSION_ID][0]);
 }
 internal static async Task GetWorkspaces(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (options.ContainsKey(Constants.WORKSPACE_ID))
     {
         OnshapeWorkspace workspace = await context.Client.GetWorkspace(options[Constants.DOCUMENT_ID][0], options[Constants.WORKSPACE_ID][0]);
         Console.WriteLine(JsonConvert.SerializeObject(workspace));
     }
     else
     {
         List<OnshapeWorkspace> workspaces = await context.Client.GetWorkspaces(options[Constants.DOCUMENT_ID][0]);
         Console.WriteLine(JsonConvert.SerializeObject(workspaces));
     }
 }
 internal static async Task DeleteWorkspace(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     await context.Client.DeleteWorkspace(options[Constants.DOCUMENT_ID][0], options[Constants.WORKSPACE_ID][0]);
 }
        private static CommandExecutionContext InitExecutionContext(ParsedCommandLine cmdLine)
        {
            CommandExecutionContext result = null;

            String baseUri = cmdLine.Options.ContainsKey(Constants.BASE_URI) && cmdLine.Options[Constants.BASE_URI].Count == 1 ? cmdLine.Options[Constants.BASE_URI][0] : Environment.GetEnvironmentVariable(Constants.ONSHAPE_BASE_URI);
            String clientId = Environment.GetEnvironmentVariable(Constants.ONSHAPE_CLIENT_ID);
            String clientSecret = Environment.GetEnvironmentVariable(Constants.ONSHAPE_CLIENT_SECRET);
            String oauthToken = cmdLine.Options.ContainsKey(Constants.OAUTH_TOKEN) && cmdLine.Options[Constants.OAUTH_TOKEN].Count == 1 ? cmdLine.Options[Constants.OAUTH_TOKEN][0] : Environment.GetEnvironmentVariable(Constants.ONSHAPE_OAUTH_TOKEN);
            String oauthRefreshToken = cmdLine.Options.ContainsKey(Constants.OAUTH_REFRESH_TOKEN) && cmdLine.Options[Constants.OAUTH_REFRESH_TOKEN].Count == 1 ? cmdLine.Options[Constants.OAUTH_REFRESH_TOKEN][0] : Environment.GetEnvironmentVariable(Constants.ONSHAPE_OAUTH_REFRESH_TOKEN);
            Boolean interactiveMode = cmdLine.Options.ContainsKey(Constants.INTERACTIVE_MODE);

            if (BaseExecutionContext != null)
            {
                Boolean sameAsBaseContext = String.Equals(BaseExecutionContext.BaseURL, baseUri, StringComparison.InvariantCultureIgnoreCase);
                sameAsBaseContext = sameAsBaseContext && (oauthToken == null || String.Equals(BaseExecutionContext.OAuthToken, oauthToken, StringComparison.InvariantCultureIgnoreCase));
                sameAsBaseContext = sameAsBaseContext && (oauthRefreshToken == null || String.Equals(BaseExecutionContext.OAuthRefreshToken, oauthRefreshToken, StringComparison.InvariantCultureIgnoreCase));
                // Return base context if no changes
                result = sameAsBaseContext ? BaseExecutionContext : ConstructExecutionContext(baseUri, clientId, clientSecret, oauthToken, oauthRefreshToken, interactiveMode);
            }
            else
            {
                result = ConstructExecutionContext(baseUri, clientId, clientSecret, oauthToken, oauthRefreshToken, interactiveMode);
                BaseExecutionContext = result;
            }

            return result;
        }
Beispiel #6
0
 internal static async Task DeleteWorkspace(CommandExecutionContext context, Dictionary <string, List <string> > options, List <string> values)
 {
     await context.Client.DeleteWorkspace(options[Constants.DOCUMENT_ID][0], options[Constants.WORKSPACE_ID][0]);
 }
 internal static async Task GenericDelete(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (values != null && values.Count == 1)
     {
         Console.WriteLine(await context.Client.HttpDelete(values[0].StartsWith(context.BaseURL) ? values[0] : context.BaseURL + values[0]));
     }
 }
 internal static async Task Help(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     await Task.Run(() => PrintHelp(options, values));
 }
 internal static async Task GenericGet(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (values != null && values.Count == 1)
     {
         var response = await context.Client.HttpGet(values[0].StartsWith(context.BaseURL) ? values[0]: context.BaseURL + values[0]);
         if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             response = await context.Client.HttpGet(response.RequestMessage.RequestUri.ToString());
         }
         Console.WriteLine(String.Format("Response:\n{0}\nBody:\n{1}", response, await response.Content.ReadAsStringAsync()));
     }
 }
 internal static async Task GenericPost(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (values != null && values.Count == 2)
     {
         var response = await context.Client.HttpPostJson(values[0].StartsWith(context.BaseURL) ? values[0] : context.BaseURL + values[0], values[1]);
         Console.WriteLine(String.Format("Response:\n{0}\nBody:\n{1}", response, await response.Content.ReadAsStringAsync()));
     }
 }
 internal static void PrintContext(CommandExecutionContext executionContext)
 {
     Console.WriteLine("Execution context:");
     Console.ForegroundColor = ConsoleColor.DarkGray;
     try
     {
         Console.WriteLine(JsonConvert.SerializeObject(executionContext, Formatting.Indented));
     }
     finally
     {
         Console.ResetColor();
     }
     Console.WriteLine("Cached options:");
     try
     {
         Console.WriteLine(JsonConvert.SerializeObject(Program.OptionValueCache, Formatting.Indented));
     }
     finally
     {
         Console.ResetColor();
     }
 }
Beispiel #12
0
        internal static async Task GetUser(CommandExecutionContext context, Dictionary <string, List <string> > options, List <string> values)
        {
            OnshapeUser user = await context.Client.GetUser(options[Constants.USER_ID][0]);

            Console.WriteLine(JsonConvert.SerializeObject(user));
        }
Beispiel #13
0
 internal static async Task DeleteVersion(CommandExecutionContext context, Dictionary <string, List <string> > options, List <string> values)
 {
     await context.Client.DeleteVersion(options[Constants.DOCUMENT_ID][0], options[Constants.VERSION_ID][0]);
 }
 internal static async Task GetUser(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     OnshapeUser user = await context.Client.GetUser(options[Constants.USER_ID][0]);
     Console.WriteLine(JsonConvert.SerializeObject(user));
 }
 internal static async Task GetDocuments(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (options.ContainsKey(Constants.DOCUMENT_ID))
     {
         OnshapeDocument document = await context.Client.GetDocument(options[Constants.DOCUMENT_ID][0]);
         Console.WriteLine(JsonConvert.SerializeObject(document));
     }
     else if (values != null && values.Count > 0)
     {
         foreach (String v in values)
         {
             OnshapeDocument document = await context.Client.GetDocument(v);
             Console.WriteLine(JsonConvert.SerializeObject(document));
         }
     }
     else 
     {
         List<OnshapeDocument> documents = await context.Client.GetDocuments();
         documents.Print();
     }
 }
 internal static async Task GetElements(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     if (options.ContainsKey(Constants.WORKSPACE_ID))
     {
         if (options.ContainsKey(Constants.ELEMENT_ID))
         {
             OnshapeElement element = await context.Client.GetWorkspaceElement(options[Constants.DOCUMENT_ID][0], options[Constants.WORKSPACE_ID][0], options[Constants.ELEMENT_ID][0]);
             Console.WriteLine(JsonConvert.SerializeObject(element));
         }
         else
         {
             List<OnshapeElement> elements = await context.Client.GetWorkspaceElements(options[Constants.DOCUMENT_ID][0], options[Constants.WORKSPACE_ID][0]);
             Console.WriteLine(JsonConvert.SerializeObject(elements));
         }
     }
     else
     {
         if (options.ContainsKey(Constants.ELEMENT_ID))
         {
             OnshapeElement element = await context.Client.GetVersionElement(options[Constants.DOCUMENT_ID][0], options[Constants.VERSION_ID][0], options[Constants.ELEMENT_ID][0]);
             Console.WriteLine(JsonConvert.SerializeObject(element));
         }
         else
         {
             List<OnshapeElement> elements = await context.Client.GetVersionElements(options[Constants.DOCUMENT_ID][0], options[Constants.VERSION_ID][0]);
             Console.WriteLine(JsonConvert.SerializeObject(elements));
         }
     }
 }
 internal static async Task DownloadThumbnail(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     string documentId = options[Constants.DOCUMENT_ID][0];
     string fileName = options[Constants.FILE][0];
     if (await context.Client.DownloadDocumentThumbnail(documentId, null, fileName))
     {
         Console.WriteLine("The thumbnail has been downloaded to {0}", fileName);
     }
 }
 internal static async Task Exit(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     await Task.Run(() => context.InteractiveMode = false);
 }
 internal static async Task CreateDocument(CommandExecutionContext context, Dictionary<string, List<string>> options, List<string> values)
 {
     OnshapeDocument document = await context.Client.CreateDocument(values[0]);
     Console.WriteLine(JsonConvert.SerializeObject(document));
 }
        private static async Task RunAsync(string[] args)
        {
            // Init console window size
            Console.WindowWidth = 98;

            // Parse command line
            ParsedCommandLine cmdLine = null;
            try
            {
                cmdLine = ParseCommandLine(args);
            }
            catch (CommandLineParseException e)
            {
                printUsage(e.Message, null, null);
                exitCode = 1;
            }

            if (cmdLine != null)
            {
                if (cmdLine.Command != null && String.Equals(Constants.HELP, cmdLine.Command.Name))
                {
                    printUsage(null, cmdLine.Options, cmdLine.Values);
                    return;
                }
                try
                {
                    // Initialize command execution context
                    BaseExecutionContext = InitExecutionContext(cmdLine);

                    // Execute command
                    if (cmdLine.Command != null)
                    {
                        await cmdLine.Command.Worker(BaseExecutionContext, cmdLine.Options, cmdLine.Values);
                    }

                    if (BaseExecutionContext.InteractiveMode)
                    {
                        CommandExecutionContext executionContext = BaseExecutionContext;
                        string pattern = @"\s*(""([^""])*""|'([^'])*'|([^""\s])*)";
                        do {
                            Console.Write(Constants.INTERACTIVE_PROMPT);
                            MatchCollection matches = Regex.Matches(Console.ReadLine(), pattern, RegexOptions.IgnorePatternWhitespace);
                            List<string> tokenList = new List<string>();
                            foreach (Match m in matches) {
                                String t = m.Value.Trim();
                                if (t.StartsWith("\"") || t.StartsWith("'") && t.Length > 1)
                                {
                                    t = t.Substring(1, t.Length - 2);
                                }
                                if (!String.IsNullOrEmpty(t))
                                {
                                    tokenList.Add(t);
                                }
                            }
                            string[] tokens = tokenList.ToArray();
                            if (tokens.Length > 0 && tokens[0].Length > 0)
                            {
                                try
                                {
                                    // Parse command
                                    cmdLine = ParseCommandLine(tokens);

                                    // Initialize execution context
                                    executionContext = InitExecutionContext(cmdLine);

                                    // Execute command
                                    await cmdLine.Command.Worker(BaseExecutionContext, cmdLine.Options, cmdLine.Values);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(String.Format("Error:\n\t{0}", e.Message));
                                }
                            }
                        } while (executionContext != null && executionContext.InteractiveMode);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(String.Format(@"Error: {0}", e.Message));
                    exitCode = 1;
                }
            }
        }
Beispiel #21
0
        internal static async Task CreateDocument(CommandExecutionContext context, Dictionary <string, List <string> > options, List <string> values)
        {
            OnshapeDocument document = await context.Client.CreateDocument(values[0]);

            Console.WriteLine(JsonConvert.SerializeObject(document));
        }