Adding support for commands
This commit is contained in:
parent
1f8da243cf
commit
7876ad587f
54
command.go
Normal file
54
command.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type command []string
|
||||||
|
|
||||||
|
func (this *command) valid() bool {
|
||||||
|
return len(*this) >= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *command) process(dir string, flags int) error {
|
||||||
|
if !this.valid() {
|
||||||
|
return fmt.Errorf("command element is invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command((*this)[0], (*this)[1:]...)
|
||||||
|
cmd.Dir = dir
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
|
||||||
|
if flags&optVerbose == optVerbose {
|
||||||
|
log.Printf("executing command %s", strings.Join(*this, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
@ -28,11 +28,11 @@ type config struct {
|
|||||||
Tasks map[string]task
|
Tasks map[string]task
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *config) install(srcDir, dstDir, taskName string, flags int) error {
|
func (this *config) process(srcDir, dstDir, taskName string, flags int) error {
|
||||||
task, ok := this.Tasks[taskName]
|
task, ok := this.Tasks[taskName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Task not found: '%s'", taskName)
|
return fmt.Errorf("task not found %s", taskName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return task.install(srcDir, dstDir, this, flags)
|
return task.process(srcDir, dstDir, this, flags)
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func parse(filename string) (*config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Unsupported configuration file format")
|
return nil, fmt.Errorf("unsupported configuration file format")
|
||||||
}
|
}
|
||||||
|
|
||||||
return conf, nil
|
return conf, nil
|
||||||
@ -116,7 +116,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := conf.install(makeAbsPath(flag.Arg(1)), makeAbsPath(*dstDir), *taskName, flags); err != nil {
|
if err := conf.process(makeAbsPath(flag.Arg(1)), makeAbsPath(*dstDir), *taskName, flags); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
31
link.go
31
link.go
@ -29,27 +29,25 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
func cleanPath(loc string, flags int) error {
|
type link []string
|
||||||
verbose := flags&optVerbose == optVerbose
|
|
||||||
|
|
||||||
|
func cleanPath(loc string, flags int) error {
|
||||||
if info, _ := os.Lstat(loc); info != nil {
|
if info, _ := os.Lstat(loc); info != nil {
|
||||||
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
||||||
if verbose {
|
if flags&optVerbose == optVerbose {
|
||||||
log.Printf("Removing symlink: '%s'", loc)
|
log.Printf("removing symlink %s", loc)
|
||||||
}
|
}
|
||||||
if err := os.Remove(loc); err != nil {
|
if err := os.Remove(loc); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if flags&optClobber == optClobber {
|
if flags&optClobber == optClobber {
|
||||||
if verbose {
|
if flags&optVerbose == optVerbose {
|
||||||
log.Print("Clobbering path: '%s'", loc)
|
log.Printf("clobbering path %s", loc)
|
||||||
}
|
}
|
||||||
if err := os.RemoveAll(loc); err != nil {
|
if err := os.RemoveAll(loc); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return fmt.Errorf("Cannot create link; target already exists: '%s'", loc)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,20 +56,17 @@ func cleanPath(loc string, flags int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createPath(loc string, flags int) error {
|
func createPath(loc string, flags int) error {
|
||||||
if flags&optForce == 0 {
|
if flags&optForce == optForce {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
parentDir, _ := path.Split(loc)
|
parentDir, _ := path.Split(loc)
|
||||||
|
|
||||||
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
|
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
|
||||||
if flags&optVerbose == optVerbose {
|
if flags&optVerbose == optVerbose {
|
||||||
log.Printf("Force creating path: '%s'", parentDir)
|
log.Printf("force creating path %s", parentDir)
|
||||||
}
|
}
|
||||||
if err := os.MkdirAll(parentDir, 0777); err != nil {
|
if err := os.MkdirAll(parentDir, 0777); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -97,16 +92,16 @@ func (this *link) valid() bool {
|
|||||||
return length >= 1 && length <= 2
|
return length >= 1 && length <= 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *link) install(srcDir, dstDir string, flags int) error {
|
func (this *link) process(srcDir, dstDir string, flags int) error {
|
||||||
if !this.valid() {
|
if !this.valid() {
|
||||||
return fmt.Errorf("Link element is invalid")
|
return fmt.Errorf("link element is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
srcPath := path.Join(srcDir, this.source())
|
srcPath := path.Join(srcDir, this.source())
|
||||||
dstPath := path.Join(dstDir, this.destination())
|
dstPath := path.Join(dstDir, this.destination())
|
||||||
|
|
||||||
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
|
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
|
||||||
return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath)
|
return fmt.Errorf("source path %s does not exist in filesystem", srcPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := createPath(dstPath, flags); err != nil {
|
if err := createPath(dstPath, flags); err != nil {
|
||||||
@ -118,7 +113,7 @@ func (this *link) install(srcDir, dstDir string, flags int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if flags&optVerbose == optVerbose {
|
if flags&optVerbose == optVerbose {
|
||||||
log.Printf("Linking: '%s' to '%s'", srcPath, dstPath)
|
log.Printf("linking %s to %s", srcPath, dstPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
return os.Symlink(srcPath, dstPath)
|
return os.Symlink(srcPath, dstPath)
|
||||||
|
18
task.go
18
task.go
@ -24,27 +24,33 @@ package main
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type link []string
|
|
||||||
|
|
||||||
type task struct {
|
type task struct {
|
||||||
Deps []string
|
Deps []string
|
||||||
Links []link
|
Links []link
|
||||||
|
Commands []command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *task) install(srcDir, dstDir string, conf *config, flags int) error {
|
func (this *task) process(srcDir, dstDir string, conf *config, flags int) error {
|
||||||
for _, depName := range this.Deps {
|
for _, depName := range this.Deps {
|
||||||
depTask, ok := conf.Tasks[depName]
|
depTask, ok := conf.Tasks[depName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Task dependency not found: '%s'", depName)
|
return fmt.Errorf("task dependency not found %s", depName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := depTask.install(srcDir, dstDir, conf, flags); err != nil {
|
if err := depTask.process(srcDir, dstDir, conf, flags); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, currLink := range this.Links {
|
for _, currLink := range this.Links {
|
||||||
if err := currLink.install(srcDir, dstDir, flags); err != nil {
|
if err := currLink.process(srcDir, dstDir, flags); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(this.Commands)
|
||||||
|
for _, currCmd := range this.Commands {
|
||||||
|
if err := currCmd.process(dstDir, flags); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user