homemaker/homemaker.go

121 lines
3.3 KiB
Go
Raw Normal View History

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 02:16:45 +00:00
"flag"
2015-04-02 09:16:59 +00:00
"fmt"
2015-03-31 05:51:43 +00:00
"log"
2015-04-02 09:16:59 +00:00
"os"
"path"
"strings"
2015-04-02 09:16:59 +00:00
)
const (
flagClobber = 1 << iota
flagForce
flagVerbose
2015-09-06 05:45:57 +00:00
flagNoCmds
flagNoLinks
2017-06-29 13:40:34 +00:00
flagNoTemplates
flagNoMacro
2015-09-06 05:45:57 +00:00
flagUnlink = flagNoCmds | (1 << iota)
2015-03-31 05:51:43 +00:00
)
2015-04-03 03:58:41 +00:00
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] conf src\n", path.Base(os.Args[0]))
2016-02-14 21:16:03 +00:00
fmt.Fprintf(os.Stderr, "https://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 09:16:59 +00:00
taskName := flag.String("task", "default", "name of task to execute")
dstDir := flag.String("dest", "", "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-09-06 05:45:57 +00:00
nocmds := flag.Bool("nocmds", false, "don't execute commands")
nolinks := flag.Bool("nolinks", false, "don't create links")
2017-06-29 13:40:34 +00:00
notemplates := flag.Bool("notemplates", false, "don't process templates")
2015-09-06 05:45:57 +00:00
variant := flag.String("variant", "", "execution variant for tasks and macros")
unlink := flag.Bool("unlink", false, "remove existing links instead of creating them")
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 {
flags |= flagClobber
2015-04-02 09:16:59 +00:00
}
if *force {
flags |= flagForce
2015-04-02 09:16:59 +00:00
}
if *verbose {
flags |= flagVerbose
2015-04-02 09:16:59 +00:00
}
2015-09-06 05:45:57 +00:00
if *nocmds {
flags |= flagNoCmds
}
2015-09-06 05:45:57 +00:00
if *nolinks {
flags |= flagNoLinks
}
2017-06-29 13:40:34 +00:00
if *notemplates {
flags |= flagNoTemplates
}
2015-09-06 05:45:57 +00:00
if *unlink {
flags |= flagUnlink
}
2015-04-02 09:16:59 +00:00
2015-04-05 07:53:32 +00:00
if flag.NArg() == 2 {
2015-09-04 03:15:57 +00:00
confFile := makeAbsPath(flag.Arg(0))
2015-07-02 08:54:49 +00:00
2015-09-04 03:15:57 +00:00
conf, err := newConfig(confFile)
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
if strings.TrimSpace(*dstDir) == "" {
*dstDir, _ = os.UserHomeDir()
}
2015-09-04 03:15:57 +00:00
conf.srcDir = makeAbsPath(flag.Arg(1))
conf.dstDir = makeAbsPath(*dstDir)
conf.variant = *variant
conf.flags = flags
os.Setenv("HM_CONFIG", confFile)
os.Setenv("HM_TASK", *taskName)
os.Setenv("HM_SRC", conf.srcDir)
os.Setenv("HM_DEST", conf.dstDir)
os.Setenv("HM_VARIANT", conf.variant)
if err := processTask(*taskName, conf); 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
}