Exemplo n.º 1
0
        public static IHostEnvironment Build()
        {
            var environment = new ConsoleHostEnvironment();

            var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
                      ?? "Production";

            var assembly = Assembly.GetExecutingAssembly();

            environment.EnvironmentName = env;
            environment.ApplicationName = assembly.GetName().Name;
            environment.ContentRootPath = assembly.Location != null
                ? Path.GetDirectoryName(assembly.Location)
                : Environment.CurrentDirectory
            ;

            environment.ContentRootFileProvider =
                new PhysicalFileProvider(environment.ContentRootPath)
            ;

            return(environment);
        }
Exemplo n.º 2
0
        private static async Task MainAsync(string[] args)
        {
            var basePath    = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var environment = ConsoleHostEnvironment.Build();

            var configuration = new ConfigurationBuilder()
                                .SetBasePath(basePath)
                                .AddJsonFile("appSettings.json")
                                .AddJsonFile($"appSettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: false)
                                .Build()
            ;

            var section   = configuration.GetSection("AppSettings");
            var endpoints = new Endpoints();

            section.Bind(endpoints);

            Console.WriteLine($"masked-emails: Environment {environment.EnvironmentName}.");
            Console.WriteLine($"api: {endpoints.Api}.");
            Console.WriteLine($"authority: {endpoints.Authority}.");


            var cmdLine = CommandLine.Parse(args);

            var username = cmdLine.Username;
            var password = cmdLine.Password;

            var action  = cmdLine.Action;
            var options = cmdLine.Args;

            if (String.IsNullOrEmpty(username))
            {
                Console.Out.WriteLine("Login:"******"Password:"******"--password-hash", passwordHash, }).ToArray();
            }

            var credentials = new NetworkCredential(username, password);
            var client      = new Client.MaskedEmailsClient(endpoints, credentials);

            try
            {
                await client.GetProfileAsync();
            }
            catch (HttpRequestException e)
            {
                Console.Error.WriteLine($"ERR: unable to connect to the masked-emails service.");
                Console.Error.WriteLine($"ERR: {e.Message}.");
                Environment.Exit(42);
            }

            if (action == Actions.Unrecognized)
            {
                new ReadEvalPrintLoop(client).Prompt(options);
            }
            else
            {
                await Command.ExecuteAsync(client, action, options);
            }
        }