Skip to content

Commit

Permalink
feat: improve errors handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 14, 2021
1 parent 77433be commit 895f668
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
42 changes: 21 additions & 21 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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{
Expand All @@ -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)
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion file/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }()
Expand Down
2 changes: 1 addition & 1 deletion gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion structor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 895f668

Please sign in to comment.