Ejemplo n.º 1
0
        public CommandResponse StoreValidOptions()
        {
            if (_args.Length == 0)
            {
                return CommandResponse.Set(false, "Please enter valid arguments.");
            }

            for (var a = 0; a < _args.Length; a++)
            {
                //Only interested in option names.
                //If not a valid option name then the command is probably incorrect
                if (!IsEven(a))
                {
                    continue;
                }

                var arg = _args[a];
                if (!string.IsNullOrWhiteSpace(arg))
                {
                    var option = _args.ElementAt(a).Replace("-", "").ToLowerInvariant();
                    var optionValueKey = a + 1;

                    //Check if is valid option
                    var validOption = ValidOptions.FirstOrDefault(k => k.Key.Equals(option) || k.Key.First().ToString() == option);
                    
                    if (validOption.Key == null)
                    {
                        return CommandResponse.Set(false, $"The OPTION {option} is unknown.");
                    }

                    //Checks to see if last OPTION has a value
                    //Apart from -v & -h all other options should have a value
                    if (optionValueKey > _args.Length - 1)
                    {
                        return CommandResponse.Set(false, $"The OPTION {option} has not been supplied with a value.");
                    }

                    var optionValue = _args.ElementAt(optionValueKey);

                    if (!string.IsNullOrEmpty(optionValue) && !SensitiveOptions.Contains(validOption.Key))
                    {
                        optionValue = optionValue.ToLowerInvariant();
                    }

                    //If the OPTION has set values, check the incoming against this list
                    if (validOption.Key != "parameters" && validOption.Key != "setheaders" && validOption.Value.Any() && optionValue != null && !validOption.Value.Contains(optionValue))
                    {
                        return CommandResponse.Set(false, $"The OPTION {option} value is invalid.");
                    }

                    //Store the Option values
                    //Doing this here because we already have the key/values parsed
                    StoreOptionValue(validOption.Key, optionValue);
                }
            }

            return CommandResponse.Set(true, "Command Validated.");
        }
Ejemplo n.º 2
0
        public void CommandResponse_Set_Matches()
        {
            var validCommandResponse = new CommandResponse {
                Message = "Success",
                Success = true
            };

            var commandResponse = CommandResponse.Set(true, "Success");

            Assert.Equal(validCommandResponse, commandResponse, Comparers.ModelComparer <CommandResponse>());
        }
Ejemplo n.º 3
0
        public CommandResponse ParseCommand()
        {
            var message = new StringBuilder();

            if (string.IsNullOrWhiteSpace(_resource))
            {
                return CommandResponse.Set(false, $"The Resource OPTION is missing. All requests require a Resource type to be specified.");
            }

            message.AppendLine("Resource: " + _resource);
            message.AppendLine("Format: " + _format);

            if (_headers == null || !_headers.Any())
            {
                return CommandResponse.Set(false, $"All requests requires the setheaders option.");
            }

            if (!ValidMethods.Contains(_method))
            {
                return CommandResponse.Set(false, $"The Method {_method} is invalid.");
            }

            if (_method == "get" || _method == "delete" || _method == "put")
            {
                if (_parameters == null || !_parameters.Any())
                {
                    return CommandResponse.Set(false, $"The Parameters OPTION is missing or invalid. The GET, PUT and DELETE method requires parameters.");
                }

                message.AppendLine("Parameters: " + _parameters);
            }

            if ((_method == "put" || _method == "post"))
            {
                if (!string.IsNullOrWhiteSpace(_body))
                {
                    try
                    {
                        _pointerBody = JsonConvert.DeserializeObject<NrlsPointerBody>(_body);
                    }
                    catch(Exception ex)
                    {
                        return CommandResponse.Set(false, $"The Body OPTION is invalid. Exception Message: {ex.Message}");
                    }

                }
                else if (!string.IsNullOrWhiteSpace(_input))
                {
                    var inputLocation = GetInputLocation();

                    if (inputLocation == null)
                    {
                        return CommandResponse.Set(false, $"The Input OPTION is invalid. See --help for more details.");
                    }
                    else
                    {
                        if (!inputLocation.Success)
                        {
                            return inputLocation;
                        }

                        try
                        {
                            var jsonParser = new FhirJsonParser();
                            var pointer = File.ReadAllText(inputLocation.Result);
                            _pointer = jsonParser.Parse<DocumentReference>(pointer);
                        }
                        catch(Exception ex)
                        {
                            return CommandResponse.Set(false, $"Error trying to parse input. Exception Message: {ex.Message}");
                        }
                        
                       
                    }
                }
                else
                {
                    return CommandResponse.Set(false, $"Both the Body OPTION and the Input OPTION are missing. PUT and POST methods require at least one.");
                }
            }

            ////not for beta
            //if (_method == "put")
            //{
            //    //its now a parameter
            //    if (string.IsNullOrWhiteSpace(_uniqueId))
            //    {
            //        return CommandResponse.Set(false, $"The id OPTION is missing. PUT methods require a id.");
            //    }

            //    message.AppendLine("id: " + _uniqueId);
            //}

            return CommandResponse.Set(true, message.ToString());
        }