Skip to content

Commit

Permalink
Add logic to keep user added files in the new etc
Browse files Browse the repository at this point in the history
Fixes an issue where EtcBuilder would discard any files the user adds in their etc when merging into a new etc
  • Loading branch information
axtloss committed Apr 12, 2024
1 parent c038f3d commit 82dff2a
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"io"
"io/fs"
Expand All @@ -15,6 +16,12 @@ import (
"github.com/spf13/cobra"
)

type NoMatchError struct{}

func (m *NoMatchError) Error() string {
return "Specified files are not the same"
}

func NewBuildCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "build",
Expand Down Expand Up @@ -71,8 +78,10 @@ func clearDirectory(fileList []fs.DirEntry, root string) error {
}

func fileHandler(userFile string, newSysFile string, fileInfo fs.FileInfo, newFileInfo fs.FileInfo, newSys string, oldSys string, newUser string, oldUser string) error {
if fileInfo.IsDir() || newFileInfo.IsDir() || strings.ReplaceAll(userFile, oldUser, "") != strings.ReplaceAll(newSysFile, newSys, "") {
if fileInfo.IsDir() || newFileInfo.IsDir() {
return nil
} else if strings.ReplaceAll(userFile, oldUser, "") != strings.ReplaceAll(newSysFile, newSys, "") {
return &NoMatchError{}
}

if slices.Contains(settings.SpecialFiles, strings.ReplaceAll(newSysFile, strings.TrimRight(newSys, "/")+"/", "")) {
Expand All @@ -95,8 +104,11 @@ func fileHandler(userFile string, newSysFile string, fileInfo fs.FileInfo, newFi
return err
}
destFilePath := newUser + "/" + strings.ReplaceAll(userFile, oldUser, "")
os.Mkdir(strings.TrimRight(destFilePath, fileInfo.Name()), dirInfo.Mode())
copyFile(userFile, destFilePath)
os.MkdirAll(strings.TrimRight(destFilePath, fileInfo.Name()), dirInfo.Mode())
err = copyFile(userFile, destFilePath)
if err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -129,15 +141,38 @@ func buildCommand(_ *cobra.Command, args []string) error {
return err
}

var userPaths []string
err = filepath.Walk(oldUser, func(userPath string, userInfo os.FileInfo, e error) error {
isInSys := false
err := filepath.Walk(newSys, func(newPath string, newInfo os.FileInfo, err error) error {
return fileHandler(userPath, newPath, userInfo, newInfo, newSys, oldSys, newUser, oldUser)
err = fileHandler(userPath, newPath, userInfo, newInfo, newSys, oldSys, newUser, oldUser)
if err == nil {
isInSys = true
} else if errors.Is(err, &NoMatchError{}) {
isInSys = false
return nil
}
return err
})
if isInSys == false {
userPaths = append(userPaths, userPath)
}
return err
})
if err != nil {
return err
}
for _, userFile := range userPaths {
fmt.Printf("Copying user file %s\n", userFile)
fileInfo, err := os.Stat(userFile)
dirInfo, err := os.Stat(strings.TrimRight(userFile, fileInfo.Name()))
if err != nil {
return err
}
destFilePath := newUser + "/" + strings.ReplaceAll(userFile, oldUser, "")
os.MkdirAll(strings.TrimRight(destFilePath, fileInfo.Name()), dirInfo.Mode())
copyFile(userFile, destFilePath)
}
return nil
}

Expand All @@ -157,14 +192,36 @@ func ExtBuildCommand(oldSys string, newSys string, oldUser string, newUser strin
return err
}

var userPaths []string
err = filepath.Walk(oldUser, func(userPath string, userInfo os.FileInfo, e error) error {
isInSys := false
err := filepath.Walk(newSys, func(newPath string, newInfo os.FileInfo, err error) error {
return fileHandler(userPath, newPath, userInfo, newInfo, newSys, oldSys, newUser, oldUser)
err = fileHandler(userPath, newPath, userInfo, newInfo, newSys, oldSys, newUser, oldUser)
if err == nil {
isInSys = true
} else if errors.Is(err, &NoMatchError{}) {
return nil
}
return err
})
if isInSys == false {
userPaths = append(userPaths, userPath)
}
return err
})
if err != nil {
return err
}
for _, userFile := range userPaths {
fmt.Printf("Copying user file %s\n", userFile)
fileInfo, err := os.Stat(userFile)
dirInfo, err := os.Stat(strings.TrimRight(userFile, fileInfo.Name()))
if err != nil {
return err
}
destFilePath := newUser + "/" + strings.ReplaceAll(userFile, oldUser, "")
os.MkdirAll(strings.TrimRight(destFilePath, fileInfo.Name()), dirInfo.Mode())
copyFile(userFile, destFilePath)
}
return nil
}

0 comments on commit 82dff2a

Please sign in to comment.