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

Commit

Permalink
cmd/plg: allow including hidden files on init
Browse files Browse the repository at this point in the history
Dotfiles might have their original name, with a prepended dot. If that's
the case, they won't be included when the "init" command is run.

In order to include those files, the `-hidden` option can be passed.
Unwanted hidden files must be excluded with `-exclude` though.
  • Loading branch information
gbrlsnchs committed Apr 18, 2020
1 parent 136ae7e commit 71943b1
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 3 deletions.
10 changes: 8 additions & 2 deletions cmd/plg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type initCmd struct {
force bool
include cliutil.CommaSepOptionSet
exclude cliutil.CommaSepOptionSet
hidden bool
}

func (cmd *initCmd) register(getcfg func() appConfig) func(cli.Program) error {
Expand Down Expand Up @@ -59,10 +60,15 @@ func (cmd *initCmd) register(getcfg func() appConfig) func(cli.Program) error {
targets[i] = fi.Name()
}
cmd.exclude.Set(appcfg.conf)
c = config.New(targets,
opts := []func(*config.Config){
config.Include(cmd.include),
config.Exclude(cmd.exclude),
config.MergeWith(c))
config.MergeWith(c),
}
if cmd.hidden {
opts = append(opts, config.IncludeHidden)
}
c = config.New(targets, opts...)
b, err := marshalYAML(c)
if err != nil {
return err
Expand Down
317 changes: 316 additions & 1 deletion cmd/plg/init_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 @@ -501,12 +502,326 @@ func TestInit(t *testing.T) {
err: nil,
remove: true,
},
{
name: "hidden",
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{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: nil,
},
".bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
".git": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File),
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
cmd: initCmd{hidden: 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{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: nil,
},
".bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
".git": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File),
},
"hidden.yml": {
Linkname: "",
Perm: 0o644,
Data: yamlData(config.Config{
Targets: []string{
".bar",
".git",
"foo",
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
err: nil,
remove: true,
},
{
name: "no hidden",
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{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: nil,
},
".bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
".git": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File),
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
cmd: initCmd{},
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{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: nil,
},
".bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
".git": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File),
},
"no_hidden.yml": {
Linkname: "",
Perm: 0o644,
Data: yamlData(config.Config{
Targets: []string{
"foo",
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
err: nil,
remove: true,
},
{
name: "hidden and exclude",
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{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: nil,
},
".bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
".git": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File),
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
cmd: initCmd{
hidden: true,
exclude: cliutil.CommaSepOptionSet{
".git": struct{}{},
},
},
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{
"foo": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("foo"),
Children: nil,
},
".bar": {
Linkname: "",
Perm: os.ModePerm,
Data: []byte("bar"),
Children: nil,
},
".git": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File),
},
"hidden_and_exclude.yml": {
Linkname: "",
Perm: 0o644,
Data: yamlData(config.Config{
Targets: []string{
".bar",
"foo",
},
}),
Children: nil,
},
},
},
"config": {
Linkname: "",
Perm: os.ModePerm,
Data: nil,
Children: make(map[string]fstest.File, 0),
},
},
},
},
},
err: nil,
remove: true,
},
}
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,
getwd: func() (string, error) { return fstest.AbsPath("home", "dotfiles"), nil },
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/plg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ func run() int {
},
Recipient: &root.init.exclude,
},
"hidden": cli.BoolOption{
OptionDetails: cli.OptionDetails{
Description: "Include hidden files.",
Short: 'H',
},
Recipient: &root.init.hidden,
},
},
Exec: root.init.register(appcfg.copy),
},
Expand Down
Loading

0 comments on commit 71943b1

Please sign in to comment.