From 895f66819a7f362f446cf839343c6b22bd54b579 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 14 Jan 2021 21:48:46 +0100 Subject: [PATCH] feat: improve errors handling. --- core/core.go | 42 +++++++++++++++++++++--------------------- file/download.go | 2 +- gh/gh.go | 2 +- readme.md | 2 +- structor.go | 2 +- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/core/core.go b/core/core.go index 86483b8..2ea8e47 100644 --- a/core/core.go +++ b/core/core.go @@ -30,7 +30,7 @@ const ( func Execute(config *types.Configuration) error { workDir, err := ioutil.TempDir("", "structor") if err != nil { - return err + return fmt.Errorf("failed to create temp directory: %w", err) } defer func() { @@ -51,29 +51,29 @@ func process(workDir string, config *types.Configuration) error { fallbackDockerfile, err := docker.GetDockerfileFallback(config.DockerfileURL, config.DockerImageName) if err != nil { - return err + return fmt.Errorf("failed to get Dockerfile fallback: %w", err) } requirementsContent, err := requirements.GetContent(config.RequirementsURL) if err != nil { - return err + return fmt.Errorf("failed to get requirements content: %w", err) } latestTagName, err := getLatestReleaseTagName(config.Owner, config.RepositoryName) if err != nil { - return err + return fmt.Errorf("failed to get latest release: %w", err) } log.Printf("Latest tag: %s", latestTagName) branches, err := getBranches(config.ExperimentalBranchName, config.ExcludedBranches, config.Debug) if err != nil { - return err + return fmt.Errorf("failed to get branches: %w", err) } siteDir, err := createSiteDirectory() if err != nil { - return err + return fmt.Errorf("failed to create site directory: %w", err) } for _, branchRef := range branches { @@ -84,17 +84,17 @@ func process(workDir string, config *types.Configuration) error { err := repository.CreateWorkTree(versionCurrentPath, branchRef, config.Debug) if err != nil { - return err + return fmt.Errorf("failed to create worktree: %w", err) } versionDocsRoot, err := getDocumentationRoot(versionCurrentPath) if err != nil { - return err + return fmt.Errorf("failed to get documentation path: %w", err) } err = requirements.Check(versionDocsRoot) if err != nil { - return err + return fmt.Errorf("failed to check requirements: %w", err) } versionsInfo := types.VersionsInformation{ @@ -108,12 +108,12 @@ func process(workDir string, config *types.Configuration) error { err = buildDocumentation(branches, versionsInfo, fallbackDockerfile, menuContent, requirementsContent, config) if err != nil { - return err + return fmt.Errorf("failed to build documentation: %w", err) } err = copyVersionSiteToOutputSite(versionsInfo, siteDir) if err != nil { - return err + return fmt.Errorf("failed to copy site directory: %w", err) } } @@ -138,7 +138,7 @@ func getBranches(experimentalBranchName string, excludedBranches []string, debug gitBranches, err := repository.ListBranches(debug) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to list branches: %w", err) } for _, branch := range gitBranches { @@ -169,13 +169,13 @@ func containsBranch(branches []string, branch string) bool { func createSiteDirectory() (string, error) { currentDir, err := os.Getwd() if err != nil { - return "", err + return "", fmt.Errorf("failed to get current directory: %w", err) } siteDir := filepath.Join(currentDir, "site") err = createDirectory(siteDir) if err != nil { - return "", err + return "", fmt.Errorf("failed to create site directory: %w", err) } return siteDir, nil @@ -223,22 +223,22 @@ func buildDocumentation(branches []string, versionsInfo types.VersionsInformatio err = menu.Build(versionsInfo, branches, menuTemplateContent) if err != nil { - return err + return fmt.Errorf("failed to build the menu: %w", err) } err = requirements.Build(versionsInfo, requirementsContent) if err != nil { - return err + return fmt.Errorf("failed to build the requirements: %w", err) } baseDockerfile, err := docker.GetDockerfile(versionsInfo.CurrentPath, fallbackDockerfile, config.DockerfileName) if err != nil { - return err + return fmt.Errorf("failed to get Dockerfile: %w", err) } dockerImageFullName, err := baseDockerfile.BuildImage(versionsInfo, config.NoCache, config.Debug) if err != nil { - return err + return fmt.Errorf("failed to build Docker image: %w", err) } args := []string{"run", "--rm", "-v", versionsInfo.CurrentPath + ":/mkdocs"} @@ -251,7 +251,7 @@ func buildDocumentation(branches []string, versionsInfo types.VersionsInformatio output, err := docker.Exec(config.Debug, args...) if err != nil { log.Println(output) - return err + return fmt.Errorf("failed to run Docker image: %w", err) } return nil @@ -299,7 +299,7 @@ func copyVersionSiteToOutputSite(versionsInfo types.VersionsInformation, siteDir // Create a permalink for the latest version err := file.Copy(filepath.Join(currentSiteDir, "site"), outputDir) if err != nil { - return err + return fmt.Errorf("failed to create permalink for the latest version: %w", err) } outputDir = siteDir @@ -312,7 +312,7 @@ func cleanAll(workDir string, debug bool) error { output, err := git.Worktree(worktree.Prune, git.Debugger(debug)) if err != nil { log.Println(output) - return err + return fmt.Errorf("failed to prune worktree: %w", err) } return os.RemoveAll(workDir) diff --git a/file/download.go b/file/download.go index 747733e..4bdfcec 100644 --- a/file/download.go +++ b/file/download.go @@ -10,7 +10,7 @@ import ( func Download(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to download: %w", err) } defer func() { _ = resp.Body.Close() }() diff --git a/gh/gh.go b/gh/gh.go index cd36552..5875e26 100644 --- a/gh/gh.go +++ b/gh/gh.go @@ -31,7 +31,7 @@ func GetLatestReleaseTagName(owner, repositoryName string) (string, error) { location, err := resp.Location() if err != nil { - return "", err + return "", fmt.Errorf("failed to get location header: %w", err) } return strings.TrimPrefix(location.String(), baseURL+"/tag/"), nil diff --git a/readme.md b/readme.md index b36cf08..c7287df 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ # Messor Structor: Manage multiple documentation versions with Mkdocs. [![GitHub release](https://img.shields.io/github/release/traefik/structor.svg)](https://github.com/traefik/structor/releases/latest) -[![Build Status](https://travis-ci.com/traefik/structor.svg?branch=master)](https://travis-ci.com/traefik/structor) +[![Build Status](https://github.com/traefik/structor/workflows/Main/badge.svg?branch=master)](https://github.com/traefik/structor/actions) Structor use git branches to create the versions of a documentation, only works with Mkdocs. diff --git a/structor.go b/structor.go index f15cbd9..3df3d31 100644 --- a/structor.go +++ b/structor.go @@ -90,7 +90,7 @@ func main() { rootCmd.AddCommand(versionCmd) if err := rootCmd.Execute(); err != nil { - fmt.Println(err) + fmt.Printf("failed to execute: %v\n", err) os.Exit(1) } }