refactor add to git
@@ -157,44 +157,32 @@ func MainnetFlowPortURL(publicKey string) string {
return fmt.Sprintf("%s%s", FlowPortUrl, strings.TrimPrefix(publicKey, "0x"))
}
-func DirExists(path string) bool {
- info, err := os.Stat(path)
- if os.IsNotExist(err) {
- return false
- }
- return !info.IsDir()
-}
-
type ReaderWriter interface {
ReadFile(source string) ([]byte, error)
WriteFile(filename string, data []byte, perm os.FileMode) error
}
// AddToGitIgnore adds a new line to the .gitignore if one doesn't exist it creates it.
-func AddToGitIgnore(
- filename string,
- loader ReaderWriter,
-) error {
+func AddToGitIgnore(filename string, loader ReaderWriter) error {
currentWd, err := os.Getwd()
if err != nil {
return err
}
gitIgnorePath := path.Join(currentWd, ".gitignore")
-
gitIgnoreFiles := ""
- if PathExists(gitIgnorePath) {
- gitIgnoreFilesRaw, err := loader.ReadFile(gitIgnoreDir)
+
+ fileStat, err := os.Stat(gitIgnorePath)
+ if !os.IsNotExist(err) { // if gitignore exists
+ gitIgnoreFilesRaw, err := loader.ReadFile(gitIgnorePath)
if err != nil {
return err
}
gitIgnoreFiles = string(gitIgnoreFilesRaw)
}
- newFileGitIgnoreByte := []byte(string(gitIgnoreFiles) + "\n" + filename)
- err = loader.WriteFile(gitIgnoreDir, newFileGitIgnoreByte, 0644)
-
- if err != nil {
- return err
- }
- return nil
+ return loader.WriteFile(
+ gitIgnorePath,
+ []byte(fmt.Sprintf("%s\n%s", gitIgnoreFiles, filename)),
+ fileStat.Mode().Perm(),
+ )
}
