Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
cmd/plg: add option to flatten target directory
Browse files Browse the repository at this point in the history
It is common to have a parent directory just to group a bunch of
dotfiles from the same program. For example:

  + zsh
        +-- .zprofile
        +-- .zshrc

In this case, both files need to be symlinked in the home directory, so
we need to flatten the `zsh` directory. That's exactly what this option
does by reading all files in the parent directory.

Also, since it reads the directory, it'll override the `-targets`
option, rendering it useless. It might be soon deprecated, since listing
targets by hand is painful.
  • Loading branch information
gbrlsnchs committed Apr 19, 2020
1 parent de52b0c commit 9939f6a
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 4 deletions.
19 changes: 16 additions & 3 deletions cmd/plg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type configCmd struct {
link strptr
targets cliutil.CommaSepOptionList
useHome boolptr
flatten bool
}

func (cmd *configCmd) register(getcfg func() appConfig) func(cli.Program) error {
Expand All @@ -26,16 +27,28 @@ func (cmd *configCmd) register(getcfg func() appConfig) func(cli.Program) error
if err != nil {
return err
}
if cmd.flatten {
files, err := fs.ReadDir(cmd.file)
if err != nil {
return err
}
cmd.targets = make([]string, len(files))
for i, f := range files {
cmd.targets[i] = f.Name()
}
s := ""
cmd.link.addr = &s // TODO: make link string, not pointer
}
var c config.Config
if err := yaml.Unmarshal(b, &c); err != nil {
return err
}
c.Set(cmd.file, config.Config{
cc := config.Config{
BaseDir: cmd.baseDir,
Link: cmd.link.addr,
Targets: cmd.targets,
UseHome: cmd.useHome.addr,
})
}
c.Set(cmd.file, config.New(cmd.targets, config.MergeWith(cc)))
if b, err = marshalYAML(c); err != nil {
return err
}
Expand Down
259 changes: 258 additions & 1 deletion cmd/plg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"os"
"path/filepath"
"testing"

"github.com/gbrlsnchs/cli/clitest"
Expand Down Expand Up @@ -268,12 +269,268 @@ func TestConfig(t *testing.T) {
},
err: nil,
},
{
name: "flatten",
drv: fstest.InMemoryDriver{
CurrentDir: "home/dotfiles",
Files: map[string]fstest.File{
"home": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"dotfiles": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"test": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: map[string]fstest.File{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
"bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
},
},
"flatten.yml": {
Linkname: "",
Perm: os.ModePerm,
Data: yamlData(config.Config{
BaseDir: "test",
Targets: []string{
"test",
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
cmd: configCmd{
file: "test",
flatten: true,
},
want: fstest.InMemoryDriver{
CurrentDir: "home/dotfiles",
Files: map[string]fstest.File{
"home": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"dotfiles": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"test": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: map[string]fstest.File{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
"bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
},
},
"flatten.yml": {
Linkname: "",
Perm: os.ModePerm,
Data: yamlData(config.Config{
BaseDir: "test",
Targets: []string{
"test",
},
Options: map[string]config.Config{
"test": {
Link: newString(""),
Targets: []string{
"bar",
"foo",
},
},
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
err: nil,
},
{
name: "flatten to home",
drv: fstest.InMemoryDriver{
CurrentDir: "home/dotfiles",
Files: map[string]fstest.File{
"home": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"dotfiles": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"test": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: map[string]fstest.File{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
"bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
},
},
"flatten_to_home.yml": {
Linkname: "",
Perm: os.ModePerm,
Data: yamlData(config.Config{
BaseDir: "test",
Targets: []string{
"test",
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
cmd: configCmd{
file: "test",
useHome: boolptr{addr: newBool(true)},
flatten: true,
},
want: fstest.InMemoryDriver{
CurrentDir: "home/dotfiles",
Files: map[string]fstest.File{
"home": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"dotfiles": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: map[string]fstest.File{
"test": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: map[string]fstest.File{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
"bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
},
},
"flatten_to_home.yml": {
Linkname: "",
Perm: os.ModePerm,
Data: yamlData(config.Config{
BaseDir: "test",
Targets: []string{
"test",
},
Options: map[string]config.Config{
"test": {
Link: newString(""),
Targets: []string{
"bar",
"foo",
},
UseHome: newBool(true),
},
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
err: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var (
appcfg = appConfig{
conf: tc.name + ".yml",
conf: filepath.Base(t.Name()) + ".yml",
fs: &tc.drv,
}
exec = tc.cmd.register(appcfg.copy)
Expand Down
6 changes: 6 additions & 0 deletions cmd/plg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ func run() int {
},
Recipient: &root.config.useHome,
},
"flatten": cli.BoolOption{
OptionDetails: cli.OptionDetails{
Description: "Flatten a directory and promote its children up.",
},
Recipient: &root.config.flatten,
},
},
Arg: cli.StringArg{
Label: "TARGET",
Expand Down
2 changes: 2 additions & 0 deletions cmd/plg/testdata/TestCLI/darwin/config.ct
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ USAGE:

OPTIONS:
-basedir <DIR> Set the file's base directory.
-flatten Flatten a directory and promote its children up.
-h, -help Print this help message.
-home Use home directory as base directory.
-link <NAME> Set the file's link name. An empty string skips the file.
Expand All @@ -67,6 +68,7 @@ USAGE:

OPTIONS:
-basedir <DIR> Set the file's base directory.
-flatten Flatten a directory and promote its children up.
-h, -help Print this help message.
-home Use home directory as base directory.
-link <NAME> Set the file's link name. An empty string skips the file.
Expand Down
2 changes: 2 additions & 0 deletions cmd/plg/testdata/TestCLI/linux/config.ct
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ USAGE:

OPTIONS:
-basedir <DIR> Set the file's base directory.
-flatten Flatten a directory and promote its children up.
-h, -help Print this help message.
-home Use home directory as base directory.
-link <NAME> Set the file's link name. An empty string skips the file.
Expand All @@ -67,6 +68,7 @@ USAGE:

OPTIONS:
-basedir <DIR> Set the file's base directory.
-flatten Flatten a directory and promote its children up.
-h, -help Print this help message.
-home Use home directory as base directory.
-link <NAME> Set the file's link name. An empty string skips the file.
Expand Down
2 changes: 2 additions & 0 deletions cmd/plg/testdata/TestCLI/windows/config.ct
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ USAGE:

OPTIONS:
-basedir <DIR> Set the file's base directory.
-flatten Flatten a directory and promote its children up.
-h, -help Print this help message.
-home Use home directory as base directory.
-link <NAME> Set the file's link name. An empty string skips the file.
Expand All @@ -67,6 +68,7 @@ USAGE:

OPTIONS:
-basedir <DIR> Set the file's base directory.
-flatten Flatten a directory and promote its children up.
-h, -help Print this help message.
-home Use home directory as base directory.
-link <NAME> Set the file's link name. An empty string skips the file.
Expand Down

0 comments on commit 9939f6a

Please sign in to comment.