|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/arzkar/git-utils/utils" |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +var bumpCmd = &cobra.Command{ |
| 11 | + Use: "bump", |
| 12 | + Short: "Version bump the version", |
| 13 | + Run: bumpVersion, |
| 14 | +} |
| 15 | + |
| 16 | +var majorCmd = &cobra.Command{ |
| 17 | + Use: "major", |
| 18 | + Short: "Version bump the major version", |
| 19 | + Run: bumpMajor, |
| 20 | +} |
| 21 | + |
| 22 | +var minorCmd = &cobra.Command{ |
| 23 | + Use: "minor", |
| 24 | + Short: "Version bump the minor version", |
| 25 | + Run: bumpMinor, |
| 26 | +} |
| 27 | + |
| 28 | +var patchCmd = &cobra.Command{ |
| 29 | + Use: "patch", |
| 30 | + Short: "Version bump the patch version", |
| 31 | + Run: bumpPatch, |
| 32 | +} |
| 33 | + |
| 34 | +func init() { |
| 35 | + rootCmd.AddCommand(bumpCmd) |
| 36 | + bumpCmd.AddCommand(majorCmd) |
| 37 | + bumpCmd.AddCommand(minorCmd) |
| 38 | + bumpCmd.AddCommand(patchCmd) |
| 39 | +} |
| 40 | + |
| 41 | +func bumpVersion(cmd *cobra.Command, args []string) { |
| 42 | + currentVersion, err := utils.GetCurrentVersion() |
| 43 | + if err != nil { |
| 44 | + fmt.Println("Failed to read current version:", err) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + newVersion := utils.IncrementVersion(currentVersion) |
| 49 | + |
| 50 | + err = utils.UpdateFiles(currentVersion, newVersion) |
| 51 | + if err != nil { |
| 52 | + fmt.Println("Failed to update files:", err) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + commitEnabled, err := utils.GetCommitOption() |
| 57 | + if err != nil { |
| 58 | + fmt.Println("Failed to read commit option:", err) |
| 59 | + return |
| 60 | + } |
| 61 | + commitMessage := fmt.Sprintf("Bump version: %s → %s", currentVersion, newVersion) |
| 62 | + if commitEnabled { |
| 63 | + err = utils.CommitChanges(currentVersion, newVersion, commitMessage) |
| 64 | + if err != nil { |
| 65 | + fmt.Println("Failed to commit changes:", err) |
| 66 | + return |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + tagEnabled, err := utils.GetTagOption() |
| 71 | + if err != nil { |
| 72 | + fmt.Println("Failed to read tag option:", err) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if tagEnabled { |
| 77 | + err = utils.CreateTag(newVersion, commitMessage) |
| 78 | + if err != nil { |
| 79 | + fmt.Println("Failed to create tag:", err) |
| 80 | + return |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fmt.Printf("Bump version: %s → %s\n", currentVersion, newVersion) |
| 85 | +} |
| 86 | + |
| 87 | +func bumpMajor(cmd *cobra.Command, args []string) { |
| 88 | + currentVersion, err := utils.GetCurrentVersion() |
| 89 | + if err != nil { |
| 90 | + fmt.Println("Failed to read current version:", err) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + newVersion := utils.BumpMajorVersion(currentVersion) |
| 95 | + |
| 96 | + err = utils.UpdateFiles(currentVersion, newVersion) |
| 97 | + if err != nil { |
| 98 | + fmt.Println("Failed to update files:", err) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + commitEnabled, err := utils.GetCommitOption() |
| 103 | + if err != nil { |
| 104 | + fmt.Println("Failed to read commit option:", err) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + commitMessage := fmt.Sprintf("Bump version: %s → %s", currentVersion, newVersion) |
| 109 | + if commitEnabled { |
| 110 | + err = utils.CommitChanges(currentVersion, newVersion, commitMessage) |
| 111 | + if err != nil { |
| 112 | + fmt.Println("Failed to commit changes:", err) |
| 113 | + return |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + tagEnabled, err := utils.GetTagOption() |
| 118 | + if err != nil { |
| 119 | + fmt.Println("Failed to read tag option:", err) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + if tagEnabled { |
| 124 | + err = utils.CreateTag(newVersion, commitMessage) |
| 125 | + if err != nil { |
| 126 | + fmt.Println("Failed to create tag:", err) |
| 127 | + return |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fmt.Printf("Bump version: %s → %s\n", currentVersion, newVersion) |
| 132 | +} |
| 133 | + |
| 134 | +func bumpMinor(cmd *cobra.Command, args []string) { |
| 135 | + currentVersion, err := utils.GetCurrentVersion() |
| 136 | + if err != nil { |
| 137 | + fmt.Println("Failed to read current version:", err) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + newVersion := utils.BumpMinorVersion(currentVersion) |
| 142 | + |
| 143 | + err = utils.UpdateFiles(currentVersion, newVersion) |
| 144 | + if err != nil { |
| 145 | + fmt.Println("Failed to update files:", err) |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + commitEnabled, err := utils.GetCommitOption() |
| 150 | + if err != nil { |
| 151 | + fmt.Println("Failed to read commit option:", err) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + commitMessage := fmt.Sprintf("Bump version: %s → %s", currentVersion, newVersion) |
| 156 | + if commitEnabled { |
| 157 | + err = utils.CommitChanges(currentVersion, newVersion, commitMessage) |
| 158 | + if err != nil { |
| 159 | + fmt.Println("Failed to commit changes:", err) |
| 160 | + return |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + tagEnabled, err := utils.GetTagOption() |
| 165 | + if err != nil { |
| 166 | + fmt.Println("Failed to read tag option:", err) |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + if tagEnabled { |
| 171 | + err = utils.CreateTag(newVersion, commitMessage) |
| 172 | + if err != nil { |
| 173 | + fmt.Println("Failed to create tag:", err) |
| 174 | + return |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + fmt.Printf("Bump version: %s → %s\n", currentVersion, newVersion) |
| 179 | +} |
| 180 | + |
| 181 | +func bumpPatch(cmd *cobra.Command, args []string) { |
| 182 | + currentVersion, err := utils.GetCurrentVersion() |
| 183 | + if err != nil { |
| 184 | + fmt.Println("Failed to read current version:", err) |
| 185 | + return |
| 186 | + } |
| 187 | + |
| 188 | + newVersion := utils.BumpPatchVersion(currentVersion) |
| 189 | + |
| 190 | + err = utils.UpdateFiles(currentVersion, newVersion) |
| 191 | + if err != nil { |
| 192 | + fmt.Println("Failed to update files:", err) |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + commitEnabled, err := utils.GetCommitOption() |
| 197 | + if err != nil { |
| 198 | + fmt.Println("Failed to read commit option:", err) |
| 199 | + return |
| 200 | + } |
| 201 | + |
| 202 | + commitMessage := fmt.Sprintf("Bump version: %s → %s", currentVersion, newVersion) |
| 203 | + if commitEnabled { |
| 204 | + err = utils.CommitChanges(currentVersion, newVersion, commitMessage) |
| 205 | + if err != nil { |
| 206 | + fmt.Println("Failed to commit changes:", err) |
| 207 | + return |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + tagEnabled, err := utils.GetTagOption() |
| 212 | + if err != nil { |
| 213 | + fmt.Println("Failed to read tag option:", err) |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + if tagEnabled { |
| 218 | + err = utils.CreateTag(newVersion, commitMessage) |
| 219 | + if err != nil { |
| 220 | + fmt.Println("Failed to create tag:", err) |
| 221 | + return |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + fmt.Printf("Bump version: %s → %s\n", currentVersion, newVersion) |
| 226 | +} |
0 commit comments