Skip to content

Commit

Permalink
Initial commit for 'dotbro init'
Browse files Browse the repository at this point in the history
  • Loading branch information
flowchartsman committed Nov 6, 2018
1 parent beb72ef commit ba71fe9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
6 changes: 4 additions & 2 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type Configuration struct {
Directories Directories
Mapping map[string]string
Init map[string][]string
Files Files
Filepath string
}
Expand All @@ -42,19 +43,20 @@ func NewConfiguration(filename string) (conf *Configuration, err error) {
case ".json":
conf, err = fromJSON(filename)
default:
err = fmt.Errorf("Cannot read config file %s : unknown extension. Supported: conf, toml.", filename)
err = fmt.Errorf("Cannot read config file %s : unknown extension. Supported: json, toml.", filename)

This comment has been minimized.

Copy link
@flowchartsman

flowchartsman Nov 6, 2018

Author Owner

unrelated bug fixed here: doesn't actually support .conf

}

if err != nil {
return nil, err
}

conf.Filepath = filename

This comment has been minimized.

Copy link
@flowchartsman

flowchartsman Nov 6, 2018

Author Owner

unrelated bug fixed here processConf needs path, but doesn't have it


conf, err = processConf(conf)
if err != nil {
return nil, err
}

conf.Filepath = filename
return conf, nil
}

Expand Down
6 changes: 5 additions & 1 deletion docopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ func ParseArguments(argv []string) (map[string]interface{}, error) {
usage := `dotbro - simple yet effective dotfiles manager.
Usage:
dotbro [options] [--config=<filepath>]
dotbro [options]
dotbro init [options]
dotbro add [options] <filename>
dotbro -h | --help
dotbro --version
Common options:
-c --config=<filepath> Dotbro's configuration file in JSON or TOML format.
[default: dotbro.toml]

This comment has been minimized.

Copy link
@flowchartsman

flowchartsman Nov 6, 2018

Author Owner

should have a default. makes checking in everything easier. indenting looks off in github view. will fix with spaces.

This comment has been minimized.

Copy link
@hypnoglow

hypnoglow Nov 6, 2018

Default would be nice for initial run. But we still need to honor rc file, see: https://github.com/flowchartsman/dotbro/blob/ba71fe9011/main.go#L256

-q --quiet Quiet mode. Do not print any output, except warnings
and errors.
-d --dry Dry mode. Do not take any action, instead print what
WOULD happen (suppresses --quiet).
-v --verbose Verbose mode. Detailed output.
Add options:
Expand Down
34 changes: 34 additions & 0 deletions init_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"strings"

. "github.com/logrusorgru/aurora"
)

func runInitCommands(config *Configuration, outputer Outputer) error {
if err := prepInit(outputer); err != nil {
return err
}
for _, section := range []string{"common", currentOS, "after"} {
commands, has := config.Init[section]
if !has {
return nil
}
outputer.OutInfo("--> Running [%s] init commands...", section)

for _, command := range commands {
// replace any occurrence of %DOTFILEDIR with dotfile directory
command = strings.Replace(command, "%DOTFILEDIR", config.Directories.Dotfiles, -1)
if dry {
outputer.OutInfo(" %s would run: '%s'", Blue("❯"), command)
continue
}
if err := runCommand(command, outputer); err != nil {
//maybe?
return err
}
}
}
return nil
}
13 changes: 13 additions & 0 deletions linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (l *Linker) Move(oldpath, newpath string) error {
return err
}

if dry {
l.outputer.OutVerbose(" would %s backup %s to %s", Green("→"), Brown(oldpath), Brown(newpath))
return nil
}

err = l.os.MkdirAll(path.Dir(newpath), 0700)
if err != nil {
return err
Expand All @@ -43,6 +48,9 @@ func (l *Linker) Move(oldpath, newpath string) error {

// SetSymlink symlinks scrAbs to destAbs.
func (l *Linker) SetSymlink(srcAbs string, destAbs string) error {
if dry {
return nil
}

dir := path.Dir(destAbs)
if err := l.os.MkdirAll(dir, 0700); err != nil {
Expand Down Expand Up @@ -78,6 +86,11 @@ func (l *Linker) NeedSymlink(src, dest string) (bool, error) {

// here dest is a wrong symlink

if dry {
l.outputer.OutInfo(" %s would delete wrong symlink %s", Green("✓"), Brown(dest))
return true, nil
}

if err = l.os.Remove(dest); err != nil {
return false, err
}
Expand Down
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
"runtime"

. "github.com/logrusorgru/aurora"
)
Expand All @@ -15,7 +16,9 @@ const logFilepath = "${HOME}/.dotbro/dotbro.log"
var debugLogger DebugLogger

var (
osfs = new(OSFS)
osfs = new(OSFS)
dry bool
currentOS string
)

func main() {
Expand All @@ -25,6 +28,9 @@ func main() {

debugLogger.Write("Start.")

// Get current operating system
currentOS = runtime.GOOS

// Parse arguments

args, err := ParseArguments(nil)
Expand All @@ -33,12 +39,16 @@ func main() {
exit(1)
}

if args["--dry"].(bool) {
dry = true
}

debugLogger.Write("Arguments passed: %+v", args)

switch {
case args["--verbose"].(bool):
outputer.Mode = OutputerModeVerbose
case args["--quiet"].(bool):
case args["--quiet"].(bool) && !dry:
outputer.Mode = OutputerModeQuiet
default:
outputer.Mode = OutputerModeNormal
Expand Down Expand Up @@ -87,6 +97,12 @@ func main() {

outputer.OutInfo("\nCleaned!")
exit(0)
case args["init"]:
if err = runInitCommands(config, outputer); err != nil {
outputer.OutError("%s", err)
exit(1)
}
fallthrough // after init, run installAction
default:
// Default action: install
if err = installAction(config, &outputer); err != nil {
Expand Down Expand Up @@ -351,6 +367,10 @@ func installDotfile(src, dest string, linker Linker, config *Configuration, srcD
exit(1)
}

if dry {
outputer.OutInfo(" %s would symlink %s to %s", Green("→"), Brown(src), Brown(dest))
return
}
outputer.OutInfo(" %s set symlink %s -> %s", Green("+"), Brown(src), Brown(dest))
}

Expand Down
34 changes: 34 additions & 0 deletions run_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build !windows

package main

import (
"fmt"
"os"
"os/exec"
)

var runCmd string

func prepInit(outputer Outputer) error {
var err error
runCmd, err = exec.LookPath("bash")
if err != nil {
runCmd, err = exec.LookPath("sh")
if err != nil {
return fmt.Errorf("unable to find 'bash' or 'sh', cannot run init commands: %s", err)
}
outputer.OutWarn("'bash' missing, using 'sh'")
}
return nil
}

func runCommand(command string, outputer Outputer) error {
cmd := exec.Command(runCmd, "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
return nil
}

0 comments on commit ba71fe9

Please sign in to comment.