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,12 +19,7 @@ func newAddCmd() *cobra.Command {
|
|||||||
filePath := args[0]
|
filePath := args[0]
|
||||||
host, _ := cmd.Flags().GetString("host")
|
host, _ := cmd.Flags().GetString("host")
|
||||||
|
|
||||||
var lnk *core.Lnk
|
lnk := core.NewLnk(core.WithHost(host))
|
||||||
if host != "" {
|
|
||||||
lnk = core.NewLnkWithHost(host)
|
|
||||||
} else {
|
|
||||||
lnk = core.NewLnk()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := lnk.Add(filePath); err != nil {
|
if err := lnk.Add(filePath); err != nil {
|
||||||
return fmt.Errorf("failed to add file: %w", err)
|
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 {
|
func listHostConfig(cmd *cobra.Command, host string) error {
|
||||||
lnk := core.NewLnkWithHost(host)
|
lnk := core.NewLnk(core.WithHost(host))
|
||||||
managedItems, err := lnk.List()
|
managedItems, err := lnk.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to list managed items for host %s: %w", host, err)
|
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 {
|
for _, host := range hosts {
|
||||||
printf(cmd, "\n🖥️ \033[1mHost: %s\033[0m", host)
|
printf(cmd, "\n🖥️ \033[1mHost: %s\033[0m", host)
|
||||||
|
|
||||||
hostLnk := core.NewLnkWithHost(host)
|
hostLnk := core.NewLnk(core.WithHost(host))
|
||||||
hostItems, err := hostLnk.List()
|
hostItems, err := hostLnk.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printf(cmd, " \033[31m(error: %v)\033[0m\n", err)
|
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 {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
host, _ := cmd.Flags().GetString("host")
|
host, _ := cmd.Flags().GetString("host")
|
||||||
|
|
||||||
var lnk *core.Lnk
|
lnk := core.NewLnk(core.WithHost(host))
|
||||||
if host != "" {
|
|
||||||
lnk = core.NewLnkWithHost(host)
|
|
||||||
} else {
|
|
||||||
lnk = core.NewLnk()
|
|
||||||
}
|
|
||||||
|
|
||||||
restored, err := lnk.Pull()
|
restored, err := lnk.Pull()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -19,12 +19,7 @@ func newRemoveCmd() *cobra.Command {
|
|||||||
filePath := args[0]
|
filePath := args[0]
|
||||||
host, _ := cmd.Flags().GetString("host")
|
host, _ := cmd.Flags().GetString("host")
|
||||||
|
|
||||||
var lnk *core.Lnk
|
lnk := core.NewLnk(core.WithHost(host))
|
||||||
if host != "" {
|
|
||||||
lnk = core.NewLnkWithHost(host)
|
|
||||||
} else {
|
|
||||||
lnk = core.NewLnk()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := lnk.Remove(filePath); err != nil {
|
if err := lnk.Remove(filePath); err != nil {
|
||||||
return fmt.Errorf("failed to remove file: %w", err)
|
return fmt.Errorf("failed to remove file: %w", err)
|
||||||
|
@@ -19,26 +19,30 @@ type Lnk struct {
|
|||||||
fs *fs.FileSystem
|
fs *fs.FileSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLnk creates a new Lnk instance for common configuration
|
type Option func(*Lnk)
|
||||||
func NewLnk() *Lnk {
|
|
||||||
repoPath := getRepoPath()
|
// WithHost sets the host for host-specific configuration
|
||||||
return &Lnk{
|
func WithHost(host string) Option {
|
||||||
repoPath: repoPath,
|
return func(l *Lnk) {
|
||||||
host: "", // Empty host means common configuration
|
l.host = host
|
||||||
git: git.New(repoPath),
|
|
||||||
fs: fs.New(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLnkWithHost creates a new Lnk instance for host-specific configuration
|
// NewLnk creates a new Lnk instance with optional configuration
|
||||||
func NewLnkWithHost(host string) *Lnk {
|
func NewLnk(opts ...Option) *Lnk {
|
||||||
repoPath := getRepoPath()
|
repoPath := getRepoPath()
|
||||||
return &Lnk{
|
lnk := &Lnk{
|
||||||
repoPath: repoPath,
|
repoPath: repoPath,
|
||||||
host: host,
|
host: "",
|
||||||
git: git.New(repoPath),
|
git: git.New(repoPath),
|
||||||
fs: fs.New(),
|
fs: fs.New(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(lnk)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lnk
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCurrentHostname returns the current system hostname
|
// GetCurrentHostname returns the current system hostname
|
||||||
|
@@ -592,7 +592,7 @@ func (suite *CoreTestSuite) TestMultihostFileOperations() {
|
|||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
// Add file to host-specific configuration
|
// Add file to host-specific configuration
|
||||||
hostLnk := NewLnkWithHost("workstation")
|
hostLnk := NewLnk(WithHost("workstation"))
|
||||||
err = hostLnk.Add(testFile2)
|
err = hostLnk.Add(testFile2)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
@@ -661,7 +661,7 @@ func (suite *CoreTestSuite) TestMultihostSymlinkRestoration() {
|
|||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
// Create files directly in host-specific storage (simulating a pull)
|
// Create files directly in host-specific storage (simulating a pull)
|
||||||
hostLnk := NewLnkWithHost("testhost")
|
hostLnk := NewLnk(WithHost("testhost"))
|
||||||
|
|
||||||
// Ensure host storage directory exists
|
// Ensure host storage directory exists
|
||||||
hostStoragePath := hostLnk.getHostStoragePath()
|
hostStoragePath := hostLnk.getHostStoragePath()
|
||||||
@@ -729,7 +729,7 @@ func (suite *CoreTestSuite) TestMultihostIsolation() {
|
|||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
// Add to host-specific
|
// Add to host-specific
|
||||||
hostLnk := NewLnkWithHost("work")
|
hostLnk := NewLnk(WithHost("work"))
|
||||||
err = hostLnk.Add(testFile)
|
err = hostLnk.Add(testFile)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user