Esempio n. 1
0
        /// <summary>
        /// Runs the sync command
        /// </summary>
        /// <param name="options">Fetch command options</param>
        public static void FetchUpdates(FetchUpdatesOptions options)
        {
            Endpoint upstreamEndpoint;
            CompressedMetadataStore baselineSource;

            Console.WriteLine("Opening query results file");
            baselineSource = CompressedMetadataStore.Open(options.MetadataSourcePath);
            if (baselineSource == null)
            {
                ConsoleOutput.WriteRed("Cannot open the query result file!");
                return;
            }

            ConsoleOutput.WriteGreen("Done!");
            upstreamEndpoint = baselineSource.UpstreamSource;

            // The filter to apply when fetching updates
            QueryFilter queryToRun = null;

            queryToRun = baselineSource.Filters.FirstOrDefault();
            if (queryToRun != null)
            {
                ConsoleOutput.WriteCyan("Using the filter from the baseline source. Command line filters are ignored!");
            }

            if (queryToRun == null)
            {
                // A new query will be created based on command line options
                try
                {
                    queryToRun = MetadataSync.CreateValidFilterFromOptions(options, baselineSource);
                }
                catch (Exception ex)
                {
                    ConsoleOutput.WriteRed(ex.Message);
                    return;
                }
            }

            using (var syncResult = new CompressedMetadataStore(baselineSource))
            {
                if (!string.IsNullOrEmpty(options.AccountName) && !string.IsNullOrEmpty(options.AccountGuid))
                {
                    if (!Guid.TryParse(options.AccountGuid, out Guid accountGuid))
                    {
                        ConsoleOutput.WriteRed("The account GUID must be a valid GUID string");
                        return;
                    }

                    syncResult.SetUpstreamCredentials(options.AccountName, accountGuid);
                }

                FetchUpdates(queryToRun, syncResult);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create a valid filter for retrieving updates
        /// </summary>
        /// <param name="options">The user's commandline options with intended filter</param>
        /// <param name="metadataSource">Metadata source that contains the list of known categories and classifications</param>
        /// <returns>A query filter that can be used to selectively retrieve updates from the upstream server</returns>
        private static QueryFilter CreateValidFilterFromOptions(FetchUpdatesOptions options, IMetadataSource metadataSource)
        {
            var productFilter        = new List <Product>();
            var classificationFilter = new List <Classification>();

            // If a classification is specified then categories is also required, regardless of user option. Add all categories in this case.
            bool allProductsRequired = options.ProductsFilter.Count() == 0 || options.ProductsFilter.Contains("all");

            // If category is specified then classification is also required, regardless of user option. Add all classifications in this case.
            bool allClassificationsRequired = options.ClassificationsFilter.Count() == 0 || options.ClassificationsFilter.Contains("all");

            if (allProductsRequired)
            {
                productFilter.AddRange(metadataSource.ProductsIndex.Values);
            }
            else
            {
                foreach (var categoryGuidString in options.ProductsFilter)
                {
                    var categoryGuid    = new Guid(categoryGuidString);
                    var matchingProduct = metadataSource.ProductsIndex.Values.Where(category => category.Identity.ID == categoryGuid);

                    if (matchingProduct.Count() == 0)
                    {
                        throw new Exception($"Could not find a match for product filter {categoryGuidString}");
                    }

                    productFilter.Add(matchingProduct.First());
                }
            }

            if (allClassificationsRequired)
            {
                classificationFilter.AddRange(metadataSource.ClassificationsIndex.Values);
            }
            else
            {
                foreach (var classificationGuidString in options.ClassificationsFilter)
                {
                    var classificationGuid     = new Guid(classificationGuidString);
                    var matchingClassification = metadataSource.ClassificationsIndex.Values.Where(classification => classification.Identity.ID == classificationGuid);

                    if (matchingClassification.Count() == 0)
                    {
                        throw new Exception($"Could not find a match for classification filter {classificationGuidString}");
                    }

                    classificationFilter.Add(matchingClassification.First());
                }
            }

            return(new QueryFilter(productFilter, classificationFilter));
        }