Skip to content

Commit

Permalink
feat: utility to bump a go module version (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchon authored Sep 19, 2024
1 parent a9e6144 commit 700ddd9
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
106 changes: 106 additions & 0 deletions cmd/repo/bump_module_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package repo

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/spf13/cobra"
)

// BumpModuleVersionCmd represents the query command
var BumpModuleVersionCmd = &cobra.Command{
Use: "bump-module-version [path] [version]",
Args: cobra.ExactArgs(2),
Short: "Bump the version of a go module, i.e., hanchond repo bump-version /tmp/repo v21",
Run: func(_ *cobra.Command, args []string) {
path := args[0]
// Make sure that the path does not end with `/`
path = strings.TrimSuffix(path, "/")

version := args[1]
// If the version arg is missing the `v` prefix, we add it
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
}

// Find the current version
goModPath := fmt.Sprintf("%s/go.mod", path)
fmt.Println("using go.mod path as:", goModPath)
goModFile, err := filesmanager.ReadFile(goModPath)
if err != nil {
fmt.Println("error reading the go.mod file:", err.Error())
os.Exit(1)
}

// Get the current version
re := regexp.MustCompile(`(?m)^module\s+(\S+)$`)
modules := re.FindAllStringSubmatch(string(goModFile), -1)
if len(modules) == 0 {
fmt.Println("the go.mod file does not define the module name")
os.Exit(1)
}
currentVersion := modules[0][1]
fmt.Println("the current version is:", currentVersion)

// Create the new version with all the parts of the currentVersion but overwritting the last segment
newVersion := ""
parts := strings.Split(currentVersion, "/")
for k, v := range parts {
if k == len(parts)-1 {
newVersion += version
break
}
newVersion += v + "/"
}

fmt.Println("the new version is:", newVersion)

// Walk through the root directory recursively
if err = filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Println("error reading the directory", path)
os.Exit(1)
}

// Only process regular files
if info.IsDir() {
return nil
}

// Read the file
content, err := filesmanager.ReadFile(path)
if err != nil {
fmt.Println("failed reading the file:", path)
os.Exit(1)
}

fileContent := string(content)

// Replace all occurrences
re := regexp.MustCompile(regexp.QuoteMeta(currentVersion))
updatedContent := re.ReplaceAllString(fileContent, newVersion)

// Only write if the file was modified
if updatedContent != fileContent {
fmt.Printf("updating file: %s\n", path)
err := filesmanager.SaveFileWithMode([]byte(updatedContent), path, info.Mode())
if err != nil {
fmt.Println("failed saving the file:", path)
os.Exit(1)
}
}

return nil
}); err != nil {
fmt.Println("error walking the directory:", err.Error())
os.Exit(1)
}

os.Exit(0)
},
}
22 changes: 22 additions & 0 deletions cmd/repo/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package repo

import (
"os"

"github.com/spf13/cobra"
)

// RepoCmd represents the repo command
var RepoCmd = &cobra.Command{
Use: "repo",
Aliases: []string{"r"},
Short: "Repo management utils",
Run: func(cmd *cobra.Command, _ []string) {
_ = cmd.Help()
os.Exit(0)
},
}

func init() {
RepoCmd.AddCommand(BumpModuleVersionCmd)
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hanchon/hanchond/cmd/convert"
playground "github.com/hanchon/hanchond/cmd/playground"
"github.com/hanchon/hanchond/cmd/repo"

"github.com/spf13/cobra"
)
Expand All @@ -27,4 +28,5 @@ func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.AddCommand(convert.ConvertCmd)
rootCmd.AddCommand(playground.PlaygroundCmd)
rootCmd.AddCommand(repo.RepoCmd)
}
28 changes: 28 additions & 0 deletions docs/pages/hanchond/repo/bump-module-version.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Go Module Bump Version

Bump a go project module version.

This command replaces the current version with the one given by the user and then updates all the internal files using the dependency automatically.

## Usage

```sh
hanchond repo bump-module-version [path] [version]
```

## Example

```sh
hanchond repo bump-module-version /Users/hanchon/devel/evmos/evmos v21

using go.mod path as: /Users/hanchon/devel/evmos/evmos/go.mod
the current version is: github.com/evmos/evmos/v20
the new version is: github.com/evmos/evmos/v21
updating file: /Users/hanchon/devel/evmos/evmos/.github/workflows/consensuswarn.yml
updating file: /Users/hanchon/devel/evmos/evmos/api/ethermint/evm/v1/access_list_tx.go
updating file: /Users/hanchon/devel/evmos/evmos/api/ethermint/evm/v1/dynamic_fee_tx.go
updating file: /Users/hanchon/devel/evmos/evmos/api/ethermint/evm/v1/legacy_tx.go
updating file: /Users/hanchon/devel/evmos/evmos/app/ante/cosmos/authz_test.go
updating file: /Users/hanchon/devel/evmos/evmos/app/ante/cosmos/eip712.go
...
```
7 changes: 6 additions & 1 deletion playground/filesmanager/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filesmanager
import (
"errors"
"io"
"io/fs"
"os"
)

Expand All @@ -16,7 +17,11 @@ func ReadFile(path string) ([]byte, error) {
}

func SaveFile(data []byte, path string) error {
return os.WriteFile(path, data, 0o600)
return SaveFileWithMode(data, path, 0o600)
}

func SaveFileWithMode(data []byte, path string, mode fs.FileMode) error {
return os.WriteFile(path, data, mode)
}

func DoesFileExist(path string) bool {
Expand Down
10 changes: 10 additions & 0 deletions vocs.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ export default defineConfig({
},
],
},
{
text: "Repo",
collapsed: false,
items: [
{
text: "Bump Go Module Version",
link: "/hanchond/repo/bump-module-version",
},
],
},
],
},
{
Expand Down

0 comments on commit 700ddd9

Please sign in to comment.