static async Task ConsoleHub(CommandParser parser) { // Initialize the prompt from the settings. if (SettingsManager.Exists("prompt")) { var prompt = SettingsManager.Get("prompt"); Ui.DefaultPrompt = prompt; } if (!SettingsManager.Exists("access_token")) { Console.WriteLine("Seems like you haven't logged in yet. Let's do it!"); var loginCmd = new LoginCommand(); await loginCmd.Execute(); } // Initialize the credentials of the user with the access token. var accessToken = SettingsManager.Get("access_token"); GHClient.SetCredentials(accessToken); // Get the current logged in user to show the data. var currentUser = await GHClient.client.User.Current(); Ui.WriteLineGreen($"You're logged in as {currentUser.Name} ({currentUser.Email})"); string[] dividedCommand; while (true) { Ui.WritePrompt(); var input = Console.ReadLine(); dividedCommand = parser.SplitInput(input); Command cmd = null; try { cmd = parser.ParseCommand(dividedCommand); } catch (Exception error) { Ui.WriteLineRed(error.Message); } if (cmd != null) { Ui.NewLine(); await cmd.Execute(); } } }
public override async Task Execute() { // Check first if we're already logged in. if (SettingsManager.Exists("access_token")) { Console.WriteLine("You're already logged in. Log out first if you want to switch accounts."); return; } var loginUrl = GHClient.GetLoginUrl(); Console.WriteLine("This app uses OAuth to log into your account."); Console.WriteLine("The app will attempt to open a window in 5 seconds, in case it doesn't work"); Console.WriteLine("Open this URL:"); Console.WriteLine(loginUrl); System.Threading.Thread.Sleep(5000); Process.Start(loginUrl.ToString()); Console.WriteLine("...And once you finish, copy the URL and press any key. I'll be waiting."); Console.ReadKey(); Console.WriteLine("Ready? Great! Copy the URL that you got over here."); var responseUrl = Console.ReadLine(); if (String.IsNullOrEmpty(responseUrl) || !responseUrl.StartsWith("http://localhost/oauth") || !responseUrl.Contains("?code=")) { Console.WriteLine("That doesn't seem like a valid URL >.<"); Environment.Exit(0); } Console.WriteLine("Nice! Wait a second, we're getting your access token..."); var accessToken = await GHClient.GetAccessToken(responseUrl); GHClient.SetCredentials(accessToken); // Save this access token. SettingsManager.Set("access_token", accessToken); }