From 700ddd9757ea2c8b87695b3074b99c6608e57db8 Mon Sep 17 00:00:00 2001 From: Guillermo Paoletti Date: Thu, 19 Sep 2024 21:43:07 +0200 Subject: [PATCH] feat: utility to bump a go module version (#73) --- cmd/repo/bump_module_version.go | 106 ++++++++++++++++++ cmd/repo/repo.go | 22 ++++ cmd/root.go | 2 + .../hanchond/repo/bump-module-version.mdx | 28 +++++ playground/filesmanager/files.go | 7 +- vocs.config.ts | 10 ++ 6 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 cmd/repo/bump_module_version.go create mode 100644 cmd/repo/repo.go create mode 100644 docs/pages/hanchond/repo/bump-module-version.mdx diff --git a/cmd/repo/bump_module_version.go b/cmd/repo/bump_module_version.go new file mode 100644 index 0000000..1969a51 --- /dev/null +++ b/cmd/repo/bump_module_version.go @@ -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) + }, +} diff --git a/cmd/repo/repo.go b/cmd/repo/repo.go new file mode 100644 index 0000000..6380c33 --- /dev/null +++ b/cmd/repo/repo.go @@ -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) +} diff --git a/cmd/root.go b/cmd/root.go index 803bafb..38e230e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" ) @@ -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) } diff --git a/docs/pages/hanchond/repo/bump-module-version.mdx b/docs/pages/hanchond/repo/bump-module-version.mdx new file mode 100644 index 0000000..b1d9d3c --- /dev/null +++ b/docs/pages/hanchond/repo/bump-module-version.mdx @@ -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 +... +``` diff --git a/playground/filesmanager/files.go b/playground/filesmanager/files.go index ba24668..1db112a 100644 --- a/playground/filesmanager/files.go +++ b/playground/filesmanager/files.go @@ -3,6 +3,7 @@ package filesmanager import ( "errors" "io" + "io/fs" "os" ) @@ -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 { diff --git a/vocs.config.ts b/vocs.config.ts index e42c43c..66235a2 100644 --- a/vocs.config.ts +++ b/vocs.config.ts @@ -308,6 +308,16 @@ export default defineConfig({ }, ], }, + { + text: "Repo", + collapsed: false, + items: [ + { + text: "Bump Go Module Version", + link: "/hanchond/repo/bump-module-version", + }, + ], + }, ], }, {