Skip to content

Commit 1cd52b2

Browse files
committed
chore: setup for multiple sub command
creates sub packages to help with multiple sub commands, this is boiler code for the release command
1 parent bfbfbfe commit 1cd52b2

File tree

5 files changed

+61
-28
lines changed

5 files changed

+61
-28
lines changed

cmd/cmd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
commitlogCmd "github.com/barelyhuman/commitlog/cmd/commitlog"
7+
releaseCmd "github.com/barelyhuman/commitlog/cmd/release"
8+
)
9+
10+
// Init - initializes the commands and also handles the classification
11+
func Init() {
12+
// Install the needed commands from the cmd package
13+
releaseCmd.Install()
14+
commitlogCmd.Install()
15+
16+
// Classify which command to run, also pass the arguments to the command to parse
17+
if len(os.Args) > 1 {
18+
switch os.Args[1] {
19+
case "release":
20+
{
21+
releaseCmd.Run(os.Args[2:])
22+
return
23+
}
24+
}
25+
}
26+
27+
// Default Command, parse all flags
28+
commitlogCmd.Run()
29+
}

cmd/release/release.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package release
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
var releaseCmd *flag.FlagSet
9+
10+
// Install - add flags and other options
11+
func Install() {
12+
releaseCmd = flag.NewFlagSet("release", flag.ExitOnError)
13+
releaseCmd.Bool("-major", false, "If release is a major one, will increment the x.0.0 ")
14+
releaseCmd.Bool("-minor", false, "If release is a minor one, will increment the 0.x.0 ")
15+
releaseCmd.Bool("-patch", false, "If release is a patch, will increment the 0.0.x ")
16+
releaseCmd.Bool("-beta", false, "If the release is a beta, to add/increment tag by `-beta.x`")
17+
releaseCmd.String("-tag", "", "The Tag to be taken as base")
18+
}
19+
20+
// Run - execute the command
21+
func Run(args []string) {
22+
releaseCmd.Parse(args)
23+
fmt.Println("Note: The release command is not yet implemented")
24+
}

log/gitutils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ func openRepository(path string) *git.Repository {
120120

121121
// GetCommitFromString - get commit from hash string
122122
func GetCommitFromString(commitString string, repo *git.Repository) *object.Commit {
123+
if commitString == "" {
124+
return nil
125+
}
126+
123127
hash, err := repo.ResolveRevision(plumbing.Revision(commitString))
124128
if err != nil {
125129
log.Fatal("Unable to get Repo head:", err)

log/log.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,8 @@ func CommitLog(path string, startCommitString string, endCommitString string, in
146146
return "", ErrMessage{"Unable to get repository HEAD:", err}
147147
}
148148

149-
if startCommitString != "" {
150-
startHash = GetCommitFromString(startCommitString, currentRepository)
151-
}
152-
153-
if endCommitString != "" {
154-
endHash = GetCommitFromString(endCommitString, currentRepository)
155-
}
149+
startHash = GetCommitFromString(startCommitString, currentRepository)
150+
endHash = GetCommitFromString(endCommitString, currentRepository)
156151

157152
if startHash != nil {
158153
cIter, err = currentRepository.Log(&git.LogOptions{From: startHash.Hash})

main.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,9 @@
33
package main
44

55
import (
6-
"flag"
7-
"fmt"
8-
"log"
9-
10-
clog "github.com/barelyhuman/commitlog/log"
6+
"github.com/barelyhuman/commitlog/cmd"
117
)
128

139
func main() {
14-
var repoPath = flag.String("p", ".", "path to the repository, points to the current working directory by default")
15-
var startCommit = flag.String("s", "", "commit hash string / revision (ex. HEAD, HEAD^, HEAD~2) \n to start collecting commit messages from")
16-
var endCommit = flag.String("e", "", "commit hash string / revision (ex. HEAD, HEAD^, HEAD~2) \n to stop collecting commit message at")
17-
var inclusionFlags = flag.String("i", clog.SupportedKeys, "commit types to be includes")
18-
var skipClassification = flag.Bool("skip", false, "if true will skip trying to classify and just give a list of changes")
19-
20-
flag.Parse()
21-
22-
changelog, err := clog.CommitLog(*repoPath, *startCommit, *endCommit, *inclusionFlags, *skipClassification)
23-
24-
if err.Err != nil {
25-
log.Fatal(err.Message, err.Err)
26-
}
27-
28-
fmt.Println(changelog)
29-
10+
cmd.Init()
3011
}

0 commit comments

Comments
 (0)