diff --git a/cmd/add.go b/cmd/add.go index b2f30d3..675ba8a 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -19,12 +19,7 @@ func newAddCmd() *cobra.Command { filePath := args[0] host, _ := cmd.Flags().GetString("host") - var lnk *core.Lnk - if host != "" { - lnk = core.NewLnkWithHost(host) - } else { - lnk = core.NewLnk() - } + lnk := core.NewLnk(core.WithHost(host)) if err := lnk.Add(filePath); err != nil { return fmt.Errorf("failed to add file: %w", err) diff --git a/cmd/list.go b/cmd/list.go index 5c58898..890c091 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -68,7 +68,7 @@ func listCommonConfig(cmd *cobra.Command) error { } func listHostConfig(cmd *cobra.Command, host string) error { - lnk := core.NewLnkWithHost(host) + lnk := core.NewLnk(core.WithHost(host)) managedItems, err := lnk.List() if err != nil { return fmt.Errorf("failed to list managed items for host %s: %w", host, err) @@ -127,7 +127,7 @@ func listAllConfigs(cmd *cobra.Command) error { for _, host := range hosts { printf(cmd, "\n🖥️ \033[1mHost: %s\033[0m", host) - hostLnk := core.NewLnkWithHost(host) + hostLnk := core.NewLnk(core.WithHost(host)) hostItems, err := hostLnk.List() if err != nil { printf(cmd, " \033[31m(error: %v)\033[0m\n", err) diff --git a/cmd/pull.go b/cmd/pull.go index f5cdc9c..c3a8bdc 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -16,12 +16,7 @@ func newPullCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { host, _ := cmd.Flags().GetString("host") - var lnk *core.Lnk - if host != "" { - lnk = core.NewLnkWithHost(host) - } else { - lnk = core.NewLnk() - } + lnk := core.NewLnk(core.WithHost(host)) restored, err := lnk.Pull() if err != nil { diff --git a/cmd/rm.go b/cmd/rm.go index 4473f42..9c030c5 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -19,12 +19,7 @@ func newRemoveCmd() *cobra.Command { filePath := args[0] host, _ := cmd.Flags().GetString("host") - var lnk *core.Lnk - if host != "" { - lnk = core.NewLnkWithHost(host) - } else { - lnk = core.NewLnk() - } + lnk := core.NewLnk(core.WithHost(host)) if err := lnk.Remove(filePath); err != nil { return fmt.Errorf("failed to remove file: %w", err) diff --git a/internal/core/lnk.go b/internal/core/lnk.go index bc49efa..75cdddf 100644 --- a/internal/core/lnk.go +++ b/internal/core/lnk.go @@ -19,26 +19,30 @@ type Lnk struct { fs *fs.FileSystem } -// NewLnk creates a new Lnk instance for common configuration -func NewLnk() *Lnk { - repoPath := getRepoPath() - return &Lnk{ - repoPath: repoPath, - host: "", // Empty host means common configuration - git: git.New(repoPath), - fs: fs.New(), +type Option func(*Lnk) + +// WithHost sets the host for host-specific configuration +func WithHost(host string) Option { + return func(l *Lnk) { + l.host = host } } -// NewLnkWithHost creates a new Lnk instance for host-specific configuration -func NewLnkWithHost(host string) *Lnk { +// NewLnk creates a new Lnk instance with optional configuration +func NewLnk(opts ...Option) *Lnk { repoPath := getRepoPath() - return &Lnk{ + lnk := &Lnk{ repoPath: repoPath, - host: host, + host: "", git: git.New(repoPath), fs: fs.New(), } + + for _, opt := range opts { + opt(lnk) + } + + return lnk } // GetCurrentHostname returns the current system hostname diff --git a/internal/core/lnk_test.go b/internal/core/lnk_test.go index 1b3e1fa..9798982 100644 --- a/internal/core/lnk_test.go +++ b/internal/core/lnk_test.go @@ -592,7 +592,7 @@ func (suite *CoreTestSuite) TestMultihostFileOperations() { suite.Require().NoError(err) // Add file to host-specific configuration - hostLnk := NewLnkWithHost("workstation") + hostLnk := NewLnk(WithHost("workstation")) err = hostLnk.Add(testFile2) suite.Require().NoError(err) @@ -661,7 +661,7 @@ func (suite *CoreTestSuite) TestMultihostSymlinkRestoration() { suite.Require().NoError(err) // Create files directly in host-specific storage (simulating a pull) - hostLnk := NewLnkWithHost("testhost") + hostLnk := NewLnk(WithHost("testhost")) // Ensure host storage directory exists hostStoragePath := hostLnk.getHostStoragePath() @@ -729,7 +729,7 @@ func (suite *CoreTestSuite) TestMultihostIsolation() { suite.Require().NoError(err) // Add to host-specific - hostLnk := NewLnkWithHost("work") + hostLnk := NewLnk(WithHost("work")) err = hostLnk.Add(testFile) suite.Require().NoError(err)