Ejemplo n.º 1
0
        private static void Sources(ArgList args)
        {
            var command = args.PopCommand()?.ToLowerInvariant();

            switch (command)
            {
            case "list":
                list();
                break;

            case "display":
                display();
                break;

            case "create":
                create();
                break;

            case "delete":
                delete();
                break;

            case "default":
                defaultCommand();
                break;

            default:
                Console.WriteLine("Usage:");
                Console.WriteLine("romp sources list");
                Console.WriteLine("romp sources display <name> [--show-hidden]");
                Console.WriteLine("romp sources create <name> <url>");
                Console.WriteLine("romp sources delete <name>");
                break;
            }

            void list()
            {
                bool any = false;

                Console.WriteLine("Package sources:");

                foreach (var s in RompDb.GetPackageSources())
                {
                    any = true;
                    var url = s.FeedUrl;
                    if (!string.IsNullOrEmpty(s.UserName))
                    {
                        url = s.UserName + "@" + url;
                    }
                    Console.WriteLine(" " + s.Name + ": " + url);
                }

                if (!any)
                {
                    Console.WriteLine(" (none)");
                }
            }

            void display()
            {
                var name = args.PopCommand();

                if (string.IsNullOrEmpty(name))
                {
                    throw new RompException("Expected source name.");
                }

                var source = RompDb.GetPackageSources()
                             .FirstOrDefault(s => string.Equals(s.Name, name, StringComparison.OrdinalIgnoreCase));

                if (source == null)
                {
                    throw new RompException($"Source {name} not found.");
                }

                bool showHidden = false;

                args.ProcessOptions(
                    o =>
                {
                    if (string.Equals(o.Key, "show-hidden", StringComparison.OrdinalIgnoreCase))
                    {
                        showHidden = true;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                    );

                args.ThrowIfAnyRemaining();

                Console.WriteLine("Name: " + source.Name);
                Console.WriteLine("Url: " + source.FeedUrl);
                Console.WriteLine("User: "******"(not specified)");
                if (showHidden)
                {
                    Console.WriteLine("Password: "******"(not specified)");
                }
            }

            void create()
            {
                var name = args.PopCommand();
                var url  = args.PopCommand();

                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(url))
                {
                    throw new RompException("Usage: romp sources create <name> <url>");
                }

                Uri uri;

                try
                {
                    uri = new Uri(url);
                }
                catch (Exception ex)
                {
                    throw new RompException("Invalid URL: " + ex.Message, ex);
                }

                string       userName = null;
                SecureString password = null;

                if (!string.IsNullOrEmpty(uri.UserInfo))
                {
                    var parts = uri.UserInfo.Split(new[] { ':' }, 2);
                    userName = parts[0];
                    password = AH.CreateSecureString(parts.Length > 1 ? parts[1] : string.Empty);
                }

                var sanitizedUrl = new UriBuilder(uri)
                {
                    UserName = null,
                    Password = null
                };

                RompDb.CreateOrUpdatePackageSource(name, sanitizedUrl.ToString(), userName, password);
                Console.WriteLine("Package source stored.");
            }

            void delete()
            {
                var name = args.PopCommand();

                if (string.IsNullOrEmpty(name))
                {
                    throw new RompException("Expected source name.");
                }

                RompDb.DeletePackageSource(name);
                Console.WriteLine("Package source deleted.");

                if (string.Equals(RompConfig.DefaultSource, name, StringComparison.OrdinalIgnoreCase))
                {
                    RompConfig.DeleteValue("default-source");
                }
            }

            void defaultCommand()
            {
                var name = args.PopCommand();

                if (string.IsNullOrEmpty(name))
                {
                    throw new RompException("Expected source name.");
                }

                if (!RompDb.GetPackageSources().Any(s => string.Equals(s.Name, name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new RompException($"No source named {name} has been configured.");
                }

                RompConfig.SetValue("default-source", name);
                Console.WriteLine($"Default source set to {name}.");
            }
        }
Ejemplo n.º 2
0
        private static void Config(ArgList args)
        {
            var command = args.PopCommand()?.ToLowerInvariant();

            switch (command)
            {
            case "list":
                list();
                break;

            case "export":
                export();
                break;

            case "set":
                set();
                break;

            case "delete":
                delete();
                break;

            default:
                Console.WriteLine("Usage:");
                Console.WriteLine("romp config list");
                Console.WriteLine("romp config export <file-name> [--overwrite] [--all]");
                Console.WriteLine("romp config set <key> <value> [--machine]");
                Console.WriteLine("romp config delete <key>");
                break;
            }

            void list()
            {
                bool showAll = false;

                args.ProcessOptions(
                    o =>
                {
                    if (o.Key == "all")
                    {
                        showAll = true;
                        return(true);
                    }

                    return(false);
                }
                    );

                foreach (var p in typeof(RompConfigValues).GetProperties())
                {
                    var name  = p.GetCustomAttribute <JsonPropertyAttribute>()?.PropertyName;
                    var value = p.GetValue(showAll ? RompConfig.Values : RompConfig.OverriddenValues);
                    if (!string.IsNullOrEmpty(name) && !Attribute.IsDefined(p, typeof(NotCascadedAttribute)) && value != null)
                    {
                        Console.WriteLine(name + "=" + p.GetValue(RompConfig.Values));
                    }
                }
            }

            void export()
            {
                bool includeAll = false;
                bool overwrite  = false;

                args.ProcessOptions(
                    o =>
                {
                    if (o.Key == "all")
                    {
                        includeAll = true;
                        return(true);
                    }
                    else if (o.Key == "overwrite")
                    {
                        overwrite = true;
                        return(true);
                    }

                    return(false);
                }
                    );

                var fileName = args.PopCommand();

                if (string.IsNullOrEmpty(fileName))
                {
                    throw new RompException("Usage: romp config export <file-name> [--overwrite] [--all]");
                }

                args.ThrowIfAnyRemaining();

                if (!overwrite && File.Exists(fileName))
                {
                    throw new RompException($"File {fileName} already exists. Use --overwrite if overwriting is intentional.");
                }

                using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                    using (var writer = new StreamWriter(fileStream, InedoLib.UTF8Encoding))
                    {
                        var serializer = JsonSerializer.Create(new JsonSerializerSettings {
                            Formatting = Formatting.Indented
                        });
                        serializer.Serialize(writer, includeAll ? RompConfig.Values : RompConfig.OverriddenValues);
                    }

                Console.WriteLine("Configuration written to " + fileName);
            }

            void set()
            {
                var key = args.PopCommand();

                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new RompException("Usage: romp config set <key> <value>");
                }

                var value = args.PopCommand();

                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new RompException("Usage: romp config set <key> <value>");
                }

                RompConfig.SetValue(key, value);
                Console.WriteLine("Config value set.");
            }

            void delete()
            {
                var key = args.PopCommand();

                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new RompException("Usage: romp config delete <key>");
                }

                RompConfig.DeleteValue(key);
                Console.WriteLine("Config value deleted.");
            }
        }