private static void Pack(ArgList args) { var source = args.PopCommand(); if (string.IsNullOrEmpty(source)) { throw new RompException("Usage: romp pack <source-directory> [output-file-name] [--overwrite]"); } var packageName = args.PopCommand(); bool overwrite = false; args.ProcessOptions(parseOption); args.ThrowIfAnyRemaining(); PackageBuilder.BuildPackage(Path.GetFullPath(source), packageName, overwrite); bool parseOption(ArgOption o) { switch (o.Key.ToLowerInvariant()) { case "overwrite": overwrite = true; return(true); default: return(false); } } }
public static PackageSpecifier FromArgs(ArgList args) { var packageName = args.PopCommand(); if (string.IsNullOrEmpty(packageName)) { return(null); } var inst = new PackageSpecifier(); args.ProcessOptions(inst.ParseOption); // set source to default if not already specified if (inst.Source == null && !string.IsNullOrWhiteSpace(RompConfig.DefaultSource)) { var match = RompDb.GetPackageSources() .FirstOrDefault(s => string.Equals(s.Name, RompConfig.DefaultSource, StringComparison.OrdinalIgnoreCase)); if (match != null) { if (string.IsNullOrEmpty(match.UserName) || match.Password == null) { inst.Source = new UniversalFeedEndpoint(match.FeedUrl, true); } else { inst.Source = new UniversalFeedEndpoint(new Uri(match.FeedUrl), match.UserName, match.Password); } } } if (packageName.EndsWith(".upack", StringComparison.OrdinalIgnoreCase)) { if (inst.Source != null) { throw new RompException("--source cannot be specified if <packageName> refers to a file."); } if (inst.PackageVersion != null) { throw new RompException("--version cannot be specified if <packageName> refers to a file."); } inst.FileName = packageName; } else { try { inst.PackageId = UniversalPackageId.Parse(packageName); } catch (Exception ex) { throw new RompException("Invalid package name: " + packageName, ex); } } return(inst); }
private static void Credentials(ArgList args) { var command = args.PopCommand()?.ToLowerInvariant(); switch (command) { case "list": list(); break; case "display": display(); break; case "store": store(); break; case "delete": delete(); break; default: Console.WriteLine("Usage:"); Console.WriteLine("romp credentials list"); Console.WriteLine("romp credentials display <name> [--show-hidden]"); Console.WriteLine("romp credentials store <name>"); Console.WriteLine("romp credentials delete <name>"); break; } void list() { foreach (var c in RompDb.GetCredentials()) { Console.WriteLine(c.CredentialType_Name + "::" + c.Credential_Name); } } void display() { var(type, name) = parseQualifiedName(); var creds = RompDb.GetCredentialsByName(type, name); if (creds == null) { throw new RompException($"Credentials {type}::{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(); var instance = (ResourceCredentials)Persistence.DeserializeFromPersistedObjectXml(creds.Configuration_Xml); Console.WriteLine($"Name: {creds.CredentialType_Name}::{creds.Credential_Name}"); foreach (var prop in Persistence.GetPersistentProperties(instance.GetType(), false)) { var alias = prop.GetCustomAttribute <ScriptAliasAttribute>()?.Alias; if (alias != null) // only show items with ScriptAlias { var propName = prop.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName ?? alias; bool hidden = prop.GetCustomAttribute <PersistentAttribute>().Encrypted; var value = prop.GetValue(instance); if (value is SecureString secure) { value = AH.Unprotect(secure); } if (hidden && !showHidden) { value = "(hidden)"; } if (value == null) { value = "(not specified)"; } Console.WriteLine(propName + ": " + value); } } } void store() { var n = parseQualifiedName(); var type = (from c in ExtensionsManager.GetComponentsByBaseClass <ResourceCredentials>() let a = c.ComponentType.GetCustomAttribute <ScriptAliasAttribute>() where string.Equals(a?.Alias, n.type, StringComparison.OrdinalIgnoreCase) || string.Equals(c.ComponentType.Name, n.type, StringComparison.OrdinalIgnoreCase) orderby string.Equals(a?.Alias, n.type, StringComparison.OrdinalIgnoreCase) descending select c.ComponentType).FirstOrDefault(); if (type == null) { throw new RompException($"Unknown credentials type \"{n.type}\". Are you missing an extension?"); } var credentials = (ResourceCredentials)Activator.CreateInstance(type); if (!Console.IsInputRedirected) { foreach (var property in Persistence.GetPersistentProperties(type, true)) { Again: Console.Write((property.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName ?? property.Name) + ": "); string value; if (property.GetCustomAttribute <PersistentAttribute>()?.Encrypted == true || property.PropertyType == typeof(SecureString)) { value = ReadSensitive(); } else { value = Console.ReadLine(); } if (!string.IsNullOrEmpty(value)) { if (property.PropertyType == typeof(string)) { property.SetValue(credentials, value); } else if (property.PropertyType == typeof(SecureString)) { property.SetValue(credentials, AH.CreateSecureString(value)); } else { try { var convertedValue = Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); property.SetValue(credentials, convertedValue); } catch (Exception ex) { Console.Error.WriteLine("Invalid value: " + ex.Message); goto Again; } } } } } else { throw new RompException("Credentials must be stored interactively."); } RompDb.CreateOrUpdateCredentials(n.name, credentials, true); Console.WriteLine("Credentials stored."); } void delete() { var(type, name) = parseQualifiedName(); RompDb.DeleteCredentials(type, name); Console.WriteLine("Credentials deleted."); } (string type, string name) parseQualifiedName() { var qualifiedName = args.PopCommand(); if (string.IsNullOrEmpty(qualifiedName)) { throw new RompException("Expected credentials name."); } var parts = qualifiedName.Split(new[] { "::" }, StringSplitOptions.None); if (parts.Length != 2 || string.IsNullOrWhiteSpace(parts[0]) || string.IsNullOrWhiteSpace(parts[1])) { throw new RompException("Invalid credentials name specification."); } return(parts[0], parts[1]); } }
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}."); } }
private static async Task Uninstall(ArgList args) { var spec = PackageSpecifier.FromArgs(args); if (spec == null) { throw new RompException("Usage: romp uninstall <package> [-Vvar=value...]"); } Console.WriteLine("Package: " + spec); Console.WriteLine(); var registeredPackage = await GetRegisteredPackageAsync(spec.PackageId); if (registeredPackage == null) { throw new RompException("Package is not installed."); } spec.PackageVersion = UniversalPackageVersion.Parse(registeredPackage.Version); await ExtensionsManager.WaitForInitializationAsync(); var vars = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); using (var package = await spec.FetchPackageAsync(args, default)) { args.ProcessOptions(parseOption); foreach (var var in vars) { RompSessionVariable.SetSessionVariable(var.Key, var.Value); } var packageInfo = RompPackInfo.Load(package); if (packageInfo.WriteScriptErrors()) { throw new RompException("Error compiling uninstall script."); } PackageInstaller.TargetDirectory = registeredPackage.InstallPath; RompSessionVariable.SetSessionVariable("TargetDirectory", registeredPackage.InstallPath); await PackageInstaller.RunAsync(package, "uninstall.otter", false); using (var registry = PackageRegistry.GetRegistry(RompConfig.UserMode)) { await registry.LockAsync(); await registry.UnregisterPackageAsync(registeredPackage); await registry.DeleteFromCacheAsync(spec.PackageId, package.Version); await registry.UnlockAsync(); } } bool parseOption(ArgOption o) { if (o.Key.StartsWith("V") && o.Key.Length > 1) { vars[o.Key.Substring(1)] = o.Value ?? string.Empty; return(true); } return(false); } }
private static async Task Install(ArgList args) { var spec = PackageSpecifier.FromArgs(args); if (spec == null) { throw new RompException("Usage: romp install <package-file-or-name> [--version=<version-number>] [--source=<name-or-feed-url>] [--force] [-Vvar=value...]"); } Console.WriteLine("Package: " + spec); Console.WriteLine(); await ExtensionsManager.WaitForInitializationAsync(); bool simulate = false; bool force = false; var vars = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); using (var package = await spec.FetchPackageAsync(args, default)) { args.ProcessOptions(parseOption); if (!force) { var registeredPackage = await GetRegisteredPackageAsync(spec.PackageId); if (registeredPackage != null) { Console.WriteLine("Package is already installed. Use --force to install anyway."); return; } } foreach (var var in vars) { RompSessionVariable.SetSessionVariable(var.Key, var.Value); } var packageInfo = RompPackInfo.Load(package); if (packageInfo.WriteScriptErrors()) { throw new RompException("Error compiling install script."); } foreach (var var in packageInfo.Variables) { if (var.Value.Value != null && !vars.ContainsKey(var.Key)) { RompSessionVariable.SetSessionVariable(var.Key, var.Value.Value.Value); } } foreach (var var in packageInfo.Variables) { // should also validate/coerce type here if (var.Value.Required && var.Value.Value == null && !vars.ContainsKey(var.Key)) { if (Console.IsOutputRedirected) { throw new RompException("Missing required variable: " + var.Key); } Console.WriteLine($"Variable \"{var.Key}\" is required."); if (!string.IsNullOrWhiteSpace(var.Value.Description)) { Console.WriteLine("Description: " + var.Value.Description); } string value; do { // should not assume type to be scalar Console.Write(new RuntimeVariableName(var.Key, RuntimeValueType.Scalar) + ": "); if (var.Value.Sensitive) { value = ReadSensitive(); } else { value = Console.ReadLine(); } }while (string.IsNullOrEmpty(value)); RompSessionVariable.SetSessionVariable(var.Key, value); } } bool credentialsMissing = false; foreach (var creds in packageInfo.Credentials.Values) { if (RompDb.GetCredentialsByName(creds.Type, creds.Name) == null) { credentialsMissing = true; var text = "Credentials required: " + creds.FullName; if (!string.IsNullOrWhiteSpace(creds.Description)) { text += " (" + creds.Description + ")"; } RompConsoleMessenger.WriteDirect(text, ConsoleColor.Red); } } if (credentialsMissing) { throw new RompException("Use \"romp credentials store\" to create missing credentials."); } await PackageInstaller.RunAsync(package, "install.otter", simulate); using (var registry = PackageRegistry.GetRegistry(RompConfig.UserMode)) { await registry.LockAsync(); await registry.RegisterPackageAsync( new RegisteredPackage { Group = package.Group, Name = package.Name, Version = package.Version.ToString(), InstallationDate = DateTimeOffset.Now.ToString("o"), InstalledBy = Environment.UserName, InstalledUsing = "Romp", InstallPath = PackageInstaller.TargetDirectory } ); await registry.UnlockAsync(); } } bool parseOption(ArgOption o) { switch (o.Key.ToLowerInvariant()) { case "force": force = true; return(true); } if (o.Key.StartsWith("V") && o.Key.Length > 1) { vars[o.Key.Substring(1)] = o.Value ?? string.Empty; return(true); } return(false); } }
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."); } }