Skip to content

Commit 7992287

Browse files
authored
Use cobra command for argument handling
While this doesn't add any options or arguments, it does set up the command to recognize them. Specifically, that it shouldn't receive any arguments or options. This results in an error message when a user executes the command with additional arguments or options, so that usage like `git lzc --version` won't result in the executable surprisingly generating a commit. Fixes #10
1 parent 3be15d4 commit 7992287

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

cmd/git-lzc/main.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,13 @@ package main
33
import (
44
"fmt"
55
"os"
6-
7-
lazycommit "github.com/spenserblack/git-lazy-commit"
86
)
97

108
func main() {
11-
repo := lazycommit.Repo(".")
12-
13-
noStaged, err := repo.NoStaged()
14-
onError(err)
15-
16-
if noStaged {
17-
onError(repo.StageAll())
9+
if err := rootCmd.Execute(); err != nil {
10+
fmt.Fprintln(os.Stderr, err)
11+
os.Exit(1)
1812
}
19-
20-
out, err := repo.Commit()
21-
onError(err)
22-
23-
fmt.Printf("%s", out)
2413
}
2514

2615
func onError(err error) {

go.mod

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ module github.com/spenserblack/git-lazy-commit
22

33
go 1.20
44

5-
require github.com/cli/safeexec v1.0.1
5+
require (
6+
github.com/cli/safeexec v1.0.1
7+
github.com/spf13/cobra v1.6.1
8+
)
9+
10+
require (
11+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
12+
github.com/spf13/pflag v1.0.5 // indirect
13+
)

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
github.com/cli/safeexec v1.0.1 h1:e/C79PbXF4yYTN/wauC4tviMxEV13BwljGj0N9j+N00=
22
github.com/cli/safeexec v1.0.1/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
4+
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
5+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
6+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
7+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8+
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
9+
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
10+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
11+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
12+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)