This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from talal/cleanup
add init command and minor cleanup
- Loading branch information
Showing
29 changed files
with
720 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module github.com/talal/bonclay | ||
|
||
require ( | ||
github.com/talal/go-bits v0.0.0-20190112130027-6c329ec8a3e2 | ||
github.com/talal/go-bits/cli v0.0.0-20190206232352-5b28e4a5144d | ||
github.com/talal/go-bits/color v0.0.0-20190206232352-5b28e4a5144d | ||
gopkg.in/yaml.v2 v2.2.2 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package commands | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/talal/bonclay/pkg/file" | ||
"github.com/talal/bonclay/pkg/mistertwo" | ||
"github.com/talal/bonclay/pkg/write" | ||
"github.com/talal/go-bits/cli" | ||
) | ||
|
||
const backupDesc = `Backup files/directories to their target location.` | ||
|
||
const backupUsage = ` | ||
Usage: bonclay backup <config-file> | ||
Backup uses the 'source:target' pairs defined in the configuration spec | ||
to copy the sources to the targets. | ||
` | ||
|
||
// Backup represents the backup command. | ||
var Backup = makeBackupCmd() | ||
|
||
func makeBackupCmd() cli.Command { | ||
fs := flag.NewFlagSet("bonclay backup", flag.ExitOnError) | ||
|
||
fs.Usage = func() { | ||
fmt.Println(strings.TrimSpace(backupUsage)) | ||
} | ||
|
||
return cli.Command{ | ||
Name: "backup", | ||
Desc: backupDesc, | ||
Action: func(args []string) { | ||
fs.Parse(args) | ||
args = fs.Args() | ||
|
||
if len(args) != 1 { | ||
fs.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
cfg := mistertwo.NewConfiguration(args[0]) | ||
backupTask(cfg) | ||
}, | ||
} | ||
} | ||
|
||
// backupTask uses 'source:target' pairs defined in the configuration spec to copy | ||
// the sources to the targets. | ||
func backupTask(config *mistertwo.Configuration) { | ||
write.TaskHeader("backup") | ||
|
||
errors := make([]string, 0, len(config.Spec)) | ||
for src, dst := range config.Spec { | ||
err := file.Copy(src, dst, config.Backup.Overwrite) | ||
if err != nil { | ||
errors = append(errors, err.Error()) | ||
write.TaskFailure(src, dst) | ||
continue | ||
} | ||
|
||
write.TaskSuccess(src, dst) | ||
} | ||
|
||
write.TaskFooter("backup", len(errors) == 0) | ||
write.TaskErrors(errors) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package commands | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/talal/go-bits/cli" | ||
) | ||
|
||
const initDesc = `Create a new config file in the current directory.` | ||
|
||
const initUsage = ` | ||
Usage: bonclay init | ||
Create a new config file in the current directory with sane defaults and | ||
example spec section. | ||
` | ||
|
||
const exampleConfig = ` | ||
# For a detailed description on the different options, take a look at the | ||
# user guide: https://github.com/talal/bonclay/blob/master/doc/guide.md | ||
backup: | ||
overwrite: false | ||
restore: | ||
overwrite: false | ||
sync: | ||
clean: true | ||
overwrite: true | ||
spec: | ||
# ~/examplefile: file | ||
# ~/exampledir: dir | ||
# ~/dir/another one: another one | ||
` | ||
|
||
// Init represents the init command. | ||
var Init = makeInitCmd() | ||
|
||
func makeInitCmd() cli.Command { | ||
fs := flag.NewFlagSet("bonclay init", flag.ExitOnError) | ||
|
||
fs.Usage = func() { | ||
fmt.Println(strings.TrimSpace(initUsage)) | ||
} | ||
|
||
return cli.Command{ | ||
Name: "init", | ||
Desc: initDesc, | ||
Action: func(args []string) { | ||
fs.Parse(args) | ||
args = fs.Args() | ||
|
||
if len(args) > 0 { | ||
fs.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
initTask() | ||
}, | ||
} | ||
} | ||
|
||
// initTask creates a sample config file (bonclay.conf.yaml) in the current | ||
// directory. | ||
func initTask() { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
cwd = os.Getenv("PWD") | ||
} | ||
|
||
cfgPath := filepath.Join(filepath.Clean(cwd), "bonclay.conf.yaml") | ||
|
||
_, err = os.Lstat(cfgPath) | ||
if err == nil { | ||
fmt.Fprintf(os.Stderr, "bonclay: error: config file already exists: %s\n", cfgPath) | ||
os.Exit(1) | ||
} | ||
|
||
f, err := os.Create(cfgPath) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "bonclay: error: could not create config file: %s\n", cfgPath) | ||
os.Exit(1) | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.WriteString(strings.TrimSpace(exampleConfig)) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "bonclay: error: could not create config file: %s\n", cfgPath) | ||
os.Exit(1) | ||
} | ||
f.Sync() | ||
|
||
fmt.Printf("bonclay: config file created: %s\n", cfgPath) | ||
} |
Oops, something went wrong.