Skip to content

Commit

Permalink
refactor: structure
Browse files Browse the repository at this point in the history
  • Loading branch information
KoNekoD committed Oct 12, 2024
1 parent 04d398a commit 0fe39e2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
6 changes: 6 additions & 0 deletions pkg/domain/dtos/commands_options/changed_files_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package commands_options

type ChangedFilesOptions struct {
WithDependencies bool
Files []string
}
44 changes: 9 additions & 35 deletions pkg/infrastructure/commands/changed_files_command.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,25 @@
package commands

import (
"github.com/KoNekoD/go-deptrac/pkg/infrastructure/services"
"github.com/KoNekoD/go-deptrac/pkg/infrastructure/services/formatters"
services "github.com/KoNekoD/go-deptrac/pkg/application/services"
"github.com/KoNekoD/go-deptrac/pkg/domain/dtos/commands_options"
"github.com/KoNekoD/go-deptrac/pkg/infrastructure/services/runners"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type ChangedFilesCommand struct {
runner *runners.ChangedFilesRunner
runner *runners.ChangedFilesRunner
options *commands_options.ChangedFilesOptions
}

const (
argWithDependencies = "with-dependencies"
argFiles = "files"
)

func NewChangedFilesCommand(runner *runners.ChangedFilesRunner) *cobra.Command {
cmd := &ChangedFilesCommand{runner: runner}

cobraCmd := &cobra.Command{
Use: "debug:changed-files",
Short: "Lists layers corresponding to the changed files",
RunE: cmd.run,
}

cobraCmd.Flags().BoolP("verbose", "v", false, "verbose output")
cobraCmd.Flags().BoolP("debug", "d", false, "debug output")

cobraCmd.Flags().Bool(argWithDependencies, false, "show dependent layers")
cobraCmd.Flags().StringArray(argFiles, []string{}, "List of changed files")
_ = cobraCmd.MarkFlagRequired(argFiles)

return cobraCmd
func NewChangedFilesCommand(runner *runners.ChangedFilesRunner, options *commands_options.ChangedFilesOptions) *ChangedFilesCommand {
return &ChangedFilesCommand{runner: runner, options: options}
}

func (cmd *ChangedFilesCommand) run(cobraCmd *cobra.Command, args []string) error {
symfonyOutput := services.NewSymfonyOutput(formatters.NewStyle(cobraCmd.Flags().Changed("verbose"), cobraCmd.Flags().Changed("debug")))

files, err := cobraCmd.Flags().GetStringArray(argFiles)
if err != nil {
return errors.WithStack(err)
}

err = cmd.runner.Run(files, cobraCmd.Flags().Changed(argWithDependencies), symfonyOutput)
func (c *ChangedFilesCommand) Run(output services.OutputInterface) error {
err := c.runner.Run(c.options.Files, c.options.WithDependencies, output)
if err != nil {
output.GetStyle().Error(services.StringOrArrayOfStrings{String: "<fg=red>Changed files debugging failed.</> error: " + err.Error()})
return errors.WithStack(err)
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/infrastructure/di/container_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ type ContainerBuilder struct {
FormatterProvider *formatters.FormatterProvider
FormatterConfiguration *formatters.FormatterConfiguration
AnalyseRunner *runners.AnalyseRunner
AnalyseCommand *commands.AnalyseCommand
NodeNamer *domainServices.NodeNamer
AnalyseOptions *commands_options.AnalyseOptions
AnalyseCommand *commands.AnalyseCommand
ChangedFilesCommand *commands.ChangedFilesCommand
DebugDependenciesCommand *commands.DebugDependenciesCommand
DebugLayerCommand *commands.DebugLayerCommand
DebugTokenCommand *commands.DebugTokenCommand
DebugUnassignedCommand *commands.DebugUnassignedCommand
DebugUnusedCommand *commands.DebugUnusedCommand
InitCommand *commands.InitCommand
}

func NewContainerBuilder(workingDirectory string) *ContainerBuilder {
Expand Down
32 changes: 31 additions & 1 deletion pkg/infrastructure/di/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ func Services(builder *ContainerBuilder) error {
nil != failOnUncovered && *failOnUncovered == true,
)
RegForAnalyseCommand(consoleSubscriber, progressSubscriber, !analyseOptions.NoProgress)

// TODO
debugDependenciesOptions := &commands_options.DebugDependenciesOptions{}
debugLayerOptions := &commands_options.DebugLayerOptions{}
debugTokenOptions := &commands_options.DebugTokenOptions{}
debugUnusedOptions := &commands_options.DebugUnusedOptions{}
initOptions := &commands_options.InitOptions{}
changedFilesOptions := &commands_options.ChangedFilesOptions{}
//

/*
Expand Down Expand Up @@ -258,7 +266,21 @@ func Services(builder *ContainerBuilder) error {
* Console
*/
analyseRunner := runners.NewAnalyseRunner(dependencyLayersAnalyser, formatterProvider)
changedFilesRunner := runners.NewChangedFilesRunner(layerForTokenAnalyser, dependencyLayersAnalyser)
debugDependenciesRunner := runners.NewDebugDependenciesRunner(layerDependenciesAnalyser)
debugLayerRunner := runners.NewDebugLayerRunner(tokenInLayerAnalyser, builderConfiguration.Layers)
debugTokenRunner := runners.NewDebugTokenRunner(layerForTokenAnalyser)
debugUnassignedRunner := runners.NewDebugUnassignedRunner(unassignedTokenAnalyser)
debugUnusedRunner := runners.NewDebugUnusedRunner(rulesetUsageAnalyser)

analyseCommand := commands.NewAnalyseCommand(analyseRunner, eventDispatcher, formatterProvider, *verboseBoolFlag, *debugBoolFlag, consoleSubscriber, progressSubscriber, analyseOptions)
changedFilesCommand := commands.NewChangedFilesCommand(changedFilesRunner, changedFilesOptions)
debugDependenciesCommand := commands.NewDebugDependenciesCommand(debugDependenciesRunner, debugDependenciesOptions)
debugLayerCommand := commands.NewDebugLayerCommand(debugLayerRunner, debugLayerOptions)
debugTokenCommand := commands.NewDebugTokenCommand(debugTokenRunner, debugTokenOptions)
debugUnassignedCommand := commands.NewDebugUnassignedCommand(debugUnassignedRunner)
debugUnusedCommand := commands.NewDebugUnusedCommand(debugUnusedRunner, debugUnusedOptions)
initCommand := commands.NewInitCommand(dumper, initOptions)

// TODO: other commands
// $services->set(InitCommand::class)->autowire()->tag('console_supportive.command');
Expand Down Expand Up @@ -317,10 +339,18 @@ func Services(builder *ContainerBuilder) error {
builder.FormatterProvider = formatterProvider
builder.FormatterConfiguration = formatterConfiguration
builder.AnalyseRunner = analyseRunner
builder.AnalyseCommand = analyseCommand
builder.NodeNamer = nodeNamer
builder.AnalyseOptions = analyseOptions

builder.AnalyseCommand = analyseCommand
builder.ChangedFilesCommand = changedFilesCommand
builder.DebugDependenciesCommand = debugDependenciesCommand
builder.DebugLayerCommand = debugLayerCommand
builder.DebugTokenCommand = debugTokenCommand
builder.DebugUnassignedCommand = debugUnassignedCommand
builder.DebugUnusedCommand = debugUnusedCommand
builder.InitCommand = initCommand

return nil
}

Expand Down

0 comments on commit 0fe39e2

Please sign in to comment.