dotfile-linker.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* dotfile-linker.go
  2. a simple linker which links up dotfiles will create symlinks of files in
  3. scripts/ directory to $HOME/bin and will create directories and symlinks
  4. from dotfiles/ directory to $HOME */
  5. package main
  6. import (
  7. "fmt"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. func unique(elements []string) []string {
  13. encountered := map[string]bool{}
  14. for v := range elements {
  15. encountered[elements[v]] = true
  16. }
  17. result := []string{}
  18. for key := range encountered {
  19. result = append(result, key)
  20. }
  21. return result
  22. }
  23. func walker(path string) ([]string, []string) {
  24. var files, fullpaths []string
  25. err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
  26. if info.IsDir() {
  27. return nil
  28. }
  29. fullpaths = append(fullpaths, path)
  30. files = append(files, info.Name())
  31. return nil
  32. })
  33. if err != nil {
  34. panic(err)
  35. }
  36. return files, fullpaths
  37. }
  38. func dotfile_gen(dotfile_path string) ([]string, []string) {
  39. var dotfile_relpaths, dotfile_reldirs []string
  40. dotfiles, dotfiles_paths := walker(dotfile_path)
  41. for i, dotfile_dir := range dotfiles_paths {
  42. if strings.Contains(dotfile_dir, dotfiles[i]) {
  43. dotfile_relpaths = append(dotfile_relpaths, dotfile_dir[len(dotfile_path)+1:])
  44. dotfile_reldir_split := strings.Split(dotfile_relpaths[i], "/")
  45. dotfile_reldirs = append(dotfile_reldirs, strings.Join(dotfile_reldir_split[:len(dotfile_reldir_split)-1], "/"))
  46. }
  47. }
  48. return dotfile_relpaths, dotfile_reldirs
  49. }
  50. func main() {
  51. dir, _ := os.Getwd()
  52. dotfile_dir := dir + "/dotfiles"
  53. home_dir := os.Getenv("HOME")
  54. script_dir := dir + "/scripts"
  55. script_loc := home_dir + "/bin"
  56. err := os.Mkdir(script_loc, 0700)
  57. if err != nil {
  58. fmt.Printf("Error: %s\n", err)
  59. }
  60. script_files, script_paths := walker(script_dir)
  61. dotfile_files, dotfile_dirs := dotfile_gen(dotfile_dir)
  62. for i := range script_paths {
  63. fmt.Printf("%s -> %s\n", script_paths[i], script_loc+"/"+script_files[i])
  64. err := os.Symlink(script_paths[i], script_loc+"/"+script_files[i])
  65. if err != nil {
  66. fmt.Printf("Error: %s\n", err)
  67. }
  68. }
  69. for _, dotfile_dir := range unique(dotfile_dirs) {
  70. fullpath := home_dir + "/" + dotfile_dir
  71. fmt.Printf("Creating directory %s\n", fullpath)
  72. err := os.MkdirAll(fullpath, 0700)
  73. if err != nil {
  74. fmt.Printf("Error: %s\n", err)
  75. }
  76. }
  77. for _, dotfile := range dotfile_files {
  78. fmt.Printf("%s -> %s\n", dotfile_dir+"/"+dotfile, home_dir+"/"+dotfile)
  79. err := os.Symlink(dotfile_dir+"/"+dotfile, home_dir+"/"+dotfile)
  80. if err != nil {
  81. fmt.Printf("Error: %s\n", err)
  82. }
  83. }
  84. }