Skip to content

Commit c1347c3

Browse files
hwbrzzlkrishankumar01almas-x
authored
merge: v1.16.4 (#1245)
* correctly set cc and bcc headers (#1144) * correct the error return from SendMailJob handle (#1147) * fix: [#743] package make command generates correct code (#1151) (#1152) (cherry picked from commit 93dc8a3) * fix: [#749] The path is incorrect when publishing package files (#1157) * chore: optimize assertions for package installation (#1160) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting (#1162) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting * optimize * fix: [#738] The Orm Creating event can be triggered when the query with the Model method (#1166) * fix: [#738] The Orm Creating event can be triggered when the query with the Model method * fix tests * upgrade: v1.16.1 * fix: [#762] handle panic when using transaction (#1183) * fix: [#762] handle panic when using transaction * v1.16.2 * optimize * fix lint * fix: [#768] facades.DB will panic when migrating a new column (#1185) * fix: [#768] facades.DB will panic when migrating a new column * optimize * optimize * feat: [#770] Add a SelectRaw function for the ORM (#1186) * fix: [#770] Add a SelectRaw function for the ORM * fix: [#770] Add a SelectRaw function for the ORM * fix ci * upgrade: v1.16.3 * fix: comand cannot be run concurrently (#1243) * fix: comand cannot be run concurrently * fix ci * fix ci * optimize * optimize * optimize * optimize * optimize global options * fix ci * upgrade v1.16.4 * optimize * optimize --------- Co-authored-by: krishan kumar <84431594+kkumar-gcc@users.noreply.github.com> Co-authored-by: ALMAS <almas.cc@icloud.com>
1 parent 6490c80 commit c1347c3

File tree

10 files changed

+285
-65
lines changed

10 files changed

+285
-65
lines changed

console/application.go

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package console
22

33
import (
44
"context"
5+
"io"
56
"os"
67
"slices"
78
"strings"
@@ -26,25 +27,25 @@ var (
2627
)
2728

2829
type Application struct {
29-
instance *cli.Command
30+
commands []cli.Command
31+
name string
32+
usage string
33+
usageText string
3034
useArtisan bool
35+
version string
36+
writer io.Writer
3137
}
3238

3339
// NewApplication Create a new Artisan application.
3440
// Will add artisan flag to the command if useArtisan is true.
3541
func NewApplication(name, usage, usageText, version string, useArtisan bool) console.Artisan {
36-
instance := &cli.Command{}
37-
instance.Name = name
38-
instance.Usage = usage
39-
instance.UsageText = usageText
40-
instance.Version = version
41-
instance.CommandNotFound = commandNotFound
42-
instance.OnUsageError = onUsageError
43-
instance.Flags = []cli.Flag{noANSIFlag}
44-
4542
return &Application{
46-
instance: instance,
43+
name: name,
44+
usage: usage,
45+
usageText: usageText,
4746
useArtisan: useArtisan,
47+
version: version,
48+
writer: os.Stdout,
4849
}
4950
}
5051

@@ -59,16 +60,22 @@ func (r *Application) Register(commands []console.Command) {
5960
cliCommand := cli.Command{
6061
Name: item.Signature(),
6162
Usage: item.Description(),
62-
Action: func(_ context.Context, cmd *cli.Command) error {
63-
return item.Handle(NewCliContext(cmd))
63+
Action: func(ctx context.Context, cmd *cli.Command) error {
64+
cliCtx := NewCliContext(cmd)
65+
if cliCtx.OptionBool("help") {
66+
return cli.ShowCommandHelp(ctx, cmd, cmd.Name)
67+
}
68+
69+
return item.Handle(cliCtx)
6470
},
6571
Category: item.Extend().Category,
6672
ArgsUsage: item.Extend().ArgsUsage,
6773
Flags: flagsToCliFlags(item.Extend().Flags),
6874
Arguments: arguments,
6975
OnUsageError: onUsageError,
7076
}
71-
r.instance.Commands = append(r.instance.Commands, &cliCommand)
77+
78+
r.commands = append(r.commands, cliCommand)
7279
}
7380
}
7481

@@ -123,13 +130,13 @@ func (r *Application) Run(args []string, exitIfArtisan bool) error {
123130
}
124131

125132
if artisanIndex != -1 {
126-
// Add --help if no command argument is provided.
133+
command := r.command()
127134
if artisanIndex+1 == len(args) {
128-
args = append(args, "--help")
135+
args = append(args, "help")
129136
}
130137

131138
cliArgs := append([]string{args[0]}, args[artisanIndex+1:]...)
132-
if err := r.instance.Run(context.Background(), cliArgs); err != nil {
139+
if err := command.Run(context.Background(), cliArgs); err != nil {
133140
if exitIfArtisan {
134141
panic(err.Error())
135142
}
@@ -145,6 +152,29 @@ func (r *Application) Run(args []string, exitIfArtisan bool) error {
145152
return nil
146153
}
147154

155+
func (r *Application) command() *cli.Command {
156+
commands := make([]*cli.Command, len(r.commands))
157+
for i, cmd := range r.commands {
158+
commands[i] = &cmd
159+
}
160+
161+
command := &cli.Command{}
162+
command.CommandNotFound = commandNotFound
163+
command.Commands = commands
164+
command.Flags = []cli.Flag{noANSIFlag}
165+
command.Name = r.name
166+
command.OnUsageError = onUsageError
167+
command.Usage = r.usage
168+
command.UsageText = r.usageText
169+
command.Version = r.version
170+
command.Writer = r.writer
171+
172+
// There is a concurrency issue with urfave/cli v3 when help is not hidden.
173+
command.HideHelp = true
174+
175+
return command
176+
}
177+
148178
func flagsToCliFlags(flags []command.Flag) []cli.Flag {
149179
var cliFlags []cli.Flag
150180
for _, flag := range flags {
@@ -234,6 +264,34 @@ func flagsToCliFlags(flags []command.Flag) []cli.Flag {
234264
}
235265
}
236266

267+
var (
268+
existHelp bool
269+
existH bool
270+
)
271+
for _, flag := range cliFlags {
272+
names := flag.Names()
273+
if slices.Contains(names, "help") {
274+
existHelp = true
275+
}
276+
if slices.Contains(names, "h") {
277+
existH = true
278+
}
279+
}
280+
281+
if !existHelp {
282+
helpFlag := &cli.BoolFlag{
283+
Name: "help",
284+
Usage: "Show help",
285+
HideDefault: true,
286+
}
287+
if !existH {
288+
helpFlag.Aliases = []string{"h"}
289+
}
290+
cliFlags = append(cliFlags, helpFlag)
291+
}
292+
293+
cliFlags = append(cliFlags, noANSIFlag)
294+
237295
return cliFlags
238296
}
239297

console/application_test.go

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package console
22

33
import (
4+
"sync"
5+
"sync/atomic"
46
"testing"
57
"time"
68

@@ -11,16 +13,47 @@ import (
1113
"github.com/goravel/framework/contracts/console/command"
1214
)
1315

14-
var testCommand = 0
16+
var (
17+
testCommand int64
18+
testCommand1 atomic.Int64
19+
testCommand2 atomic.Int64
20+
)
1521

1622
func TestRun(t *testing.T) {
23+
testCommand = 0
1724
cliApp := NewApplication("test", "test", "test", "test", true)
1825
cliApp.Register([]console.Command{
1926
&TestCommand{},
2027
})
2128

2229
assert.NoError(t, cliApp.Call("test"))
23-
assert.Equal(t, 1, testCommand)
30+
assert.Equal(t, int64(1), testCommand)
31+
}
32+
33+
func TestRun_Concurrent(t *testing.T) {
34+
testCommand1.Store(0)
35+
testCommand2.Store(0)
36+
37+
cliApp := NewApplication("test", "test", "test", "test", true)
38+
cliApp.Register([]console.Command{
39+
&TestCommand1{},
40+
&TestCommand2{},
41+
})
42+
43+
var wg sync.WaitGroup
44+
for i := 0; i < 100; i++ {
45+
wg.Add(1)
46+
go func() {
47+
_ = cliApp.Call("test1")
48+
_ = cliApp.Call("test2")
49+
wg.Done()
50+
}()
51+
}
52+
53+
wg.Wait()
54+
55+
assert.Equal(t, int64(100), testCommand1.Load())
56+
assert.Equal(t, int64(100), testCommand2.Load())
2457
}
2558

2659
func TestFlagsToCliFlags(t *testing.T) {
@@ -43,7 +76,7 @@ func TestFlagsToCliFlags(t *testing.T) {
4376
assert.NotNil(t, cliFlags)
4477

4578
// Assert that the number of CLI flags matches the number of command flags
46-
assert.Equal(t, len(cliFlags), len(flags))
79+
assert.Equal(t, len(cliFlags), len(flags)+2) // +2 for noANSIFlag and helpFlag
4780

4881
// Assert that each CLI flag has the expected name, aliases, usage, required, and value
4982
for i, flag := range flags {
@@ -438,3 +471,45 @@ func (receiver *TestCommand) Handle(ctx console.Context) error {
438471

439472
return nil
440473
}
474+
475+
type TestCommand1 struct {
476+
}
477+
478+
func (receiver *TestCommand1) Signature() string {
479+
return "test1"
480+
}
481+
482+
func (receiver *TestCommand1) Description() string {
483+
return "Test command1"
484+
}
485+
486+
func (receiver *TestCommand1) Extend() command.Extend {
487+
return command.Extend{}
488+
}
489+
490+
func (receiver *TestCommand1) Handle(ctx console.Context) error {
491+
testCommand1.Add(1)
492+
493+
return nil
494+
}
495+
496+
type TestCommand2 struct {
497+
}
498+
499+
func (receiver *TestCommand2) Signature() string {
500+
return "test2"
501+
}
502+
503+
func (receiver *TestCommand2) Description() string {
504+
return "Test command2"
505+
}
506+
507+
func (receiver *TestCommand2) Extend() command.Extend {
508+
return command.Extend{}
509+
}
510+
511+
func (receiver *TestCommand2) Handle(ctx console.Context) error {
512+
testCommand2.Add(1)
513+
514+
return nil
515+
}

console/cli_context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ func (r *CliContext) Info(message string) {
141141
color.Infoln(message)
142142
}
143143

144+
func (r *CliContext) Instance() *cli.Command {
145+
return r.instance
146+
}
147+
144148
func (r *CliContext) Line(message string) {
145149
color.Default().Println(message)
146150
}

console/cli_helper.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func init() {
2525
cli.CommandHelpTemplate = commandHelpTemplate
2626
cli.SubcommandHelpTemplate = commandHelpTemplate
2727
cli.VersionPrinter = printVersion
28-
huh.ErrUserAborted = cli.Exit(color.Red().Sprint("Cancelled."), 0)
28+
huh.ErrUserAborted = cli.Exit(color.Red().Sprint("Cancelled"), 0)
2929
}
3030

3131
const maxLineLength = 10000
@@ -46,9 +46,7 @@ var (
4646
{{ (colorize .Usage) }}
4747
4848
{{ yellow "Usage:" }}
49-
{{template "usageTemplate" .}}{{with $root := .Root}}
50-
51-
{{ yellow "Global options:" }}{{template "flagTemplate" (sortVisibleFlags $root)}}{{end}}{{if .VisibleFlags}}
49+
{{template "usageTemplate" .}}{{with $root := .Root}}{{end}}{{if .VisibleFlags}}
5250
5351
{{ yellow "Options:" }}{{template "flagTemplate" (sortVisibleFlags .)}}{{end}}
5452
`
@@ -209,18 +207,6 @@ func handleNoANSI() {
209207
}
210208

211209
func helpName(fullName string) string {
212-
var namePath []string
213-
for i, name := range strings.Split(fullName, " ") {
214-
namePath = append(namePath, name)
215-
if i == 0 {
216-
namePath = append(namePath, "[global options]")
217-
}
218-
}
219-
220-
if len(namePath) > 1 {
221-
fullName = strings.Join(namePath, " ")
222-
}
223-
224210
return fullName
225211
}
226212

0 commit comments

Comments
 (0)