diff --git a/Contribution.md b/Contribution.md new file mode 100644 index 0000000..e4c200d --- /dev/null +++ b/Contribution.md @@ -0,0 +1,10 @@ +If you want to contribute to this project, start with an existing issue or propose a new one. + +# Development + +## Build + +```bash +VERSION=$(semver info v) +go build -ldflags "-X main.version=$VERSION" . +``` \ No newline at end of file diff --git a/semver.go b/semver.go index 20a4d33..2d0e7f7 100644 --- a/semver.go +++ b/semver.go @@ -1,7 +1,6 @@ package main import ( - "errors" "fmt" "log" "os" @@ -11,23 +10,28 @@ import ( "github.com/urfave/cli/v2" ) -var version string +var ( + version string + appName = "SemVer" + repository = "https://github.com/dbgjerez/semantic-versioning-cli" +) func main() { var file string - var store domain.ConfigStore cli.VersionPrinter = func(cCtx *cli.Context) { - fmt.Printf("%s\n", cCtx.App.Version) + fmt.Printf("%s - %s\nVersion: %s\nRepository: %s\n", appName, cCtx.App.Usage, cCtx.App.Version, repository) } cli.VersionFlag = &cli.BoolFlag{ Name: "version", Aliases: []string{"v"}, - Usage: "print only the version", + Usage: "Print the application name, repository, and version", } app := &cli.App{ + Name: appName, + Usage: "A tool for managing semantic versioning", Version: version, Flags: []cli.Flag{ &cli.StringFlag{ @@ -35,226 +39,244 @@ func main() { Aliases: []string{"f"}, Usage: "Config file", Value: ".semver.yaml", - Required: false, Destination: &file, }, }, Commands: []*cli.Command{ + newInfoCommand(&file), + newMajorCommand(&file), + newFeatureCommand(&file), + newPatchCommand(&file), + newSnapshotCommand(&file), + newInitCommand(&file), + }, + } + + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} + +func newInfoCommand(file *string) *cli.Command { + return &cli.Command{ + Name: "info", + Aliases: []string{"i"}, + Usage: "Show the artifact info", + Action: func(*cli.Context) error { + store := domain.NewConfigStore(*file) + c, err := store.ReadConfig() + if err != nil { + return err + } + action := actions.NewInfoAction(&c) + fmt.Printf(action.CompleteInfo()) + return nil + }, + Subcommands: []*cli.Command{ { - Name: "info", - Aliases: []string{"i"}, - Usage: "Show the artifact info", + Name: "version", + Aliases: []string{"v"}, + Usage: "Artifact version", Action: func(*cli.Context) error { - store := domain.NewConfigStore(file) + store := domain.NewConfigStore(*file) c, err := store.ReadConfig() if err != nil { return err } action := actions.NewInfoAction(&c) - fmt.Printf(action.CompleteInfo()) + fmt.Printf(action.ArtifactVersion()) return nil }, - Subcommands: []*cli.Command{ - { - Name: "version", - Aliases: []string{"v"}, - Usage: "Artifact version", - Action: func(*cli.Context) error { - store = domain.NewConfigStore(file) - c, err := store.ReadConfig() - if err != nil { - return err - } - action := actions.NewInfoAction(&c) - fmt.Printf(action.ArtifactVersion()) - return nil - }, - }, - }, }, - { - Name: "major", - Aliases: []string{"m"}, - Usage: "Create a new major version", - Flags: []cli.Flag{ - &cli.IntFlag{ - Name: "force", - Aliases: []string{"f"}, - Usage: "force the version", - Value: -1, - }, - }, - Action: func(ctx *cli.Context) error { - r := ctx.Int("force") - store := domain.NewConfigStore(file) - c, err := store.ReadConfig() - if err != nil { - return err - } - action := actions.NewReleaseAction(&c) - config, err2 := action.CreateMajor(r) - if err2 != nil { - return err2 - } - infoAction := actions.NewInfoAction(&config) - fmt.Printf(infoAction.ArtifactVersion()) - return store.SaveConfig(config) - }, + }, + } +} + +func newMajorCommand(file *string) *cli.Command { + return &cli.Command{ + Name: "major", + Aliases: []string{"m"}, + Usage: "Create a new major version", + Flags: []cli.Flag{ + &cli.IntFlag{ + Name: "force", + Aliases: []string{"f"}, + Usage: "Force the version", + Value: -1, }, - { - Name: "feature", + }, + Action: func(ctx *cli.Context) error { + r := ctx.Int("force") + store := domain.NewConfigStore(*file) + c, err := store.ReadConfig() + if err != nil { + return err + } + action := actions.NewReleaseAction(&c) + config, err2 := action.CreateMajor(r) + if err2 != nil { + return err2 + } + infoAction := actions.NewInfoAction(&config) + fmt.Printf(infoAction.ArtifactVersion()) + return store.SaveConfig(config) + }, + } +} + +func newFeatureCommand(file *string) *cli.Command { + return &cli.Command{ + Name: "feature", + Aliases: []string{"f"}, + Usage: "Create a new feature", + Flags: []cli.Flag{ + &cli.IntFlag{ + Name: "force", Aliases: []string{"f"}, - Usage: "Create a new feature", - Flags: []cli.Flag{ - &cli.IntFlag{ - Name: "force", - Aliases: []string{"f"}, - Usage: "force the version", - Value: -1, - }, - }, - Action: func(ctx *cli.Context) error { - r := ctx.Int("force") - store := domain.NewConfigStore(file) - c, err := store.ReadConfig() - if err != nil { - return err - } - action := actions.NewReleaseAction(&c) - config, err2 := action.CreateFeature(r) - if err2 != nil { - return err2 - } - infoAction := actions.NewInfoAction(&config) - fmt.Printf(infoAction.ArtifactVersion()) - return store.SaveConfig(config) - }, + Usage: "Force the version", + Value: -1, }, - { - Name: "patch", - Aliases: []string{"p"}, - Usage: "Create a new patch", - Flags: []cli.Flag{ - &cli.IntFlag{ - Name: "force", - Aliases: []string{"f"}, - Usage: "force the version", - Value: -1, - }, - }, - Action: func(ctx *cli.Context) error { - r := ctx.Int("force") - store := domain.NewConfigStore(file) - c, err := store.ReadConfig() - if err != nil { - return err - } - action := actions.NewReleaseAction(&c) - config, err2 := action.CreatePatch(r) - if err2 != nil { - return err2 - } - infoAction := actions.NewInfoAction(&config) - fmt.Printf(infoAction.ArtifactVersion()) - return store.SaveConfig(config) - }, + }, + Action: func(ctx *cli.Context) error { + r := ctx.Int("force") + store := domain.NewConfigStore(*file) + c, err := store.ReadConfig() + if err != nil { + return err + } + action := actions.NewReleaseAction(&c) + config, err2 := action.CreateFeature(r) + if err2 != nil { + return err2 + } + infoAction := actions.NewInfoAction(&config) + fmt.Printf(infoAction.ArtifactVersion()) + return store.SaveConfig(config) + }, + } +} + +func newPatchCommand(file *string) *cli.Command { + return &cli.Command{ + Name: "patch", + Aliases: []string{"p"}, + Usage: "Create a new patch", + Flags: []cli.Flag{ + &cli.IntFlag{ + Name: "force", + Aliases: []string{"f"}, + Usage: "Force the version", + Value: -1, }, - { - Name: "snapshot", - Aliases: []string{"sn"}, - Usage: "Modify the snapshot flag", - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "force", - Aliases: []string{"f"}, - Usage: "force the snapshot value", - }, - }, - Action: func(ctx *cli.Context) error { - force := false - if ctx.Count("force") > 0 { // if > 0, user set value - force = true - } - forcedValue := ctx.Bool("force") - store := domain.NewConfigStore(file) - config, err := store.ReadConfig() - if err != nil { - return err - } + }, + Action: func(ctx *cli.Context) error { + r := ctx.Int("force") + store := domain.NewConfigStore(*file) + c, err := store.ReadConfig() + if err != nil { + return err + } + action := actions.NewReleaseAction(&c) + config, err2 := action.CreatePatch(r) + if err2 != nil { + return err2 + } + infoAction := actions.NewInfoAction(&config) + fmt.Printf(infoAction.ArtifactVersion()) + return store.SaveConfig(config) + }, + } +} - action := actions.SnapshotAction{ - C: &config, - Force: force, - ForcedValue: forcedValue, - } +func newSnapshotCommand(file *string) *cli.Command { + return &cli.Command{ + Name: "snapshot", + Aliases: []string{"sn"}, + Usage: "Modify the snapshot flag", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "force", + Aliases: []string{"f"}, + Usage: "Force the snapshot value", + }, + }, + Action: func(ctx *cli.Context) error { + force := ctx.Bool("force") + store := domain.NewConfigStore(*file) + config, err := store.ReadConfig() + if err != nil { + return err + } - err = action.ChangeStatus() - if err != nil { - return err - } + action := actions.SnapshotAction{ + C: &config, + Force: true, + ForcedValue: force, + } - infoAction := actions.NewInfoAction(&config) - fmt.Printf(infoAction.ArtifactVersion()) - return store.SaveConfig(config) - }, - }, - { - Name: "init", - Usage: "Init the versioning configuration file", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "name", - Aliases: []string{"n"}, - Usage: "Artifact name", - Required: true, - }, - &cli.IntFlag{ - Name: "major", - Aliases: []string{"ma"}, - Usage: "Init major number", - Value: actions.INIT_MAJOR_VERSION, - }, - &cli.IntFlag{ - Name: "minor", - Aliases: []string{"mi"}, - Usage: "Init minor number", - Value: actions.INIT_MINOR_VERSION, - }, - &cli.IntFlag{ - Name: "patch", - Aliases: []string{"p"}, - Usage: "Init patch number", - Value: actions.INIT_PATCH_VERSION, - }, - &cli.BoolFlag{ - Name: "snapshot", - Aliases: []string{"s"}, - Usage: "Enable Snapshots", - }, - }, - Action: func(ctx *cli.Context) error { - store = domain.NewConfigStore(file) - if store.Exists() { - return errors.New("Project initialized yet!") - } - action := actions.InitAction{ - ArtifactName: ctx.String("name"), - Major: ctx.Int("major"), - Minor: ctx.Int("minor"), - Patch: ctx.Int("patch"), - SnapshotsEnable: ctx.Bool("snapshot"), - } - config, err := action.NewConfig() - if err != nil { - return err - } - return store.SaveConfig(config) - }, - }, + if err := action.ChangeStatus(); err != nil { + return err + } + + infoAction := actions.NewInfoAction(&config) + fmt.Printf(infoAction.ArtifactVersion()) + return store.SaveConfig(config) }, } +} - if err := app.Run(os.Args); err != nil { - log.Fatal(err) +func newInitCommand(file *string) *cli.Command { + return &cli.Command{ + Name: "init", + Usage: "Init the versioning configuration file", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + Usage: "Artifact name", + Required: true, + }, + &cli.IntFlag{ + Name: "major", + Aliases: []string{"ma"}, + Usage: "Init major number", + Value: actions.INIT_MAJOR_VERSION, + }, + &cli.IntFlag{ + Name: "minor", + Aliases: []string{"mi"}, + Usage: "Init minor number", + Value: actions.INIT_MINOR_VERSION, + }, + &cli.IntFlag{ + Name: "patch", + Aliases: []string{"p"}, + Usage: "Init patch number", + Value: actions.INIT_PATCH_VERSION, + }, + &cli.BoolFlag{ + Name: "snapshot", + Aliases: []string{"s"}, + Usage: "Enable Snapshots", + }, + }, + Action: func(ctx *cli.Context) error { + store := domain.NewConfigStore(*file) + if store.Exists() { + return fmt.Errorf("Project already initialized!") + } + action := actions.InitAction{ + ArtifactName: ctx.String("name"), + Major: ctx.Int("major"), + Minor: ctx.Int("minor"), + Patch: ctx.Int("patch"), + SnapshotsEnable: ctx.Bool("snapshot"), + } + config, err := action.NewConfig() + if err != nil { + return err + } + return store.SaveConfig(config) + }, } }