Skip to content

Commit

Permalink
Merge pull request #7 from sigmonsays/automode
Browse files Browse the repository at this point in the history
Automode
  • Loading branch information
sigmonsays authored Nov 26, 2022
2 parents 6aa11ca + b9b2f00 commit 4b2c35b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ If no configuration file is given, the default is assumed to be

# Configuration

A configuration file is optional if automode is used.

dotbot -c is used to provide what configuration to run. Multiple configuration
files can be specified by passing -c with a file multiple times.

Expand All @@ -49,6 +51,17 @@ Sample configuration
~/.bash_profile: .bash_profile
~/.bashrc: .bashrc

# AutoMode

Auto mode is enabled by passing `--auto` or `-a` to `dotbot link`

With automode, no configuration file is required, instead a configuration is automatically generated at runtime. The current directory is used as a file list.

The `.git` file name is ignored.

automode works with pretend mode

dotbot link -a -p

# Usage

Expand Down
4 changes: 4 additions & 0 deletions cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (me *Cleanup) RunFile(path string) error {
if log.IsTrace() {
cfg.PrintConfig()
}
return me.RunConfig(cfg)
}

func (me *Cleanup) RunConfig(cfg *AppConfig) error {

run, err := CompileRun(cfg.Symlinks)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (me *AppConfig) PrintConfig() {
}

func GetDefaultConfig() *AppConfig {
return &AppConfig{}
cfg := &AppConfig{}
cfg.Symlinks = make(map[string]string, 0)
return cfg
}

func (c *AppConfig) LoadDefault() {
Expand Down
70 changes: 65 additions & 5 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@ func (me *Link) Flags() []cli.Flag {
Usage: "pretend mode",
Aliases: []string{"p"},
},
&cli.BoolFlag{
Name: "auto",
Usage: "auto mode",
Aliases: []string{"a"},
},
}
return nil
}

type LinkOptions struct {
Pretend bool
Pretend bool
AutoMode bool
}

func (me *Link) Run(c *cli.Context) error {
opts := &LinkOptions{}
configfiles := getConfigFiles(c)
opts.Pretend = c.Bool("pretend")
opts.AutoMode = c.Bool("auto")
log.Tracef("%d files to execute", len(configfiles))

if len(configfiles) == 0 {
if len(configfiles) == 0 && opts.AutoMode == false {
log.Warnf("Nothing to do, try passing -c dotbot.yaml ")
return nil
}
Expand All @@ -50,6 +57,13 @@ func (me *Link) Run(c *cli.Context) error {
}
}

if opts.AutoMode {
err := me.RunAutoMode(opts)
if err != nil {
log.Warnf("RunAutoMode: %s", err)
}
}

return nil
}

Expand All @@ -64,6 +78,10 @@ func (me *Link) RunFile(opts *LinkOptions, path string) error {
cfg.PrintConfig()
}

return me.RunConfig(opts, cfg)
}

func (me *Link) RunConfig(opts *LinkOptions, cfg *AppConfig) error {
run, err := CompileRun(cfg.Symlinks)
if err != nil {
return err
Expand All @@ -74,7 +92,17 @@ func (me *Link) RunFile(opts *LinkOptions, path string) error {
return err
}

created := 0
err = CreateLinks(opts, run)
if err != nil {
return err
}
return nil
}
func CreateLinks(opts *LinkOptions, run *Run) error {
var (
err error
created int
)

for _, li := range run.Links {
if opts.Pretend {
Expand Down Expand Up @@ -102,9 +130,7 @@ func (me *Link) RunFile(opts *LinkOptions, path string) error {
if created > 0 {
log.Infof("created %d links", created)
}

return nil

}

func Mkdirs(homedir string, paths []string) error {
Expand All @@ -126,3 +152,37 @@ func Mkdirs(homedir string, paths []string) error {
}
return nil
}

func (me *Link) RunAutoMode(opts *LinkOptions) error {
cfg := GetDefaultConfig()
cwd, err := os.Getwd()
if err != nil {
return err
}

filenames, err := ListDir(cwd)
if err != nil {
return err
}


// build config using current directory listing
for _, filename := range filenames {
if filename == ".git" {
continue
}
cfg.Symlinks["~/"+filename] = filename
}

run, err := CompileRun(cfg.Symlinks)
if err != nil {
return err
}

err = CreateLinks(opts, run)
if err != nil {
return err
}

return nil
}

0 comments on commit 4b2c35b

Please sign in to comment.