mirror of
https://github.com/yarlson/lnk.git
synced 2025-08-30 17:59:47 +02:00
refactor(core): simplify Lnk creation with functional options pattern
This commit is contained in:
@@ -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)
|
||||
|
@@ -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)
|
||||
|
@@ -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 {
|
||||
|
@@ -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)
|
||||
|
@@ -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