2015-03-31 04:37:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2015-03-31 05:51:43 +00:00
|
|
|
import (
|
2015-04-02 11:29:29 +00:00
|
|
|
"encoding/json"
|
2015-04-02 02:16:45 +00:00
|
|
|
"flag"
|
2015-04-02 09:16:59 +00:00
|
|
|
"fmt"
|
2015-03-31 05:51:43 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-04-02 09:16:59 +00:00
|
|
|
"os"
|
2015-04-02 02:16:45 +00:00
|
|
|
"os/user"
|
2015-04-02 09:16:59 +00:00
|
|
|
"path"
|
2015-06-24 09:01:55 +00:00
|
|
|
|
2015-08-10 04:48:57 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-06-24 09:01:55 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2015-04-02 09:16:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-08-10 09:33:22 +00:00
|
|
|
flagClobber = 1 << iota
|
|
|
|
flagForce
|
|
|
|
flagVerbose
|
|
|
|
flagNoCmd
|
|
|
|
flagNoLink
|
|
|
|
flagNoMacro
|
2015-03-31 05:51:43 +00:00
|
|
|
)
|
|
|
|
|
2015-07-20 10:42:08 +00:00
|
|
|
func parseCfg(filename string) (*config, error) {
|
2015-03-31 11:59:09 +00:00
|
|
|
bytes, err := ioutil.ReadFile(filename)
|
2015-03-31 05:51:43 +00:00
|
|
|
if err != nil {
|
2015-03-31 11:59:09 +00:00
|
|
|
return nil, err
|
2015-03-31 05:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 11:59:09 +00:00
|
|
|
conf := &config{}
|
2015-04-02 11:29:29 +00:00
|
|
|
switch path.Ext(filename) {
|
2015-04-02 11:30:57 +00:00
|
|
|
case ".json":
|
2015-04-02 11:29:29 +00:00
|
|
|
if err := json.Unmarshal(bytes, &conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-20 04:09:08 +00:00
|
|
|
case ".toml", ".tml":
|
2015-04-02 11:29:29 +00:00
|
|
|
if err := toml.Unmarshal(bytes, &conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-20 04:09:08 +00:00
|
|
|
case ".yaml", ".yml":
|
2015-04-02 11:29:29 +00:00
|
|
|
if err := yaml.Unmarshal(bytes, &conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2015-04-05 08:55:09 +00:00
|
|
|
return nil, fmt.Errorf("unsupported configuration file format")
|
2015-03-31 05:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 11:59:09 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2015-04-03 03:58:41 +00:00
|
|
|
func usage() {
|
2015-08-10 04:48:57 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] conf src\n", path.Base(os.Args[0]))
|
2015-05-18 02:11:25 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "http://foosoft.net/projects/homemaker/\n\n")
|
|
|
|
fmt.Fprintf(os.Stderr, "Parameters:\n")
|
2015-04-02 09:16:59 +00:00
|
|
|
flag.PrintDefaults()
|
2015-04-02 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 11:59:09 +00:00
|
|
|
func main() {
|
2015-04-02 02:16:45 +00:00
|
|
|
currUsr, err := user.Current()
|
2015-04-02 09:31:36 +00:00
|
|
|
if err != nil {
|
2015-03-31 05:51:43 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-03-31 04:37:32 +00:00
|
|
|
|
2015-04-02 09:16:59 +00:00
|
|
|
taskName := flag.String("task", "default", "name of task to execute")
|
2015-04-06 01:23:17 +00:00
|
|
|
dstDir := flag.String("dest", currUsr.HomeDir, "target directory for tasks")
|
2015-04-02 09:16:59 +00:00
|
|
|
force := flag.Bool("force", true, "create parent directories to target")
|
2015-04-02 02:16:45 +00:00
|
|
|
clobber := flag.Bool("clobber", false, "delete files and directories at target")
|
2015-04-02 09:16:59 +00:00
|
|
|
verbose := flag.Bool("verbose", false, "verbose output")
|
2015-06-14 08:00:07 +00:00
|
|
|
nocmd := flag.Bool("nocmd", false, "don't execute commands")
|
|
|
|
nolink := flag.Bool("nolink", false, "don't create links")
|
2015-04-02 02:16:45 +00:00
|
|
|
|
2015-04-03 03:58:41 +00:00
|
|
|
flag.Usage = usage
|
2015-04-02 02:16:45 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-04-02 09:16:59 +00:00
|
|
|
flags := 0
|
|
|
|
if *clobber {
|
2015-08-10 09:33:22 +00:00
|
|
|
flags |= flagClobber
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
|
|
|
if *force {
|
2015-08-10 09:33:22 +00:00
|
|
|
flags |= flagForce
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
|
|
|
if *verbose {
|
2015-08-10 09:33:22 +00:00
|
|
|
flags |= flagVerbose
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
2015-06-14 08:00:07 +00:00
|
|
|
if *nocmd {
|
2015-08-10 09:33:22 +00:00
|
|
|
flags |= flagNoCmd
|
2015-06-14 08:00:07 +00:00
|
|
|
}
|
|
|
|
if *nolink {
|
2015-08-10 09:33:22 +00:00
|
|
|
flags |= flagNoLink
|
2015-06-14 08:00:07 +00:00
|
|
|
}
|
2015-04-02 09:16:59 +00:00
|
|
|
|
2015-04-05 07:53:32 +00:00
|
|
|
if flag.NArg() == 2 {
|
2015-07-02 08:55:58 +00:00
|
|
|
confDirAbs := makeAbsPath(flag.Arg(0))
|
2015-07-02 08:54:49 +00:00
|
|
|
srcDirAbs := makeAbsPath(flag.Arg(1))
|
|
|
|
dstDirAbs := makeAbsPath(*dstDir)
|
|
|
|
|
|
|
|
os.Setenv("HM_CONFIG", confDirAbs)
|
|
|
|
os.Setenv("HM_TASK", *taskName)
|
|
|
|
os.Setenv("HM_SRC", srcDirAbs)
|
|
|
|
os.Setenv("HM_DEST", dstDirAbs)
|
|
|
|
|
2015-07-20 10:42:08 +00:00
|
|
|
conf, err := parseCfg(confDirAbs)
|
2015-04-05 07:53:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
2015-04-05 07:53:32 +00:00
|
|
|
|
2015-07-02 08:55:58 +00:00
|
|
|
if err := conf.process(srcDirAbs, dstDirAbs, *taskName, flags); err != nil {
|
2015-04-02 09:16:59 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-04-05 07:53:32 +00:00
|
|
|
} else {
|
2015-04-03 03:58:41 +00:00
|
|
|
usage()
|
|
|
|
os.Exit(2)
|
2015-04-02 02:16:45 +00:00
|
|
|
}
|
2015-03-31 04:37:32 +00:00
|
|
|
}
|