Skip to content

Commit

Permalink
feat: detect all dotfiles in homedir
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Sep 16, 2020
1 parent c302812 commit f57900b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"

"github.com/spf13/cobra"

"github.com/doron-cohen/antidot/internal/dirs"
"github.com/doron-cohen/antidot/internal/dotfile"
)

func init() {
Expand All @@ -15,5 +18,8 @@ var cleanCmd = &cobra.Command{
Short: "Clean up dotfiles from your $HOME",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Cleaning up!")
var userHomeDir = dirs.GetHomeDir()
var dotfiles = dotfile.Detect(userHomeDir)
fmt.Println(dotfiles)
},
}
12 changes: 12 additions & 0 deletions internal/dirs/homedir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dirs

import "os/user"

func GetHomeDir() string {
user, err := user.Current()
if err != nil {
panic(err)
}

return user.HomeDir
}
38 changes: 38 additions & 0 deletions internal/dotfile/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dotfile

import (
"io/ioutil"
"log"
"strings"
)

type Dotfile struct {
name string
isDir bool
}

func isDotfile(filename string) bool {
return filename != "." && strings.HasPrefix(filename, ".")
}

func Detect(dir string) []Dotfile {
// TODO: better handle file errors
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatalln(err)
}

found := make([]Dotfile, 0, len(files))
for _, fileInfo := range files {
filename := fileInfo.Name()
if isDotfile(filename) {
dotfile := Dotfile{
name: filename,
isDir: fileInfo.IsDir(),
}
found = append(found, dotfile)
}
}

return found
}

0 comments on commit f57900b

Please sign in to comment.