public int Execute(Options options) { var workingDirectory = options.WorkingDirectory; var gitPath = Path.Combine(options.WorkingDirectory, ".git"); if (!Directory.Exists(gitPath)) { _logger.Warning($"Directory {gitPath} doesn't exist!"); return(1); } var repo = new Repository(gitPath); var currentBranch = repo.Branches.First(b => b.IsCurrentRepositoryHead); if (currentBranch.FriendlyName != "master" && currentBranch.FriendlyName != "test-react-hooks") { return(0); } var fileStatus = repo.RetrieveStatus(); _logger.Info($"Checking '${TODO}' in files..."); var filesToCheck = fileStatus .Where(f => f.State != FileStatus.Ignored) .Where(f => FileExtensions.Contains(Path.GetExtension(f.FilePath))) .Where(f => Path.GetFileNameWithoutExtension(f.FilePath) != nameof(GitPreCommitCommand)) .ToList(); var filesWithTodo = new List <string>(); foreach (var file in filesToCheck) { var path = Path.Combine(workingDirectory, file.FilePath); _logger.Info($"Checking {path}..."); var isOk = CheckIsNoToDoInFile(path); if (!isOk) { filesWithTodo.Add(path); } } if (filesWithTodo.Any()) { _logger.Error("There are files containing '${TODO}'"); filesWithTodo.ForEach(_logger.Error); _logger.Error("Commit interrupted!"); return(1); } _logger.Success("Ok!"); return(0); }
public void Upload(string host, string folderPath, bool isOverrideWhenExists) { _logger.Info($"Processing folder: {folderPath} ..."); var url = $"{host}/api/blog"; var markdownFiles = Directory.GetFiles(folderPath, "*.md") .ToList(); if (markdownFiles.Count != 1) { _logger.Warning($"Folder should contain single markdown file!"); _logger.Info($"Posting article cancelled."); return; } var imageFiles = Directory.GetFiles(folderPath, "*.png") .ToList(); var files = markdownFiles.Union(imageFiles).ToList(); using (var client = new HttpClient()) { var markdownFile = markdownFiles.Single(); var isMarkdownOk = CheckMarkdownFile(client, markdownFile, url, isOverrideWhenExists); if (!isMarkdownOk) { _logger.Info($"Posting article cancelled."); return; } using (var formData = new MultipartFormDataContent()) { foreach (var filePath in files) { var iFileName = Path.GetFileName(filePath); _logger.Info($" File: {iFileName}"); var iFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); var iStreamContent = CreateFileContent(iFileStream, iFileName); formData.Add(iStreamContent); } _logger.Info($"Posting blog to: {url} ..."); var response = client.PostAsync(url, formData).Result; if (response.IsSuccessStatusCode) { _logger.Success($"Posted!"); _logger.Info($"Status code: {response.StatusCode}"); _logger.Info(response.ReasonPhrase); } else { _logger.Error($"Status code: {response.StatusCode}"); _logger.Error(response.ReasonPhrase); } } } }