This commit is contained in:
Alex Yatskov 2015-07-20 18:53:42 +09:00
parent d1dcb0a2f4
commit 20602f0a96
7 changed files with 49 additions and 52 deletions

View File

@ -30,10 +30,8 @@ import (
"strings"
)
type command []string
func (c command) process(dir string, flags int) error {
args := appendExpEnv(nil, c)
func processCmd(params []string, dir string, flags int) error {
args := appendExpEnv(nil, params)
var cmd *exec.Cmd
switch {

View File

@ -30,5 +30,5 @@ type config struct {
func (c *config) process(srcDir, dstDir, taskName string, flags int) error {
c.tasksHandled = make(map[string]bool)
return task(taskName).process(srcDir, dstDir, c, flags)
return processTask(taskName, srcDir, dstDir, c, flags)
}

View File

@ -29,31 +29,29 @@ import (
"strings"
)
type env []string
func (e env) process(flags int) error {
items := appendExpEnv(nil, e)
func processEnv(env []string, flags int) error {
args := appendExpEnv(nil, env)
var value string
switch {
case len(items) == 0:
case len(args) == 0:
return fmt.Errorf("enviornment element is invalid")
case len(items) == 1:
case len(args) == 1:
if flags&flagVerbose == flagVerbose {
log.Printf("unsetting variable %s", items[0])
log.Printf("unsetting variable %s", args[0])
}
os.Unsetenv(items[0])
os.Unsetenv(args[0])
return nil
case len(items) == 2:
value = items[1]
case len(args) == 2:
value = args[1]
default:
value = strings.Join(items[1:], ",")
value = strings.Join(args[1:], ",")
}
if flags&flagVerbose == flagVerbose {
log.Printf("setting variable %s to %s", items[0], value)
log.Printf("setting variable %s to %s", args[0], value)
}
os.Setenv(items[0], value)
os.Setenv(args[0], value)
return nil
}

16
link.go
View File

@ -30,8 +30,6 @@ import (
"strconv"
)
type link []string
func cleanPath(loc string, flags int) error {
if info, _ := os.Lstat(loc); info != nil {
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
@ -72,8 +70,8 @@ func createPath(loc string, flags int, mode os.FileMode) error {
return nil
}
func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err error) {
length := len(l)
func parseLink(params []string) (srcPath, dstPath string, mode os.FileMode, err error) {
length := len(params)
if length < 1 || length > 3 {
err = fmt.Errorf("link element is invalid")
return
@ -81,7 +79,7 @@ func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err err
if length > 2 {
var parsed uint64
parsed, err = strconv.ParseUint(l[2], 0, 64)
parsed, err = strconv.ParseUint(params[2], 0, 64)
if err != nil {
return
}
@ -91,17 +89,17 @@ func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err err
mode = 0755
}
dstPath = os.ExpandEnv(l[0])
dstPath = os.ExpandEnv(params[0])
srcPath = dstPath
if length > 1 {
srcPath = os.ExpandEnv(l[1])
srcPath = os.ExpandEnv(params[1])
}
return
}
func (l link) process(srcDir, dstDir string, flags int) error {
srcPath, dstPath, mode, err := l.parse()
func processLink(params []string, srcDir, dstDir string, flags int) error {
srcPath, dstPath, mode, err := parseLink(params)
if err != nil {
return err
}

View File

@ -53,17 +53,15 @@ func (m macroDef) process(dir string, params []string, flags int) error {
return cmd.Run()
}
type macro []string
func (m macro) process(dir string, conf *config, flags int) error {
if len(m) == 0 {
func processMacro(args []string, dir string, conf *config, flags int) error {
if len(args) == 0 {
return fmt.Errorf("macro element is invalid")
}
macro, ok := conf.Macros[m[0]]
macro, ok := conf.Macros[args[0]]
if !ok {
return fmt.Errorf("macro dependency not found %s", m[0])
return fmt.Errorf("macro dependency not found %s", args[0])
}
return macro.process(dir, m[1:], flags)
return macro.process(dir, args[1:], flags)
}

28
task.go
View File

@ -25,29 +25,29 @@ package main
import "fmt"
type taskDef struct {
Deps []task `json:",string"`
Links []link
Cmds []command
Macros []macro
Envs []env
Deps []string
Links [][]string
Cmds [][]string
Macros [][]string
Envs [][]string
}
func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error {
for _, currTask := range t.Deps {
if err := currTask.process(srcDir, dstDir, conf, flags); err != nil {
if err := processTask(currTask, srcDir, dstDir, conf, flags); err != nil {
return err
}
}
for _, currEnv := range t.Envs {
if err := currEnv.process(flags); err != nil {
if err := processEnv(currEnv, flags); err != nil {
return err
}
}
if flags&flagNoMacro == 0 {
for _, currMacro := range t.Macros {
if err := currMacro.process(dstDir, conf, flags); err != nil {
if err := processMacro(currMacro, dstDir, conf, flags); err != nil {
return err
}
}
@ -55,7 +55,7 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
if flags&flagNoLink == 0 {
for _, currLink := range t.Links {
if err := currLink.process(srcDir, dstDir, flags); err != nil {
if err := processLink(currLink, srcDir, dstDir, flags); err != nil {
return err
}
}
@ -63,7 +63,7 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
if flags&flagNoCmd == 0 {
for _, currCmd := range t.Cmds {
if err := currCmd.process(dstDir, flags); err != nil {
if err := processCmd(currCmd, dstDir, flags); err != nil {
return err
}
}
@ -72,11 +72,7 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
return nil
}
type task string
func (t task) process(srcDir, dstDir string, conf *config, flags int) error {
taskName := string(t)
func processTask(taskName, srcDir, dstDir string, conf *config, flags int) error {
handled, ok := conf.tasksHandled[taskName]
if ok && handled {
return nil
@ -86,7 +82,7 @@ func (t task) process(srcDir, dstDir string, conf *config, flags int) error {
task, ok := conf.Tasks[taskName]
if !ok {
return fmt.Errorf("task not found %s", t)
return fmt.Errorf("task not found %s", taskName)
}
return task.process(srcDir, dstDir, conf, flags)

13
util.go
View File

@ -1,6 +1,15 @@
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
all
* 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
@ -9,7 +18,7 @@ all
* 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