Skip to content

Commit c6b12fc

Browse files
committed
refactor(cli): reorganize project structure and improve code readability
- Move pkg_kit and urfave_cli packages to internal/cli_kit - Rename version.go to pkg_kit/version.go - Update import paths and package references - Improve code formatting and add line breaks for readability - Use shorter variable names and remove redundant comments
1 parent 1493a75 commit c6b12fc

File tree

15 files changed

+143
-45
lines changed

15 files changed

+143
-45
lines changed

cmd/cli/app.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
package cli
22

33
import (
4-
"fmt"
54
"github.com/bridgewwater/temp-golang-cli-fast/command"
65
"github.com/bridgewwater/temp-golang-cli-fast/command/subcommand_new"
7-
"github.com/bridgewwater/temp-golang-cli-fast/internal/pkg_kit"
8-
"github.com/bridgewwater/temp-golang-cli-fast/internal/urfave_cli"
9-
"github.com/bridgewwater/temp-golang-cli-fast/internal/urfave_cli/cli_exit_urfave"
6+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/pkg_kit"
7+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/urfave_cli"
8+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/urfave_cli/cli_exit_urfave"
109
"github.com/urfave/cli/v2"
1110
)
1211

1312
const (
14-
// defaultExitCode SIGINT as 2
13+
// defaultExitCode SIGINT as 2.
1514
defaultExitCode = 2
1615
)
1716

1817
func NewCliApp(bdInfo pkg_kit.BuildInfo) *cli.App {
1918
cli_exit_urfave.ChangeDefaultExitCode(defaultExitCode)
19+
2020
app := cli.NewApp()
2121
app.EnableBashCompletion = true
2222
app.Name = bdInfo.PgkNameString()
2323
app.Version = bdInfo.VersionString()
24+
2425
if pkg_kit.GetPackageJsonHomepage() != "" {
25-
app.Usage = fmt.Sprintf("see: %s", pkg_kit.GetPackageJsonHomepage())
26+
app.Usage = "see: " + pkg_kit.GetPackageJsonHomepage()
2627
}
28+
2729
app.Description = pkg_kit.GetPackageJsonDescription()
2830
jsonAuthor := pkg_kit.GetPackageJsonAuthor()
2931
app.Copyright = bdInfo.String()

cmd/temp-golang-cli-fast/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ package main
44

55
import (
66
"fmt"
7-
"github.com/bridgewwater/temp-golang-cli-fast"
7+
os "os"
8+
9+
temp_golang_cli_fast "github.com/bridgewwater/temp-golang-cli-fast"
810
"github.com/bridgewwater/temp-golang-cli-fast/cmd/cli"
911
"github.com/bridgewwater/temp-golang-cli-fast/constant"
12+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/pkg_kit"
1013
"github.com/bridgewwater/temp-golang-cli-fast/internal/d_log"
11-
"github.com/bridgewwater/temp-golang-cli-fast/internal/pkg_kit"
1214
"github.com/gookit/color"
13-
os "os"
1415
)
1516

1617
const (
17-
// exitCodeCmdArgs SIGINT as 2
18+
// exitCodeCmdArgs SIGINT as 2.
1819
exitCodeCmdArgs = 2
1920
)
2021

2122
//nolint:gochecknoglobals
2223
var (
23-
// Populated by goreleaser during build
24+
// Populated by goreleaser during build.
2425
version = "unknown"
2526
rawVersion = "unknown"
2627
buildID string
@@ -57,6 +58,7 @@ func main() {
5758
fmt.Printf("%s %s --help\n", color.Yellow.Render("please see help as:"), app.Name)
5859
os.Exit(exitCodeCmdArgs)
5960
}
61+
6062
if err := app.Run(args); nil != err {
6163
color.Redf("cli err at %v\n", err)
6264
os.Exit(exitCodeCmdArgs)

command/global.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package command
22

33
import (
4+
"errors"
45
"fmt"
6+
"os"
7+
"strings"
8+
59
"github.com/bridgewwater/temp-golang-cli-fast/constant"
10+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/pkg_kit"
11+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/urfave_cli/cli_exit_urfave"
612
"github.com/bridgewwater/temp-golang-cli-fast/internal/d_log"
7-
"github.com/bridgewwater/temp-golang-cli-fast/internal/pkg_kit"
8-
"github.com/bridgewwater/temp-golang-cli-fast/internal/urfave_cli/cli_exit_urfave"
913
"github.com/urfave/cli/v2"
10-
"os"
11-
"strings"
1214
)
1315

1416
type GlobalConfig struct {
@@ -34,9 +36,7 @@ type (
3436
}
3537
)
3638

37-
var (
38-
cmdGlobalEntry *GlobalCommand
39-
)
39+
var cmdGlobalEntry *GlobalCommand
4040

4141
// CmdGlobalEntry
4242
//
@@ -50,10 +50,12 @@ func CmdGlobalEntry() *GlobalCommand {
5050
// do global command exec
5151
func (c *GlobalCommand) globalExec() error {
5252
d_log.Debug("-> start GlobalAction")
53+
5354
if c.Verbose {
5455
d_log.Verbosef("cli exec full: %s", c.RootCfg.ExecFullPath)
5556
d_log.Verbosef(" run path: %s", c.RootCfg.RunRootPath)
5657
d_log.Verbosef(" args len: %v", c.Args.Len())
58+
5759
if c.Args.Len() > 0 {
5860
d_log.Verbosef(" args content: %s", strings.Join(c.Args.Slice(), " | "))
5961
}
@@ -64,7 +66,7 @@ func (c *GlobalCommand) globalExec() error {
6466

6567
// withGlobalFlag
6668
//
67-
// bind global flag to globalExec
69+
// bind global flag to globalExec.
6870
func withGlobalFlag(c *cli.Context, cliVersion, cliName, homePage string) (*GlobalCommand, error) {
6971
d_log.Debug("-> withGlobalFlag")
7072

@@ -79,15 +81,23 @@ func withGlobalFlag(c *cli.Context, cliVersion, cliName, homePage string) (*Glob
7981
if len(cliRunRootPath) == 0 {
8082
rootDir, err := os.Getwd()
8183
if err != nil {
82-
d_log.Errorf(err, "get rooted path name corresponding to the current directory path err")
84+
d_log.Errorf(
85+
err,
86+
"get rooted path name corresponding to the current directory path err",
87+
)
88+
8389
return nil, cli_exit_urfave.Err(err)
8490
}
91+
8592
cliRunRootPath = rootDir
8693
}
8794

8895
timeoutSecond := c.Uint(constant.NameCliTimeoutSecond)
8996
if timeoutSecond < constant.MinimumTimeoutSecond {
90-
d_log.Warnf("timeout second is too small, will use default value: %d", constant.MinimumTimeoutSecond)
97+
d_log.Warnf(
98+
"timeout second is too small, will use default value: %d",
99+
constant.MinimumTimeoutSecond,
100+
)
91101
}
92102

93103
config := GlobalConfig{
@@ -108,6 +118,7 @@ func withGlobalFlag(c *cli.Context, cliVersion, cliName, homePage string) (*Glob
108118
Verbose: isVerbose,
109119
RootCfg: config,
110120
}
121+
111122
return &p, nil
112123
}
113124

@@ -120,10 +131,12 @@ func GlobalBeforeAction(c *cli.Context) error {
120131
}
121132

122133
appName := c.App.Name
134+
123135
cliVersion := c.App.Version
124136
if isVerbose {
125137
d_log.Warnf("-> open verbose, and now command version is: %s", cliVersion)
126138
}
139+
127140
homePage := pkg_kit.GetPackageJsonHomepage()
128141

129142
cmdEntry, err := withGlobalFlag(c, cliVersion, appName, homePage)
@@ -132,20 +145,22 @@ func GlobalBeforeAction(c *cli.Context) error {
132145
}
133146

134147
cmdGlobalEntry = cmdEntry
148+
135149
return nil
136150
}
137151

138152
// GlobalAction
139153
// do command Action flag.
140154
func GlobalAction(c *cli.Context) error {
141155
if cmdGlobalEntry == nil {
142-
panic(fmt.Errorf("not init GlobalBeforeAction success to new cmdGlobalEntry"))
156+
panic(errors.New("not init GlobalBeforeAction success to new cmdGlobalEntry"))
143157
}
144158

145159
err := cmdGlobalEntry.globalExec()
146160
if err != nil {
147161
return cli_exit_urfave.Format("run GlobalAction err: %v", err)
148162
}
163+
149164
return nil
150165
}
151166

@@ -157,8 +172,13 @@ func GlobalAction(c *cli.Context) error {
157172
func GlobalAfterAction(c *cli.Context) error {
158173
if cmdGlobalEntry != nil {
159174
if c.Bool(constant.NameCliVerbose) {
160-
d_log.Infof("-> finish run command: %s, version %s", cmdGlobalEntry.Name, cmdGlobalEntry.Version)
175+
d_log.Infof(
176+
"-> finish run command: %s, version %s",
177+
cmdGlobalEntry.Name,
178+
cmdGlobalEntry.Version,
179+
)
161180
}
162181
}
182+
163183
return nil
164184
}

command/subcommand_new/subCmdNew.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package subcommand_new
22

33
import (
4+
"strings"
5+
46
"github.com/bridgewwater/temp-golang-cli-fast/command"
57
"github.com/bridgewwater/temp-golang-cli-fast/constant"
8+
"github.com/bridgewwater/temp-golang-cli-fast/internal/cli_kit/urfave_cli"
69
"github.com/bridgewwater/temp-golang-cli-fast/internal/d_log"
7-
"github.com/bridgewwater/temp-golang-cli-fast/internal/urfave_cli"
810
"github.com/urfave/cli/v2"
9-
"strings"
1011
)
1112

1213
const commandName = "new"
@@ -29,11 +30,19 @@ type NewCommand struct {
2930
}
3031

3132
func (n *NewCommand) Exec() error {
32-
d_log.Debugf("-> Exec cli [ %s ] by subCommand [ %s ], version %s buildID %s", n.cliName, commandName, n.version, n.buildId)
33+
d_log.Debugf(
34+
"-> Exec cli [ %s ] by subCommand [ %s ], version %s buildID %s",
35+
n.cliName,
36+
commandName,
37+
n.version,
38+
n.buildId,
39+
)
40+
3341
if n.isDebug {
3442
d_log.Verbosef("cli full path: %s", n.execFullPath)
3543
d_log.Verbosef(" run path: %s", n.runRootPath)
3644
d_log.Verbosef(" args len: %v", n.Args.Len())
45+
3746
if n.Args.Len() > 0 {
3847
d_log.Verbosef(" args content: %s", strings.Join(n.Args.Slice(), " | "))
3948
}
@@ -63,7 +72,9 @@ func withEntry(c *cli.Context) (*NewCommand, error) {
6372
if c.Bool("lib") {
6473
d_log.Info("new lib mode")
6574
}
75+
6676
globalEntry := command.CmdGlobalEntry()
77+
6778
return &NewCommand{
6879
cliName: globalEntry.Name,
6980
version: globalEntry.Version,
@@ -83,11 +94,14 @@ func withEntry(c *cli.Context) (*NewCommand, error) {
8394

8495
func action(c *cli.Context) error {
8596
d_log.Debugf("-> Sub Command action [ %s ] start", commandName)
97+
8698
entry, err := withEntry(c)
8799
if err != nil {
88100
return err
89101
}
102+
90103
commandEntry = entry
104+
91105
return commandEntry.Exec()
92106
}
93107

@@ -98,9 +112,9 @@ func Command() []*cli.Command {
98112
Usage: "",
99113
Action: action,
100114

101-
//Flags: flag(),
115+
// Flags: flag(),
102116
// todo: if not use platform config, remove this use method flag()
103-
//Flags: urfave_cli.UrfaveCliAppendCliFlag(flag(), command.HideGlobalFlag()),
117+
// Flags: urfave_cli.UrfaveCliAppendCliFlag(flag(), command.HideGlobalFlag()),
104118
Flags: urfave_cli.UrfaveCliAppendCliFlag(flag(), constant.PlatformFlags()),
105119
},
106120
}
File renamed without changes.

0 commit comments

Comments
 (0)