private static void SetupEnv() { userName = string.Empty; pat = string.Empty; configuration = null; selectedFile = string.Empty; SetConfigFolder(); SelectConfiguration(); if (configuration != null && configuration.UpdateGitReposOnConnect) { RefreshConfigFile(); } }
private static void SelectConfiguration() { while (configuration == null) { var configurations = Directory.GetFiles(directory, "*.gcconfig"); Console.WriteLine("Following Configurations File(s) Found. Enter index to select a configuration."); for (var index = 0; index < configurations.Length; index++) { var configuration = configurations[index]; var fileData = File.ReadAllText(configuration); var configData = JsonConvert.DeserializeObject <GitCloneConfiguration>(fileData); Console.WriteLine($"{index} : {configData.ConfigurationName} - {configData.Repos.Count} Repos"); } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("x: Create new Configuration"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("cd: Change Work Directory"); var input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { Console.WriteLine("Enter valid option"); } else if (input.Equals("x", StringComparison.InvariantCultureIgnoreCase)) { CreateConfiguration(); } else if (input.Equals("cd", StringComparison.InvariantCultureIgnoreCase)) { SetConfigFolder(); } else if (int.TryParse(input, out var selection)) { if (selection > configurations.Length - 1) { Console.WriteLine("Enter valid option"); } else { var selConfig = configurations[selection]; var fileData = File.ReadAllText(selConfig); var configData = JsonConvert.DeserializeObject <GitCloneConfiguration>(fileData); configuration = configData; selectedFile = selConfig; } } } }
private static List <GitCloneRepoConfiguration> GetServerRepos(GitCloneConfiguration config, string username, string password, IList <GitCloneRepoConfiguration> configRepos) { string authInfo = $"{username}:{password}"; authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo)); var client = new HttpClient { //BaseAddress = new Uri($"{config.GitOrgUrl}/api/v3/") }; client.DefaultRequestHeaders.Add("Authorization", "Basic " + authInfo); var clientData = client.GetAsync($"{config.GitOrgUrl}/api/v3/{orgs}").Result; var data = JsonConvert.DeserializeObject <IList <JsonOrgResponse> >(clientData.Content.ReadAsStringAsync() .Result); var repos = new List <GitCloneRepoConfiguration>(); foreach (var item in data) { client = new HttpClient(); client.DefaultRequestHeaders.Add("Authorization", "Basic " + authInfo); var repoData = client.GetAsync(item.repos_url + "?per_page=100").Result; var repoDetails = JsonConvert.DeserializeObject <IList <GitHubRepoModel> >(repoData.Content .ReadAsStringAsync() .Result); repos.AddRange(repoDetails.Where(b => b.permissions.pull).Select(b => new GitCloneRepoConfiguration() { DirectoryName = b.name, GitRepoPath = b.clone_url, RepoName = b.name, Ignore = configRepos.FirstOrDefault(x => x.RepoName == b.name)?.Ignore ?? false })); } return(repos); }
private static void CreateConfiguration() { var goodToGo = false; while (goodToGo == false) { Console.Write("Enter Configuration Name: "); var configName = Console.ReadLine(); Console.Write("Enter Work Dir (will be created if doesn't exist): "); var workDir = Console.ReadLine(); Console.Write("Is this GitHub Org (enterprise github) [Y/N]: "); var isGitOrgResponse = Console.ReadLine(); var isGitOrg = !string.IsNullOrEmpty(isGitOrgResponse) && (isGitOrgResponse == "y" || isGitOrgResponse == "Y"); string githubHostname = string.Empty; bool storeCredentials = false; string username = string.Empty; string localPat = string.Empty; string localPassword = string.Empty; bool refreshConfigOnConnect = false; if (isGitOrg) { Console.Write("Enter Enterprise github hostname with protocol. for example, for https://abcd.github.com/xyz, enter https://abcd.github.com: "); githubHostname = Console.ReadLine(); Console.Write("Refresh Config on connect [Y/N]: "); var refreshConfigOnConnectResponse = Console.ReadLine(); refreshConfigOnConnect = !string.IsNullOrEmpty(refreshConfigOnConnectResponse) && (refreshConfigOnConnectResponse == "y" || refreshConfigOnConnectResponse == "Y"); } Console.Write("Does operation requires authentication [Y/N]?"); var requirePassResponse = Console.ReadLine(); var requireAuth = !string.IsNullOrEmpty(requirePassResponse) && (requirePassResponse == "y" || requirePassResponse == "Y"); if (requireAuth) { Console.Write("Enter Username: "******"Enter PAT token: "); var localPat1 = ReadMaskedPassword(); Console.WriteLine(); Console.Write("Re-enter PAT token: "); var localPat2 = ReadMaskedPassword(); Console.WriteLine(); isPassOk = localPat1 == localPat2; localPat = localPat1; } while (!isPassOk); Console.Write("Do you want to store credentials. They will be stored as encrypted. You would need a password to use configuration [Y/N]: "); var storePasswordResponse = Console.ReadLine(); storeCredentials = !string.IsNullOrEmpty(storePasswordResponse) && (storePasswordResponse == "y" || storePasswordResponse == "Y"); if (storeCredentials) { isPassOk = false; do { Console.WriteLine(); Console.Write("Enter configuration password to encrypt credentials: "); var localPat1 = ReadMaskedPassword(); Console.WriteLine(); Console.Write("Re-enter configuration password to encrypt credentials: "); var localPat2 = ReadMaskedPassword(); Console.WriteLine(); isPassOk = !string.IsNullOrEmpty(localPat1) && localPat1 == localPat2 && localPat1.Length > 3; localPassword = localPat1; } while (!isPassOk); } } var repos = new List <GitCloneRepoConfiguration>(); if (!isGitOrg) { Console.WriteLine("Please start entering git urls. Include git path till .git."); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Please enter empty string in gitname to end git entry and save configuration"); Console.ForegroundColor = ConsoleColor.White; bool continueGitEntry; do { Console.Write("Enter Git URL: "); var gitUrl = Console.ReadLine(); continueGitEntry = !string.IsNullOrEmpty(gitUrl); if (!continueGitEntry) { break; } Console.Write("Enter repo name: "); var repoName = Console.ReadLine(); //abdc/de/de/fefefg.git //012345678901234567890 //10 , 17 if (string.IsNullOrEmpty(repoName)) { var index = gitUrl.LastIndexOf('/'); var index2 = gitUrl.IndexOf(".git"); repoName = gitUrl.Substring(index + 1, index2 - index - 1); } Console.Write($"Enter folder name [{repoName}]: "); var folderNameResponse = Console.ReadLine(); var folderName = string.IsNullOrEmpty(folderNameResponse) ? repoName : folderNameResponse; repos.Add(new GitCloneRepoConfiguration { GitRepoPath = gitUrl, DirectoryName = folderName, RepoName = repoName, Ignore = false }); } while (continueGitEntry); } else { //fetch from git hub } Console.WriteLine($"Configuration name: {configName}"); Console.WriteLine($"WorkDir: {workDir}"); Console.WriteLine($"Is Github Enterprise (org based): {isGitOrg}"); if (isGitOrg) { Console.WriteLine($"Enterprise github hostname: {githubHostname}"); Console.WriteLine($"Refresh Config: {refreshConfigOnConnect}"); } Console.WriteLine($"Require Auth: {requireAuth}"); if (requireAuth) { Console.WriteLine($"Store Credentials: {storeCredentials}"); Console.WriteLine($"Username: {username}"); Console.WriteLine($"PAT: ************"); if (storeCredentials) { Console.WriteLine($"Config password: {localPassword.Substring(0, 2)}**********"); } } if (isGitOrg) { Console.WriteLine("Repos to be fetched from server on runtime."); } else { Console.WriteLine("Repos"); foreach (var repo in repos) { Console.WriteLine($"-- {repo.GitRepoPath} in {repo.DirectoryName}"); } } Console.Write("Save Configuration [Y/N]: "); var saveConfigResponse = Console.ReadLine(); var saveConfig = !string.IsNullOrEmpty(saveConfigResponse) && (saveConfigResponse == "y" || saveConfigResponse == "Y"); if (saveConfig) { var fileName = Path.Combine(directory, $"{configName}.gcconfig"); var config = new GitCloneConfiguration { ConfigurationName = configName, IsGitOrgRepo = isGitOrg, Repos = repos, StoreCredentials = storeCredentials, Username = username, PasswordHash = requireAuth && storeCredentials ? EncryptionUtils.Encrypt(localPat, localPassword) : null, WorkDir = workDir, GitOrgUrl = githubHostname.TrimEnd('/'), AuthRequired = requireAuth, UpdateGitReposOnConnect = refreshConfigOnConnect }; if (config.IsGitOrgRepo) { config.Repos = GetServerRepos(config, config.Username, localPat, config.Repos); } File.WriteAllText(fileName, JsonConvert.SerializeObject(config, Formatting.Indented)); goodToGo = true; } } }