Skip to content

Commit

Permalink
some more refactoring and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gszr committed Jul 20, 2023
1 parent 572c6e7 commit 1ec1155
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 57 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ dot files manager.
Let's start with an example:

```
files:
map:
i3:
imwheelrc:
config/alacritty:
config/alacritty.yml:
config/redshift.conf:
```

Expand Down Expand Up @@ -48,10 +48,29 @@ map:
os: linux
imwheelrc:
os: linux
config/alacritty:
config/alacritty.yml:
os: macos
docker/config.json:
as: copy
opt:
cd: dots/
```

In this example, all files live under a subdirectory `dots/`.
```
$ tree .
.
├── dots
│   ├── config
│   │   └── alacritty.yml
│   ├── docker
│   │   └── config.json
│   ├── i3
│   ├── imwheelrc
│   ├── xinitrc
│   └── Xresources
└── dot.yml
```

## Features
Expand Down
17 changes: 7 additions & 10 deletions fixtures/01-dots-basic.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
files:
fixtures/gitconfig:
to: out/gitconfig
os: all # all, linux, macos
fixtures/zshrc:
to: out/zshrc
as: copy
os: linux
opt: # copy opts NYI
# basic mapping

# handle directories
map:
gitconfig:
zshrc:

opt:
cd: fixtures/
18 changes: 7 additions & 11 deletions fixtures/02-dots-basic.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
files:
# infer destination (goes to $HOME)
# uses prefix

map:
gitconfig:
dst: out/gitconfig
os: all # all, linux, macos
zshrc:
dst: out/zshrc
typ: copy
os: linux
opts: # copy opts NYI
files_opts:
dir: fixtures/

# handle directories
to: ~/.zshrc
opt:
cd: fixtures/
13 changes: 13 additions & 0 deletions fixtures/03-dots-os-as.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# to, os, as

map:
gitconfig:
to: out/gitconfig
os: all # all, linux, macos
zshrc:
to: out/zshrc
as: copy
os: linux

opt:
cd: fixtures/
13 changes: 0 additions & 13 deletions fixtures/dots.yml

This file was deleted.

61 changes: 41 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"log"
"os"
"os/user"
"strings"
"fmt"
"path"
Expand All @@ -27,35 +26,24 @@ type Opts struct {
}

type Dots struct {
Opts Opts `yaml:"opts"`
Opts Opts `yaml:"opt"`
FileMappings []FileMapping `yaml:"map"`
}

func (d *Dots) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tmpDots struct {
Opts Opts `yaml:"files_opts"`
Opts Opts `yaml:"opt"`
Mappings map[string]FileMapping `yaml:"map"`
}
err := unmarshal(&tmpDots)
if err != nil {
return err
}
d.Opts.Cd = tmpDots.Opts.Cd
d.Opts = tmpDots.Opts
for file, mapping := range tmpDots.Mappings {
mapping.From = file
mapping.To = expandTilde(mapping.To)
if len(mapping.To) == 0 {
mapping.To = inferDestination(mapping.From)
}
if len(d.Opts.Cd) > 0 {
mapping.From = path.Join(d.Opts.Cd, file)
}
if len(mapping.As) == 0 {
mapping.As = "link"
}
d.FileMappings = append(d.FileMappings, mapping)
}

return nil
}

Expand Down Expand Up @@ -101,7 +89,7 @@ func isDirectory(path string) bool {
return fileInfo.IsDir()
}

func validateDots(dots *Dots) (bool, []error) {
func validateDots(dots Dots) (bool, []error) {
var errs []error
for _, mapping := range dots.FileMappings {
if ! pathExists(mapping.From) {
Expand All @@ -113,10 +101,40 @@ func validateDots(dots *Dots) (bool, []error) {
return len(errs) == 0, errs
}

func transformDots(dots Dots) Dots {
opts := dots.Opts
mappings := dots.FileMappings

var newDots Dots
newDots.Opts = opts

for _, mapping := range mappings {
if len(mapping.To) > 0 {
// expand destination ~
mapping.To = expandTilde(mapping.To)
} else {
// infer destination based on From
mapping.To = inferDestination(mapping.From)
}
if len(opts.Cd) > 0 {
// Cd set: add prefix to From
mapping.From = path.Join(opts.Cd, mapping.From)
}
// default As to symlink
if len(mapping.As) == 0 {
mapping.As = "link"
}

newDots.FileMappings = append(newDots.FileMappings, mapping)
}

return newDots
}

func getHomeDir() string {
usr, _ := user.Current()
return usr.HomeDir
return os.Getenv("HOME")
}

func expandTilde(path string) string {
if strings.HasPrefix(path, "~") {
homeDir := getHomeDir()
Expand All @@ -134,14 +152,17 @@ func readDotFile() Dots {
if err := yaml.Unmarshal([]byte(rcFileData), &dots); err != nil {
logger.Fatalf("cannot decode data: %v", err)
}
valid, errs := validateDots(&dots)

newDots := transformDots(dots)
valid, errs := validateDots(newDots)
if ! valid {
for _, err := range errs {
logger.Printf("failed validating dots file: %v", err)
}
os.Exit(1)
}
return dots

return newDots
}

func doLink(file string, dst string) {
Expand Down

0 comments on commit 1ec1155

Please sign in to comment.