Skip to content

Commit 1e30d50

Browse files
committed
refactor: restructure project and decouple services via interfaces
1 parent 3259751 commit 1e30d50

37 files changed

+638
-297
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import (
88
"strings"
99
)
1010

11-
type InputService interface {
12-
AskUser(prompt string, options []string) (string, error)
13-
GetPipedInput() (string, bool)
14-
}
15-
1611
type StdInputService struct {
1712
reader io.Reader
1813
}

cmd/add.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import (
55
"path/filepath"
66
"strings"
77

8-
"github.com/hurtki/configsManager/services"
98
"github.com/spf13/cobra"
109
)
1110

1211
type AddCmd struct {
1312
Command *cobra.Command
14-
AppConfigService services.AppConfigService
15-
InputService services.InputService
16-
ConfigsListService services.ConfigsListService
17-
OsService services.OsService
13+
AppConfigService AppConfigService
14+
InputService InputService
15+
ConfigsListService ConfigsListService
16+
OsService OsService
1817
}
1918

2019
func (c *AddCmd) run(cmd *cobra.Command, args []string) error {
@@ -125,10 +124,10 @@ func (c *AddCmd) run(cmd *cobra.Command, args []string) error {
125124
return nil
126125
}
127126

128-
func NewAddCmd(AppConfigService services.AppConfigService,
129-
InputService services.InputService,
130-
ConfigsListService services.ConfigsListService,
131-
OsService services.OsService,
127+
func NewAddCmd(AppConfigService AppConfigService,
128+
InputService InputService,
129+
ConfigsListService ConfigsListService,
130+
OsService OsService,
132131
) *AddCmd {
133132
addCmd := AddCmd{
134133
AppConfigService: AppConfigService,

cmd/cat.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/hurtki/configsManager/services"
5+
66
"github.com/spf13/cobra"
77
)
88

99
type CatCmd struct {
1010
Command *cobra.Command
11-
AppConfigService services.AppConfigService
12-
ConfigsListService services.ConfigsListService
13-
OsService services.OsService
11+
AppConfigService AppConfigService
12+
ConfigsListService ConfigsListService
13+
OsService OsService
1414
}
1515

1616
// catCmd represents the cat command
@@ -38,9 +38,9 @@ func (c *CatCmd) run(cmd *cobra.Command, args []string) error {
3838
return nil
3939
}
4040

41-
func NewCatCmd(AppConfig services.AppConfigService,
42-
ConfigsListService services.ConfigsListService,
43-
OsService services.OsService,
41+
func NewCatCmd(AppConfig AppConfigService,
42+
ConfigsListService ConfigsListService,
43+
OsService OsService,
4444
) *CatCmd {
4545
catCmd := CatCmd{
4646
AppConfigService: AppConfig,
@@ -51,13 +51,13 @@ func NewCatCmd(AppConfig services.AppConfigService,
5151
cmd := &cobra.Command{
5252
Use: "cat [key]",
5353
Short: "Print the content of the config file for a given key",
54-
Long: `The 'cat' command fetches and displays the full content of the configuration file
54+
Long: `The 'cat' command fetches and displays the full content of the configuration file
5555
associated with the specified key from the user's saved configs list.
5656
5757
Usage example:
5858
cm cat myconfig
5959
60-
This command is useful when you want to quickly inspect the contents of a configuration
60+
This command is useful when you want to quickly inspect the contents of a configuration
6161
file without opening it in an editor.`,
6262
RunE: catCmd.run,
6363
}

cmd/init.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/hurtki/configsManager/services"
5+
6+
"github.com/hurtki/configsManager/internal/services"
67
"github.com/spf13/cobra"
78
)
89

910
type InitCmd struct {
1011
Command *cobra.Command
11-
AppConfigService services.AppConfigService
12-
ConfigsListService services.ConfigsListService
12+
AppConfigService AppConfigService
13+
ConfigsListService ConfigsListService
1314
}
1415

1516
func (c *InitCmd) run(cmd *cobra.Command, args []string) error {
@@ -37,8 +38,8 @@ func (c *InitCmd) run(cmd *cobra.Command, args []string) error {
3738
return nil
3839
}
3940

40-
func NewInitCmd(AppConfig services.AppConfigService,
41-
ConfigsListService services.ConfigsListService,
41+
func NewInitCmd(AppConfig AppConfigService,
42+
ConfigsListService ConfigsListService,
4243
) InitCmd {
4344
InitCmd := InitCmd{
4445
AppConfigService: AppConfig,
@@ -48,10 +49,10 @@ func NewInitCmd(AppConfig services.AppConfigService,
4849
cmd := &cobra.Command{
4950
Use: "init",
5051
Short: "Initializes anapp folder",
51-
Long: `The 'init' command initializes the configuration management folder structure
52+
Long: `The 'init' command initializes the configuration management folder structure
5253
and creates default configuration files if they do not exist.
5354
54-
This is the first command that should be run before using other commands
55+
This is the first command that should be run before using other commands
5556
like 'add', 'open', or 'edit', since it sets up the required files and directories.
5657
5758
Usage example:

cmd/interfaces.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"github.com/hurtki/configsManager/internal/config"
5+
"github.com/hurtki/configsManager/internal/domain"
6+
)
7+
8+
type AppConfigService interface {
9+
Load() (*config.AppConfig, error)
10+
Save(*config.AppConfig) error
11+
}
12+
13+
type ConfigsListService interface {
14+
Load() (*domain.ConfigsList, error)
15+
Save(*domain.ConfigsList) error
16+
GenerateUniqueKeyForPath(path string) (string, error)
17+
}
18+
19+
type OsService interface {
20+
GetFileData(path string) ([]byte, error)
21+
OpenInEditor(editor, path string) error
22+
FileExists(path string) (bool, error)
23+
GetAbsolutePath(path string) (string, error)
24+
MakePathAndFile(path string) error
25+
WriteFile(path string, data []byte) error
26+
GetHomeDir() (string, error)
27+
}
28+
29+
type InputService interface {
30+
AskUser(prompt string, options []string) (string, error)
31+
GetPipedInput() (string, bool)
32+
}

cmd/keys.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/hurtki/configsManager/services"
5+
66
"github.com/spf13/cobra"
77
)
88

99
type KeysCmd struct {
1010
Command *cobra.Command
11-
AppConfigService services.AppConfigService
12-
ConfigsListService services.ConfigsListService
11+
AppConfigService AppConfigService
12+
ConfigsListService ConfigsListService
1313
}
1414

1515
func (c *KeysCmd) run(cmd *cobra.Command, args []string) error {
@@ -26,8 +26,8 @@ func (c *KeysCmd) run(cmd *cobra.Command, args []string) error {
2626
return nil
2727
}
2828

29-
func NewKeysCmd(AppConfig services.AppConfigService,
30-
ConfigsListService services.ConfigsListService,
29+
func NewKeysCmd(AppConfig AppConfigService,
30+
ConfigsListService ConfigsListService,
3131
) KeysCmd {
3232
keysCmd := KeysCmd{
3333
AppConfigService: AppConfig,
@@ -37,13 +37,13 @@ func NewKeysCmd(AppConfig services.AppConfigService,
3737
cmd := &cobra.Command{
3838
Use: "keys",
3939
Short: "List all configuration keys",
40-
Long: `The 'keys' command outputs all available configuration keys stored
40+
Long: `The 'keys' command outputs all available configuration keys stored
4141
in the user's configuration list.
4242
4343
Usage example:
4444
cm keys
4545
46-
This command is useful to quickly see what configuration entries exist
46+
This command is useful to quickly see what configuration entries exist
4747
and can be accessed or managed with other commands.`,
4848
RunE: keysCmd.run,
4949
}

cmd/open.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/hurtki/configsManager/services"
76
"github.com/spf13/cobra"
87
)
98

109
type OpenCmd struct {
1110
Command *cobra.Command
12-
AppConfigService services.AppConfigService
13-
OsService services.OsService
14-
ConfigsListService services.ConfigsListService
11+
AppConfigService AppConfigService
12+
OsService OsService
13+
ConfigsListService ConfigsListService
1514
}
1615

1716
func (c *OpenCmd) run(cmd *cobra.Command, args []string) error {
@@ -33,9 +32,9 @@ func (c *OpenCmd) run(cmd *cobra.Command, args []string) error {
3332
return c.OsService.OpenInEditor(*editor, path)
3433
}
3534

36-
func NewOpenCmd(AppConfig services.AppConfigService,
37-
ConfigsListService services.ConfigsListService,
38-
OsService services.OsService,
35+
func NewOpenCmd(AppConfig AppConfigService,
36+
ConfigsListService ConfigsListService,
37+
OsService OsService,
3938
) OpenCmd {
4039
openCmd := OpenCmd{
4140
AppConfigService: AppConfig,
@@ -46,15 +45,15 @@ func NewOpenCmd(AppConfig services.AppConfigService,
4645
cmd := &cobra.Command{
4746
Use: "open [filename]",
4847
Short: "Opens a config by name",
49-
Long: `The 'open' command launches the default text editor specified in the application configuration
48+
Long: `The 'open' command launches the default text editor specified in the application configuration
5049
to open the configuration file associated with the provided key.
5150
5251
This is useful for quickly editing or viewing the content of config files without manually locating them.
5352
5453
Usage example:
5554
cm open myconfig
5655
57-
This command will open the file linked to 'myconfig' in the configured editor,
56+
This command will open the file linked to 'myconfig' in the configured editor,
5857
you can configure it in app config by running 'cm open cm_config'`,
5958
RunE: openCmd.run,
6059
}

cmd/path.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/hurtki/configsManager/services"
5+
66
"github.com/spf13/cobra"
77
)
88

99
type PathCmd struct {
1010
Command *cobra.Command
11-
AppConfigService services.AppConfigService
12-
ConfigsListService services.ConfigsListService
11+
ConfigsListService ConfigsListService
1312
}
1413

1514
func (c *PathCmd) run(cmd *cobra.Command, args []string) error {
@@ -29,23 +28,22 @@ func (c *PathCmd) run(cmd *cobra.Command, args []string) error {
2928
return nil
3029
}
3130

32-
func NewPathCmd(AppConfig services.AppConfigService, ConfigsListService services.ConfigsListService) PathCmd {
31+
func NewPathCmd(ConfigsListService ConfigsListService) PathCmd {
3332
pathCmd := PathCmd{
34-
AppConfigService: AppConfig,
3533
ConfigsListService: ConfigsListService,
3634
}
3735

3836
cmd := &cobra.Command{
3937
Use: "path [key]",
4038
Short: "Retrieve the file path associated with a configuration key",
41-
Long: `The 'path' command fetches the absolute file path stored under the given configuration key
39+
Long: `The 'path' command fetches the absolute file path stored under the given configuration key
4240
from the user's saved configuration list.
4341
4442
Usage examples:
4543
cm path myconfig # Prints the file path associated with "myconfig"
4644
cm path cm_config # Prints the default configManager config path
4745
48-
This command helps you quickly locate configuration files by their keys, making management easier
46+
This command helps you quickly locate configuration files by their keys, making management easier
4947
and more efficient.`,
5048
RunE: pathCmd.run,
5149
}

cmd/rm.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/hurtki/configsManager/services"
65
"github.com/spf13/cobra"
76
)
87

98
type RmCmd struct {
109
Command *cobra.Command
11-
AppConfigService services.AppConfigService
12-
ConfigsListService services.ConfigsListService
10+
AppConfigService AppConfigService
11+
ConfigsListService ConfigsListService
1312
}
1413

1514
func (c *RmCmd) run(cmd *cobra.Command, args []string) error {
@@ -30,7 +29,7 @@ func (c *RmCmd) run(cmd *cobra.Command, args []string) error {
3029

3130
}
3231

33-
func NewRmCmd(AppConfig services.AppConfigService, ConfigsListService services.ConfigsListService) RmCmd {
32+
func NewRmCmd(AppConfig AppConfigService, ConfigsListService ConfigsListService) RmCmd {
3433
rmCmd := RmCmd{
3534
AppConfigService: AppConfig,
3635
ConfigsListService: ConfigsListService,

cmd/root.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package cmd
22

33
import (
4-
"github.com/hurtki/configsManager/services"
5-
syncServices "github.com/hurtki/configsManager/services/sync"
4+
sync_cmd "github.com/hurtki/configsManager/cmd/sync"
65
"github.com/spf13/cobra"
76
)
87

98
// rootCmd represents the base command when called without any subcommands
109
var rootCmd = &cobra.Command{
1110
Use: "cm",
1211
Short: "A CLI tool to manage configuration file paths by keys",
13-
Long: `ConfigManager is a simple and efficient CLI application that helps you
12+
Long: `ConfigManager is a simple and efficient CLI application that helps you
1413
manage your configuration files by associating keys with file paths.
1514
1615
With configManager, you can:
@@ -43,18 +42,18 @@ func init() {
4342
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4443
}
4544

46-
func NewRootCmd(AppConfigService services.AppConfigService,
47-
InputService services.InputService,
48-
ConfigsListService services.ConfigsListService,
49-
OsService services.OsService,
50-
SyncService syncServices.SyncService,
45+
func NewRootCmd(AppConfigService AppConfigService,
46+
InputService InputService,
47+
ConfigsListService ConfigsListService,
48+
OsService OsService,
49+
SyncService sync_cmd.SyncService,
5150
) *cobra.Command {
5251
// creating commands with dependencies
5352
addCmd := NewAddCmd(AppConfigService, InputService, ConfigsListService, OsService)
5453
catCmd := NewCatCmd(AppConfigService, ConfigsListService, OsService)
5554
keysCmd := NewKeysCmd(AppConfigService, ConfigsListService)
5655
openCmd := NewOpenCmd(AppConfigService, ConfigsListService, OsService)
57-
pathCmd := NewPathCmd(AppConfigService, ConfigsListService)
56+
pathCmd := NewPathCmd(ConfigsListService)
5857
rmCmd := NewRmCmd(AppConfigService, ConfigsListService)
5958
initCmd := NewInitCmd(AppConfigService, ConfigsListService)
6059
syncCmd := NewSyncCmd(AppConfigService, ConfigsListService, OsService, SyncService)

0 commit comments

Comments
 (0)