private static void BuildSettingsFile(string file, NpgsqlConnection connection, string pgroutinerSettingsFile) { try { File.WriteAllText(file, BuildFormatedSettings(connection: connection)); Program.WriteLine(""); Program.Write(ConsoleColor.Yellow, $"Settings file "); Program.Write(ConsoleColor.Cyan, pgroutinerSettingsFile); Program.WriteLine(ConsoleColor.Yellow, $" successfully created!", ""); Program.Write(ConsoleColor.Yellow, "- Run "); Program.Write(ConsoleColor.Cyan, $"pgroutiner {SettingsArgs.Alias}"); Program.Write(ConsoleColor.Yellow, " or "); Program.Write(ConsoleColor.Cyan, $"pgroutiner {SettingsArgs.Name}"); Program.WriteLine(ConsoleColor.Yellow, " to see current settings and switches."); Program.Write(ConsoleColor.Yellow, "- Run "); Program.Write(ConsoleColor.Cyan, $"pgroutiner {HelpArgs.Alias}"); Program.Write(ConsoleColor.Yellow, " or "); Program.Write(ConsoleColor.Cyan, $"pgroutiner {HelpArgs.Name}"); Program.WriteLine(ConsoleColor.Yellow, " to see help on available commands.", ""); } catch (Exception e) { Program.DumpError($"File {pgroutinerSettingsFile} could not be written: {e.Message}"); } }
private static void PgDumpError(string connVersion, Exception e, string option) { Program.WriteLine(""); Program.Write(ConsoleColor.Red, "ERROR - pg_dump returned an error: "); Program.WriteLine(ConsoleColor.Red, e.Message); Program.WriteLine(ConsoleColor.Red, "PostgreSQL might not be installed on your computer."); Program.WriteLine(ConsoleColor.Red, "You will not be able to create database dump files (schema file, data dump file or object tree dir), unless versions match!"); Program.Write(ConsoleColor.Red, "Make sure you have access to "); Program.Write(ConsoleColor.Red, "pg_dump"); Program.Write(ConsoleColor.Red, " version "); Program.Write(ConsoleColor.Red, connVersion); Program.Write(ConsoleColor.Red, " and, the settings key "); Program.Write(ConsoleColor.Red, option); Program.WriteLine(ConsoleColor.Red, " points to the correct path."); }
public static bool ParseInitialSettings(NpgsqlConnection connection, bool haveArguments, string pgroutinerSettingsFile) { var pgroutinerFile = Path.Join(Program.CurrentDir, pgroutinerSettingsFile); var exists = File.Exists(Path.Join(pgroutinerFile)); if (!exists && Program.Config.GetSection("PgRoutiner").GetChildren().Count() == 0 && !haveArguments) { Program.WriteLine(ConsoleColor.Yellow, "", "You don't seem to be using any available command-line commands and PgRoutiner configuration seems to be missing."); Program.Write(ConsoleColor.Yellow, $"Would you like to create a custom settings file \""); Program.Write(ConsoleColor.Cyan, pgroutinerSettingsFile); Program.WriteLine(ConsoleColor.Yellow, "\" with your current values?", "This settings configuration file can be used to change settings for this directory without using a command-line."); Program.Write(ConsoleColor.Yellow, $"Create \""); Program.Write(ConsoleColor.Cyan, pgroutinerSettingsFile); Program.WriteLine(ConsoleColor.Yellow, "\" in this dir [Y/N]?"); var answer = Program.Ask(null, ConsoleKey.Y, ConsoleKey.N); if (answer == ConsoleKey.Y) { BuildSettingsFile(pgroutinerFile, connection, pgroutinerSettingsFile); } } var routineCount = connection.GetRoutineCount(Value); var crudCount = connection.GetTableDefintionsCount(Value); if ((Value.Routines && Value.OutputDir != null && routineCount > 0) || (Value.Crud && Value.CrudOutputDir != null && crudCount > 0) || (Value.UnitTests && Value.UnitTestsDir != null)) { ProjectInfo = ParseProjectFile(); if (ProjectInfo == null) { return(false); } if (Value.Routines) { UpdateProjectReferences(ProjectInfo); } } return(true); }
public static void ShowSettings() { Program.WriteLine("", "Current settings:"); bool comment = false; foreach (var l in Settings.BuildFormatedSettings(wrap: false).Split(Environment.NewLine)) { var line = l; var trim = l.Trim(); if (trim.StartsWith("/*") || comment) { Program.WriteLine(ConsoleColor.Green, $" {line.Replace(" ", "")}"); comment = true; } else if (trim.StartsWith("\"")) { var split = line.Split(':', 2, StringSplitOptions.RemoveEmptyEntries); var key = split.Length == 2 ? split[0].Replace("\"", "") : null; var value = split.Length == 2 ? split[1].Trim() : split[0].Trim(); if (key != null) { Program.Write(ConsoleColor.Yellow, key.Replace(" ", " ")); Program.Write(": "); Program.WriteLine(ConsoleColor.Cyan, value); } else { Program.WriteLine(ConsoleColor.Cyan, $" {value}"); } } else { Program.WriteLine(ConsoleColor.Cyan, line.Replace(" ", " ")); } if (trim.Contains("*/") && comment) { comment = false; } } ShowSettingsLink(); }
public static void DumpFormat(string msg, params object[] values) { if (Settings.Value.Dump) { return; } msg = string.Format(msg, values.Select(v => $"`{v}`").ToArray()); foreach (var(line, i) in msg.Split('`').Select((l, i) => (l, i))) { if (i % 2 == 0) { Program.Write(ConsoleColor.Yellow, line); } else { Program.Write(ConsoleColor.Cyan, line); } } Program.WriteLine(""); }
private static void PgDumpMistmatch(NpgsqlConnection connection, string connVersion, string fullDumpVersion, string dumpVersion, object value, string option) { Program.WriteLine(""); Program.WriteLine(ConsoleColor.Red, "ERROR: It looks like pg_dump version mismatch: "); Program.Write(ConsoleColor.Red, "- Connection "); Program.Write(ConsoleColor.Red, Value.Connection); Program.Write(ConsoleColor.Red, " (server "); Program.Write(ConsoleColor.Red, connection.Host); Program.Write(ConsoleColor.Red, ", port "); Program.Write(ConsoleColor.Red, connection.Port.ToString()); Program.Write(ConsoleColor.Red, ", database "); Program.Write(ConsoleColor.Red, connection.Database); Program.Write(ConsoleColor.Red, ") is version "); Program.Write(ConsoleColor.Red, connVersion); Program.Write(ConsoleColor.Red, " ("); Program.Write(ConsoleColor.Red, connection.ServerVersion); Program.WriteLine(ConsoleColor.Red, ")"); Program.Write(ConsoleColor.Red, "- Default "); Program.Write(ConsoleColor.Red, "pg_dump"); Program.Write(ConsoleColor.Red, " path ("); Program.Write(ConsoleColor.Red, $"{value}"); Program.Write(ConsoleColor.Red, ") is version "); Program.Write(ConsoleColor.Red, dumpVersion); Program.Write(ConsoleColor.Red, " ("); Program.Write(ConsoleColor.Red, fullDumpVersion); Program.WriteLine(ConsoleColor.Red, ")"); Program.WriteLine(ConsoleColor.Red, "You will not be able to create database dump files (schema file, data dump files, object tree dir or schema diff), unless versions match!"); Program.Write(ConsoleColor.Red, "Make sure you have access to "); Program.Write(ConsoleColor.Red, "pg_dump"); Program.Write(ConsoleColor.Red, " version "); Program.Write(ConsoleColor.Red, connVersion); Program.Write(ConsoleColor.Red, " and, the settings key "); Program.Write(ConsoleColor.Red, option); Program.WriteLine(ConsoleColor.Red, " points to the correct path."); }
public static void ShowUpdatedSettings() { bool settingsWritten = false; void WriteSetting(string name, object value, System.Type type) { if (!settingsWritten) { Program.WriteLine(""); Program.WriteLine("Using settings: "); } if (value == null) { Program.Write(ConsoleColor.Yellow, $" {name.ToKebabCase()} = "); Program.WriteLine(ConsoleColor.Cyan, "null"); } else if (type == typeof(bool) && (bool)value) { Program.WriteLine(ConsoleColor.Yellow, $" {name.ToKebabCase()}"); } else { Program.Write(ConsoleColor.Yellow, $" {name.ToKebabCase()} = "); if (type == typeof(IList <string>)) { Program.WriteLine(ConsoleColor.Cyan, $"{string.Join(", ", (value as IList<string>).ToArray())}"); } else if (type == typeof(HashSet <string>)) { Program.WriteLine(ConsoleColor.Cyan, $"{string.Join(", ", (value as HashSet<string>).ToArray())}"); } else { Program.WriteLine(ConsoleColor.Cyan, $"{value}"); } } settingsWritten = true; } var defaultValue = new Settings(); foreach (var prop in Value.GetType().GetProperties()) { var name = prop.Name; if (prop.PropertyType == typeof(bool)) { var v1 = (bool)prop.GetValue(Value); var v2 = (bool)prop.GetValue(defaultValue); if (v1 == v2) { continue; } WriteSetting(prop.Name, v1, prop.PropertyType); } if (prop.PropertyType == typeof(string)) { var s1 = (string)prop.GetValue(Value); var s2 = (string)prop.GetValue(defaultValue); if (string.Equals(s1, s2)) { continue; } WriteSetting(prop.Name, s1, prop.PropertyType); } if (prop.PropertyType == typeof(IList <string>)) { var l1 = (IList <string>)prop.GetValue(Value); var l2 = (IList <string>)prop.GetValue(defaultValue); if (l1.SequenceEqual(l2)) { continue; } WriteSetting(prop.Name, l1, prop.PropertyType); } if (prop.PropertyType == typeof(HashSet <string>)) { var l1 = (HashSet <string>)prop.GetValue(Value); var l2 = (HashSet <string>)prop.GetValue(defaultValue); if (l1.SequenceEqual(l2)) { continue; } WriteSetting(prop.Name, l1, prop.PropertyType); } } }