| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
|
| 6 |
"github.com/spf13/cobra" |
| 7 |
|
| 8 |
"github.com/miniscruff/changie/core" |
| 9 |
) |
| 10 |
|
| 11 |
type Next struct { |
| 12 |
*cobra.Command |
| 13 |
|
| 14 |
// cli args |
| 15 |
IncludeDirs []string |
| 16 |
Prerelease []string |
| 17 |
Meta []string |
| 18 |
Project string |
| 19 |
|
| 20 |
TemplateCache *core.TemplateCache |
| 21 |
} |
| 22 |
|
| 23 |
func NewNext(cache *core.TemplateCache) *Next { |
| 24 |
next := &Next{ |
| 25 |
TemplateCache: cache, |
| 26 |
} |
| 27 |
|
| 28 |
cmd := &cobra.Command{ |
| 29 |
Use: "next major|minor|patch|auto", |
| 30 |
Short: "Next echos the next version based on semantic versioning", |
| 31 |
Long: `Next increments version based on semantic versioning. |
| 32 |
Check latest version and increment part (major, minor, patch). |
| 33 |
If auto is used, it will try and find the next version based on what kinds of changes are |
| 34 |
currently unreleased. |
| 35 |
Echo the next release version number to be used by CI tools or other commands like batch.`, |
| 36 |
ValidArgs: []string{"major", "minor", "patch", "auto"}, |
| 37 |
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), |
| 38 |
RunE: next.Run, |
| 39 |
} |
| 40 |
|
| 41 |
cmd.Flags().StringSliceVarP( |
| 42 |
&next.IncludeDirs, |
| 43 |
"include", "i", |
| 44 |
nil, |
| 45 |
"Include extra directories to search for change files, relative to change directory", |
| 46 |
) |
| 47 |
cmd.Flags().StringSliceVarP( |
| 48 |
&next.Prerelease, |
| 49 |
"prerelease", "p", |
| 50 |
nil, |
| 51 |
"Prerelease values to append to version", |
| 52 |
) |
| 53 |
cmd.Flags().StringSliceVarP( |
| 54 |
&next.Meta, |
| 55 |
"metadata", "m", |
| 56 |
nil, |
| 57 |
"Metadata values to append to version", |
| 58 |
) |
| 59 |
cmd.Flags().StringVarP( |
| 60 |
&next.Project, |
| 61 |
"project", "j", |
| 62 |
"", |
| 63 |
"Specify which project we are interested in", |
| 64 |
) |
| 65 |
|
| 66 |
next.Command = cmd |
| 67 |
|
| 68 |
return next |
| 69 |
} |
| 70 |
|
| 71 |
func (n *Next) Run(cmd *cobra.Command, args []string) error { |
| 72 |
writer := cmd.OutOrStdout() |
| 73 |
part := strings.ToLower(args[0]) |
| 74 |
projPrefix := "" |
| 75 |
|
| 76 |
config, err := core.LoadConfig() |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
if len(config.Projects) > 0 { |
| 82 |
var pc *core.ProjectConfig |
| 83 |
|
| 84 |
pc, err = config.Project(n.Project) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
|
| 89 |
n.Project = pc.Key |
| 90 |
projPrefix = pc.Key + config.ProjectsVersionSeparator |
| 91 |
} |
| 92 |
|
| 93 |
var changes []core.Change |
| 94 |
// only worry about loading changes, if we are in auto mode |
| 95 |
if part == core.AutoLevel { |
| 96 |
changes, err = core.GetChanges(config, n.IncludeDirs, n.Project) |
| 97 |
if err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
next, err := core.GetNextVersion(config, n.TemplateCache, part, n.Prerelease, n.Meta, changes, n.Project) |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
|
| 107 |
_, err = writer.Write([]byte(projPrefix + next.Original())) |
| 108 |
|
| 109 |
return err |
| 110 |
} |