Skip to content

Commit

Permalink
chore: better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Sep 26, 2024
1 parent daf8e57 commit 21ada6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
16 changes: 8 additions & 8 deletions cmd/devtools/internal/release/pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ func preRun(ctx context.Context, gitClient *git.GitClient, branch string, dryRun

err = bumpVersion(ctx, dryRun, vVersion)
if err != nil {
return fmt.Errorf("error bumping version: %w", err)
return fmt.Errorf("bump version: %w", err)
}

// loop through the examples and modules directories to update the go.mod files
for _, directory := range directories {
path := filepath.Join(ctx.RootDir, directory)
modules, err := getSubdirectories(path)
if err != nil {
return fmt.Errorf("error getting subdirectories: %w", err)
return fmt.Errorf("get subdirectories: %w", err)
}

// loop through the Go modules in the directory
for _, module := range modules {
moduleModFile := filepath.Join(path, module, "go.mod")
err := replaceInFile(dryRun, true, moduleModFile, "testcontainers-go v.*", fmt.Sprintf("testcontainers-go v%s", version))
if err != nil {
return fmt.Errorf("error replacing in file: %w", err)
return fmt.Errorf("replace in file: %w", err)
}
}

Expand All @@ -80,7 +80,7 @@ func preRun(ctx context.Context, gitClient *git.GitClient, branch string, dryRun
func extractCurrentVersion(ctx context.Context) (string, error) {
data, err := os.ReadFile(ctx.VersionFile())
if err != nil {
return "", fmt.Errorf("error reading version file: %w", err)
return "", fmt.Errorf("read version file: %w", err)
}

for _, line := range strings.Split(string(data), "\n") {
Expand Down Expand Up @@ -112,7 +112,7 @@ func replaceInFile(dryRun bool, regex bool, filePath string, old string, new str

read, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
return fmt.Errorf("read file: %w", err)
}

var newContents string
Expand All @@ -125,7 +125,7 @@ func replaceInFile(dryRun bool, regex bool, filePath string, old string, new str

err = os.WriteFile(filePath, []byte(newContents), 0)
if err != nil {
return fmt.Errorf("error writing to file: %w", err)
return fmt.Errorf("write to file: %w", err)
}

return nil
Expand Down Expand Up @@ -161,7 +161,7 @@ func processMarkdownFiles(dryRun bool, docsDir, version string) error {
releasedText := sinceVersionText(version)
err := replaceInFile(dryRun, false, path, nonReleasedText, releasedText)
if err != nil {
return fmt.Errorf("error replacing in file: %w", err)
return fmt.Errorf("replace in file: %w", err)
}

return nil
Expand All @@ -181,7 +181,7 @@ func runCommand(ctx context.Context, dryRun bool, command string, arg ...string)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running command: %w", err)
return fmt.Errorf("run command: %w", err)
}

return nil
Expand Down
32 changes: 16 additions & 16 deletions cmd/devtools/internal/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func run(ctx devcontext.Context, gitClient *git.GitClient, bumpType string, dryR

version, err := extractCurrentVersion(ctx)
if err != nil {
return fmt.Errorf("failed to extract the current version: %w", err)
return fmt.Errorf("extract current version: %w", err)
}

fmt.Println("Current version:", version)
Expand All @@ -47,7 +47,7 @@ func run(ctx devcontext.Context, gitClient *git.GitClient, bumpType string, dryR

nextVersion, err := getSemverVersion(bumpType, releaseVersion)
if err != nil {
return fmt.Errorf("failed to bump the version. Please check the semver-tool image and the bump type: %w", err)
return fmt.Errorf("get semver version. Please check the semver-tool image and the bump type: %w", err)
}

fmt.Println("Next version:", nextVersion)
Expand All @@ -64,78 +64,78 @@ func run(ctx devcontext.Context, gitClient *git.GitClient, bumpType string, dryR
for _, arg := range args {
if err := gitClient.Add(arg...); err != nil {
// log the error and continue to add the other files
fmt.Printf("error adding files: %s\n", err)
fmt.Printf("git-add files: %s\n", err)
continue
}
}

// Commit the project in the current state
if err := gitClient.Commit(fmt.Sprintf("chore: use new version (%s) in modules and examples", releaseVersion)); err != nil {
return fmt.Errorf("error committing the release: %w", err)
return fmt.Errorf("git-commit release: %w", err)
}

if err := gitClient.Tag(releaseVersion); err != nil {
return fmt.Errorf("error tagging the release: %w", err)
return fmt.Errorf("git-tag release: %w", err)
}

// loop through the examples and modules directories to create the git tags
for _, directory := range directories {
path := filepath.Join(ctx.RootDir, directory)
modules, err := getSubdirectories(path)
if err != nil {
return fmt.Errorf("error getting subdirectories: %w", err)
return fmt.Errorf("get subdirectories: %w", err)
}

// loop through the Go modules in the directory
for _, module := range modules {
moduleTag := fmt.Sprintf("%s/%s/%s", directory, module, releaseVersion) // e.g. modules/mongodb/v0.0.1
if err := gitClient.Tag(moduleTag); err != nil {
return fmt.Errorf("error tagging the module: %w", err)
return fmt.Errorf("git-tag module: %w", err)
}
}
}

fmt.Printf("Producing a %s bump of the version, from %s to %s\n", bumpType, version, nextVersion)

if err := replaceInFile(dryRun, false, ctx.VersionFile(), version, nextVersion); err != nil {
return fmt.Errorf("error replacing in version file: %w", err)
return fmt.Errorf("replace version in file: %w", err)
}

if err := gitClient.Add(ctx.VersionFile()); err != nil {
return fmt.Errorf("error adding version file: %w", err)
return fmt.Errorf("git-add version file: %w", err)
}

if err := gitClient.Commit(fmt.Sprintf("chore: prepare for next %s development version cycle (%s)", bumpType, nextVersion)); err != nil {
return fmt.Errorf("error committing the next version: %w", err)
return fmt.Errorf("git-commit next version: %w", err)
}

// for testing purposes, we can skip the remote operations, like pushing the tags
if !skipRemoteOps {
if err := gitClient.PushTags(); err != nil {
return fmt.Errorf("error pushing tags: %w", err)
return fmt.Errorf("git-push tags: %w", err)
}
}

// hitting the golang proxy to update the latest version for the core and the modules
// can be easily mocked in the tests

if err := hitGolangProxy(proxyURL, dryRun, repository, releaseVersion); err != nil {
return fmt.Errorf("error hitting the golang proxy for the core: %w", err)
return fmt.Errorf("hit golang proxy for core: %w", err)
}

// loop through the modules to hit the golang proxy
for _, directory := range directories {
path := filepath.Join(ctx.RootDir, directory)
modules, err := getSubdirectories(path)
if err != nil {
return fmt.Errorf("error getting subdirectories: %w", err)
return fmt.Errorf("get subdirectories: %w", err)
}

// loop through the Go modules in the directory
for _, module := range modules {
modulePath := fmt.Sprintf("%s/%s/%s", repository, directory, module)
if err := hitGolangProxy(proxyURL, dryRun, modulePath, releaseVersion); err != nil {
return fmt.Errorf("error hitting the golang proxy for the module: %w", err)
return fmt.Errorf("hit golang proxy for module: %w", err)
}
}
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func hitGolangProxy(proxyURL string, dryRun bool, modulePath string, moduleVersi
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error hitting the golang proxy: %s", resp.Status)
return fmt.Errorf("hit golang proxy: %s", resp.Status)
}

return nil
Expand All @@ -192,7 +192,7 @@ func getSemverVersion(bumpType string, vVersion string) (string, error) {
defer func() {
err := c.Terminate(context.Background())
if err != nil {
fmt.Println("Error terminating container", err)
fmt.Println("terminate container", err)
}
}()

Expand Down

0 comments on commit 21ada6f

Please sign in to comment.