-
Notifications
You must be signed in to change notification settings - Fork 105
feat: Add artisan about command #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b513bd0
feat: Add artisan about command
almas-x d8e61ce
feat: Add artisan about command
almas-x 09baf08
Merge remote-tracking branch 'origin/artisan-about-command' into arti…
almas-x b76c5ce
Merge branch 'master' into artisan-about-command
hwbrzzl 2504590
Merge remote-tracking branch 'origin/artisan-about-command' into arti…
almas-x 9c32392
chore: refactor code structure and add unit tests
almas-x 01a57b8
Update contracts/foundation/application.go
almas-x 9357c19
Update contracts/foundation/application.go
almas-x 4c29cc8
fix: refactor value name
almas-x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package console | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "runtime" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/goravel/framework/contracts/console" | ||
| "github.com/goravel/framework/contracts/console/command" | ||
| "github.com/goravel/framework/contracts/foundation" | ||
| "github.com/goravel/framework/support/str" | ||
| ) | ||
|
|
||
| type AboutCommand struct { | ||
| app foundation.Application | ||
| } | ||
|
|
||
| type information struct { | ||
| section map[string]int | ||
| details [][]kv | ||
| } | ||
| type kv struct { | ||
| key string | ||
| value string | ||
| } | ||
|
|
||
| var appInformation = &information{section: make(map[string]int)} | ||
| var customInformationResolvers []func() | ||
|
|
||
| func NewAboutCommand(app foundation.Application) *AboutCommand { | ||
| return &AboutCommand{ | ||
| app: app, | ||
| } | ||
| } | ||
|
|
||
| // Signature The name and signature of the console command. | ||
| func (receiver *AboutCommand) Signature() string { | ||
| return "about" | ||
| } | ||
|
|
||
| // Description The console command description. | ||
| func (receiver *AboutCommand) Description() string { | ||
| return "Display basic information about your application" | ||
| } | ||
|
|
||
| // Extend The console command extend. | ||
| func (receiver *AboutCommand) Extend() command.Extend { | ||
| return command.Extend{ | ||
| Flags: []command.Flag{ | ||
| &command.StringFlag{ | ||
| Name: "only", | ||
| Usage: "The section to display", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // Handle Execute the console command. | ||
| func (receiver *AboutCommand) Handle(ctx console.Context) error { | ||
| receiver.gatherApplicationInformation() | ||
| ctx.NewLine() | ||
| appInformation.Range(ctx.Option("only"), func(section string, details []kv) { | ||
| ctx.TwoColumnDetail("<fg=green;op=bold>"+section+"</>", "") | ||
| for i := range details { | ||
| ctx.TwoColumnDetail(details[i].key, details[i].value) | ||
| } | ||
| ctx.NewLine() | ||
| }) | ||
| return nil | ||
| } | ||
|
|
||
| // gatherApplicationInformation Gather information about the application. | ||
| func (receiver *AboutCommand) gatherApplicationInformation() { | ||
| configFacade := receiver.app.MakeConfig() | ||
| appInformation.addToSection("Environment", | ||
| "Application Name", configFacade.GetString("app.name"), | ||
| "Goravel Version", str.Of(receiver.app.Version()).LTrim("v").String(), | ||
| "Go Version", str.Of(runtime.Version()).LTrim("go").String(), | ||
| "Environment", configFacade.GetString("app.env"), | ||
| "Debug Mode", func() string { | ||
| if configFacade.GetBool("app.debug") { | ||
| return "<fg=yellow;op=bold>ENABLED</>" | ||
| } | ||
| return "OFF" | ||
| }(), | ||
| "URL", str.Of(configFacade.GetString("http.url")).Replace("http://", "").Replace("https://", "").String(), | ||
almas-x marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "HTTP Host", configFacade.GetString("http.host"), | ||
| "HTTP Port", configFacade.GetString("http.port"), | ||
| ) | ||
| appInformation.addToSection("Drivers", | ||
| "Cache", configFacade.GetString("cache.default"), | ||
| "Database", configFacade.GetString("database.default"), | ||
| "Hashing", configFacade.GetString("hashing.driver"), | ||
| "Http", configFacade.GetString("http.default"), | ||
| "Logs", func() string { | ||
| logChannel := configFacade.GetString("logging.default") | ||
| if configFacade.GetString("logging.channels."+logChannel+".driver") == "stack" { | ||
| if secondary, ok := configFacade.Get("logging.channels." + logChannel + ".channels").([]string); ok { | ||
| return fmt.Sprintf("<fg=yellow;op=bold>%s</> <fg=gray;op=bold>/</> %s", logChannel, strings.Join(secondary, ", ")) | ||
| } | ||
| } | ||
| return logChannel | ||
| }(), | ||
| "Mail", configFacade.GetString("mail.default", "smtp"), | ||
| "Queue", configFacade.GetString("queue.default"), | ||
| "Session", configFacade.GetString("session.driver"), | ||
| ) | ||
| for i := range customInformationResolvers { | ||
| customInformationResolvers[i]() | ||
| } | ||
| } | ||
|
|
||
| // addToSection Add a new section to the application information. | ||
| func (info *information) addToSection(section, key, vale string, more ...string) { | ||
hwbrzzl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| index, ok := info.section[section] | ||
| if !ok { | ||
| index = len(info.details) | ||
| info.section[section] = index | ||
| info.details = append(info.details, make([]kv, 0)) | ||
| } | ||
| info.details[index] = append(info.details[index], kv{key, vale}) | ||
| for i := 0; i < len(more); i += 2 { | ||
| detail := kv{key: more[i]} | ||
| if i+1 < len(more) { | ||
| detail.value = more[i+1] | ||
| } | ||
| info.details[index] = append(info.details[index], detail) | ||
| } | ||
| } | ||
|
|
||
| // Range Iterate over the application information sections. | ||
| func (info *information) Range(section string, ranger func(s string, details []kv)) { | ||
almas-x marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var sections []string | ||
| for s := range info.section { | ||
| if len(section) == 0 || strings.EqualFold(section, s) { | ||
| sections = append(sections, s) | ||
| } | ||
| } | ||
| if len(sections) > 1 { | ||
| sort.Slice(sections, func(i, j int) bool { | ||
| return info.section[sections[i]] < info.section[sections[j]] | ||
| }) | ||
| } | ||
| for i := range sections { | ||
| ranger(sections[i], info.details[info.section[sections[i]]]) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // AddAboutInformation Add custom information to the application information. | ||
| func AddAboutInformation(section, key, value string, more ...string) { | ||
almas-x marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| customInformationResolvers = append(customInformationResolvers, func() { | ||
| appInformation.addToSection(section, key, value, more...) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package console | ||
|
|
||
| import ( | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
|
|
||
| mocksconfig "github.com/goravel/framework/mocks/config" | ||
| consolemocks "github.com/goravel/framework/mocks/console" | ||
| mocksfoundation "github.com/goravel/framework/mocks/foundation" | ||
| "github.com/goravel/framework/support/color" | ||
| ) | ||
|
|
||
| func TestAboutCommand(t *testing.T) { | ||
| mockApp := mocksfoundation.NewApplication(t) | ||
| mockConfig := mocksconfig.NewConfig(t) | ||
| mockApp.EXPECT().MakeConfig().Return(mockConfig).Once() | ||
| mockApp.EXPECT().Version().Return("") | ||
| mockConfig.EXPECT().GetString("logging.default").Return("stack").Once() | ||
| mockConfig.EXPECT().GetString("logging.channels.stack.driver").Return("stack").Once() | ||
| mockConfig.EXPECT().Get("logging.channels.stack.channels").Return([]string{"test"}).Once() | ||
| mockConfig.EXPECT().GetString(mock.Anything).Return("") | ||
| mockConfig.EXPECT().GetString(mock.Anything, mock.Anything).Return("") | ||
| mockConfig.EXPECT().GetBool(mock.Anything).Return(true) | ||
| aboutCommand := NewAboutCommand(mockApp) | ||
| mockContext := &consolemocks.Context{} | ||
| mockContext.EXPECT().NewLine().Return() | ||
| mockContext.EXPECT().Option("only").Return("").Once() | ||
| mockContext.EXPECT().TwoColumnDetail(mock.Anything, mock.Anything).Return() | ||
| AddAboutInformation("Custom", "Test Info", "<fg=cyan>OK</>") | ||
| color.CaptureOutput(func(w io.Writer) { | ||
| assert.Nil(t, aboutCommand.Handle(mockContext)) | ||
| }) | ||
| appInformation.Range("", func(section string, details []kv) { | ||
| assert.Contains(t, []string{"Environment", "Drivers", "Custom"}, section) | ||
| assert.NotEmpty(t, details) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.