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-04-02 09:31:36 +00:00
|
|
|
"path/filepath"
|
2015-06-24 09:01:55 +00:00
|
|
|
|
|
|
|
"github.com/naoina/toml"
|
|
|
|
"gopkg.in/yaml.v2"
|
2015-04-02 09:16:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-08 10:31:47 +00:00
|
|
|
flagClobber = 1 << iota
|
|
|
|
flagForce
|
|
|
|
flagVerbose
|
2015-06-14 08:00:07 +00:00
|
|
|
flagNoCmd
|
|
|
|
flagNoLink
|
2015-03-31 05:51:43 +00:00
|
|
|
)
|
|
|
|
|
2015-03-31 11:59:09 +00:00
|
|
|
func parse(filename string) (*config, error) {
|
|
|
|
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-04-02 11:30:57 +00:00
|
|
|
case ".toml":
|
2015-04-02 11:29:29 +00:00
|
|
|
if err := toml.Unmarshal(bytes, &conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-02 11:30:57 +00:00
|
|
|
case ".yaml":
|
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-06-24 09:01:55 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] conf src\n", 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-04-05 07:53:32 +00:00
|
|
|
func makeAbsPath(path string) string {
|
2015-04-02 09:31:36 +00:00
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
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-04-05 09:40:46 +00:00
|
|
|
flags |= flagClobber
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
|
|
|
if *force {
|
2015-04-05 09:40:46 +00:00
|
|
|
flags |= flagForce
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
|
|
|
if *verbose {
|
2015-04-05 09:40:46 +00:00
|
|
|
flags |= flagVerbose
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
2015-06-14 08:00:07 +00:00
|
|
|
if *nocmd {
|
|
|
|
flags |= flagNoCmd
|
|
|
|
}
|
|
|
|
if *nolink {
|
|
|
|
flags |= flagNoLink
|
|
|
|
}
|
2015-04-02 09:16:59 +00:00
|
|
|
|
2015-04-05 07:53:32 +00:00
|
|
|
if flag.NArg() == 2 {
|
|
|
|
conf, err := parse(flag.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2015-04-02 09:16:59 +00:00
|
|
|
}
|
2015-04-05 07:53:32 +00:00
|
|
|
|
2015-04-05 08:55:09 +00:00
|
|
|
if err := conf.process(makeAbsPath(flag.Arg(1)), makeAbsPath(*dstDir), *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
|
|
|
}
|