mirror of
https://github.com/yarlson/lnk.git
synced 2025-09-02 18:12:33 +02:00
refactor(core): simplify Lnk creation with functional options pattern
This commit is contained in:
@@ -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
|
||||
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user