Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Errors and Update Readme #16

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ GoBump is a simple command-line tool written in Go that allows you to update the
## Usage

```shell
gobump --packages=<package@version>,... --modroot=<path to go.mod>
gobump --packages=<package@version> ... --modroot=<path to go.mod>
```

### Flags

* `--packages`: A comma-separated list of packages to update. Each package should be in the format `package@version`.
* `--packages`: A space-separated list of packages to update. Each package should be in the format `package@version`.
* `--modroot`: Path to the go.mod root. If not specified, it defaults to the current directory.
* `--replaces`: A comma-separated list of packages to replace. Each entry should be in the format `old=new@version`.
* `--replaces`: A space-separated list of packages to replace. Each entry should be in the format `old=new@version`.
* `--go-version`: set the go-version for 'go mod tidy' command.
* `--show-diff`: Show the difference between the original and 'go.mod' files.
* `--tidy`: Run 'go mod tidy' command.

## Example

```shell
gobump --packages=github.com/pkg/[email protected],golang.org/x/[email protected] --modroot=/path/to/your/project
gobump --packages="github.com/pkg/[email protected] golang.org/x/[email protected]" --modroot=/path/to/your/project
```

This will update the versions of `github.com/pkg/errors` and `golang.org/x/mod` in your `go.mod` file.
Expand Down
20 changes: 7 additions & 13 deletions cmd/gobump/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cmd

import (
"fmt"
"log"
"os"
"strings"

"github.com/chainguard-dev/gobump/pkg/types"
Expand All @@ -30,18 +28,16 @@ var rootCmd = &cobra.Command{
Args: cobra.NoArgs,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if rootFlags.packages == "" {
log.Println("Usage: gobump -packages=<package@version>,...")
os.Exit(1)
return fmt.Errorf("Error: No packages provided. Usage: gobump --packages=\"<package1@version> <package2@version> ...\"")
}
packages := strings.Split(rootFlags.packages, " ")
pkgVersions := map[string]*types.Package{}
for _, pkg := range packages {
parts := strings.Split(pkg, "@")
if len(parts) != 2 {
fmt.Println("Usage: gobump -packages=<package@version>,...")
os.Exit(1)
return fmt.Errorf("Error: Invalid package format. Each package should be in the format <package@version>. Usage: gobump --packages=\"<package1@version> <package2@version> ...\"")
}
pkgVersions[parts[0]] = &types.Package{
Name: parts[0],
Expand All @@ -55,14 +51,12 @@ var rootCmd = &cobra.Command{
for _, replace := range replaces {
parts := strings.Split(replace, "=")
if len(parts) != 2 {
fmt.Println("Usage: gobump -replaces=<oldpackage=newpackage@version>,...")
os.Exit(1)
return fmt.Errorf("Error: Invalid replace format. Each replace should be in the format <oldpackage=newpackage@version>. Usage: gobump -replaces=\"<oldpackage=newpackage@version> ...\"")
}
// extract the new package name and version
rep := strings.Split(strings.TrimPrefix(replace, fmt.Sprintf("%s=", parts[0])), "@")
if len(rep) != 2 {
fmt.Println("Usage: gobump -replaces=<oldpackage=newpackage@version>,...")
os.Exit(1)
return fmt.Errorf("Error: Invalid replace format. Each replace should be in the format <oldpackage=newpackage@version>. Usage: gobump -replaces=\"<oldpackage=newpackage@version> ...\"")
}
// Merge/Add the packages to replace reusing the initial list of packages
pkgVersions[rep[0]] = &types.Package{
Expand All @@ -75,9 +69,9 @@ var rootCmd = &cobra.Command{
}

if _, err := update.DoUpdate(pkgVersions, &types.Config{Modroot: rootFlags.modroot, Tidy: rootFlags.tidy, GoVersion: rootFlags.goVersion, ShowDiff: rootFlags.showDiff}); err != nil {
fmt.Println("failed running update: ", err)
os.Exit(1)
return fmt.Errorf("Failed to running update. Error: %v", err)
}
return nil
},
}

Expand Down
Loading