Ejemplo n.º 1
0
        private static RunContext InitializeContext(RunArgs run)
        {
            // init DB connection
            var ctx = new RunContext
            {
                DbType = run.DbType,
                Connection = run.Connection
            };

            if (ctx.Connection != null)
            {
                Verbose.WriteLine($"DB type: {ctx.DbType.ToString().ToUpper()}");
                Verbose.WriteLine($"DB connection: {ctx.Connection}");
            }

            // choose target path
            ctx.TargetPath = Environment.CurrentDirectory;
            if (!String.IsNullOrWhiteSpace(run.CustomPath))
                ctx.TargetPath = run.CustomPath;

            if (!Directory.Exists(ctx.TargetPath))
            {
                XConsole.NewPara().Warning.WriteLine("Cannot locate target path");
                XConsole.Write("The following target path was not found: ").Cyan.WriteLine(ctx.TargetPath);
                XConsole.Write("When you use ").Yellow.Write("-p").Default.WriteLine(" parameter to specify custom path, make sure it points to existing location");
                return null;
            }

            Verbose.WriteLine($"Target path: {ctx.TargetPath}");

            // get log path
            ctx.LogPath = Path.Combine(ctx.TargetPath, "log");
            Verbose.Write($"Log path: {ctx.LogPath}");

            if (!Directory.Exists(ctx.LogPath))
            {
                Directory.CreateDirectory(ctx.LogPath);
                Verbose.Write(" (created)");
            }

            Verbose.WriteLine();

            // optionally use "soft" reset
            ctx.UseSoftReset = run.UseSoftReset;

            return ctx;
        }
Ejemplo n.º 2
0
        private static RunArgs ParseArguments(IEnumerable<string> arguments)
        {
            if (arguments == null)
                return null;

            var args = arguments.ToList();
            if (args.Count < 1)
                return null;

            // parse command name
            if (!TryParseEnum<RunCommand>(args[0], out var cmd))
                return null;

            var run = new RunArgs { Command = cmd };
            args.RemoveAt(0);

            // read -v separately, since it affects logging
            if (args.Contains("-v"))
            {
                Verbose.Enabled = true;
                Verbose.WriteLine("Enabled verbose output");
            }

            // read -db separately, since it affects how other options work
            var db = args.FindIndex(i => i == "-db");
            if (db >= 0)
            {
                Verbose.WriteLine("Parsing DB type...");
                run.DbType = ParseDbType(args[db + 1]);
            }

            // parse required arguments based on command
            switch (run.Command)
            {
                case RunCommand.Reset:
                    if (args.Count > 0 && !args[0].StartsWith("-"))
                    {
                        Verbose.WriteLine("Parsing connection...");
                        run.Connection = ParseConnection(run.DbType, args[0]);
                        args.RemoveAt(0);
                    }
                    break;
            }

            // parse all options
            for (var i = 0; i < args.Count; i++)
            {
                var arg = args[i];
                switch (arg.ToLower())
                {
                    case "-v":
                        continue;

                    case "-db":
                        i += 1;
                        continue;

                    case "-p":
                        Verbose.WriteLine("Parsing custom path...");
                        run.CustomPath = ParsePath(args[i + 1]);
                        i += 1;
                        continue;

                    case "-s":
                        Verbose.WriteLine("\"Soft\" reset will be performed instead of a regular one");
                        run.UseSoftReset = true;
                        continue;

                    case "-y":
                        Prompt.AlwaysYes = true;
                        Verbose.WriteLine("Prompt will be disabled");
                        continue;

                    default:
                        XConsole.Warning.Write("Unknown option:").Default.WriteLine($" {arg}");
                        break;
                }
            }

            return run;
        }