Merge branch 'script_run_command' of https://github.com/queeno/aptly into queeno-script_run_command

This commit is contained in:
Andrey Smirnov
2014-08-23 22:04:50 +04:00
11 changed files with 275 additions and 29 deletions
+1
View File
@@ -4,6 +4,7 @@ gom 'code.google.com/p/gographviz', :commit => '454bc64fdfa2'
gom 'code.google.com/p/mxk/go1/flowcontrol', :commit => '5ff2502e2556' gom 'code.google.com/p/mxk/go1/flowcontrol', :commit => '5ff2502e2556'
gom 'code.google.com/p/snappy-go/snappy', :commit => '12e4b4183793' gom 'code.google.com/p/snappy-go/snappy', :commit => '12e4b4183793'
gom 'github.com/cheggaaa/pb', :commit => '74be7a1388046f374ac36e93d46f5d56e856f827' gom 'github.com/cheggaaa/pb', :commit => '74be7a1388046f374ac36e93d46f5d56e856f827'
gom 'github.com/mattn/go-shellwords', :commit => 'c7ca6f94add751566a61cf2199e1de78d4c3eee4'
gom 'github.com/mitchellh/goamz/s3', :commit => '55f224c07975fddef9d2116600c664e30df3d594' gom 'github.com/mitchellh/goamz/s3', :commit => '55f224c07975fddef9d2116600c664e30df3d594'
gom 'github.com/mkrautz/goar', :commit => '36eb5f3452b1283a211fa35bc00c646fd0db5c4b' gom 'github.com/mkrautz/goar', :commit => '36eb5f3452b1283a211fa35bc00c646fd0db5c4b'
gom 'github.com/smira/commander', :commit => 'f408b00e68d5d6e21b9f18bd310978dafc604e47' gom 'github.com/smira/commander', :commit => 'f408b00e68d5d6e21b9f18bd310978dafc604e47'
+1
View File
@@ -59,6 +59,7 @@ package environment to new version.`,
makeCmdRepo(), makeCmdRepo(),
makeCmdServe(), makeCmdServe(),
makeCmdSnapshot(), makeCmdSnapshot(),
makeCmdTask(),
makeCmdPublish(), makeCmdPublish(),
makeCmdVersion(), makeCmdVersion(),
}, },
+15
View File
@@ -24,6 +24,7 @@ import (
type AptlyContext struct { type AptlyContext struct {
flags *flag.FlagSet flags *flag.FlagSet
configLoaded bool configLoaded bool
panicked bool
progress aptly.Progress progress aptly.Progress
downloader aptly.Downloader downloader aptly.Downloader
@@ -40,6 +41,8 @@ type AptlyContext struct {
} }
var context *AptlyContext var context *AptlyContext
var savedContext *AptlyContext
var tempContext *AptlyContext
// Check interface // Check interface
var _ aptly.PublishedStorageProvider = &AptlyContext{} var _ aptly.PublishedStorageProvider = &AptlyContext{}
@@ -57,9 +60,20 @@ func Fatal(err error) {
if err == commander.ErrFlagError || err == commander.ErrCommandError { if err == commander.ErrFlagError || err == commander.ErrCommandError {
returnCode = 2 returnCode = 2
} }
if context != nil {
context.panicked = true
}
panic(&FatalError{ReturnCode: returnCode, Message: err.Error()}) panic(&FatalError{ReturnCode: returnCode, Message: err.Error()})
} }
func switchContext() {
tempContext = context
context = savedContext
savedContext = tempContext
}
// Config loads and returns current configuration // Config loads and returns current configuration
func (context *AptlyContext) Config() *utils.ConfigStructure { func (context *AptlyContext) Config() *utils.ConfigStructure {
if !context.configLoaded { if !context.configLoaded {
@@ -265,6 +279,7 @@ func InitContext(flags *flag.FlagSet) error {
context = &AptlyContext{ context = &AptlyContext{
flags: flags, flags: flags,
panicked: false,
dependencyOptions: -1, dependencyOptions: -1,
publishedStorages: map[string]aptly.PublishedStorage{}, publishedStorages: map[string]aptly.PublishedStorage{},
} }
+42
View File
@@ -0,0 +1,42 @@
package cmd
import (
"fmt"
"os"
)
func Run(cmd_args []string, exitOnPanic bool) {
defer func() {
if r := recover(); r != nil {
fatal, ok := r.(*FatalError)
if !ok {
panic(r)
}
fmt.Println("ERROR:", fatal.Message)
if exitOnPanic {
os.Exit(fatal.ReturnCode)
}
}
}()
command := RootCommand()
flags, args, err := command.ParseFlags(cmd_args)
if err != nil {
Fatal(err)
}
err = InitContext(flags)
if err != nil {
Fatal(err)
}
defer ShutdownContext()
err = command.Dispatch(args)
if err != nil {
Fatal(err)
}
}
+15
View File
@@ -0,0 +1,15 @@
package cmd
import (
"github.com/smira/commander"
)
func makeCmdTask() *commander.Command {
return &commander.Command{
UsageLine: "task",
Short: "manage aptly tasks",
Subcommands: []*commander.Command{
makeCmdTaskRun(),
},
}
}
+151
View File
@@ -0,0 +1,151 @@
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/mattn/go-shellwords"
"github.com/smira/commander"
"github.com/wsxiaoys/terminal/color"
)
func aptlyTaskRun(cmd *commander.Command, args []string) error {
var err error
var cmd_list [][]string
if filename := cmd.Flag.Lookup("filename").Value.Get().(string); filename != "" {
var text string
cmd_args := []string{}
if finfo, err := os.Stat(filename); os.IsNotExist(err) || finfo.IsDir() {
return fmt.Errorf("No such file, %s\n", filename)
}
fmt.Println("Reading file...\n")
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text = strings.TrimSpace(scanner.Text()) + ","
parsed_args, _ := shellwords.Parse(text)
cmd_args = append(cmd_args, parsed_args...)
}
if err = scanner.Err(); err != nil {
return err
}
if len(cmd_args) == 0 {
return fmt.Errorf("The file is empty. Exiting...\n")
}
cmd_list = formatCommands(cmd_args)
} else if len(args) == 0 {
var text string
cmd_args := []string{}
fmt.Println("Please enter one command per line and leave one blank when finished.")
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("> ")
text, _ = reader.ReadString('\n')
if text == "\n" {
break
} else {
text = strings.TrimSpace(text) + ","
parsed_args, _ := shellwords.Parse(text)
cmd_args = append(cmd_args, parsed_args...)
}
}
if len(cmd_args) == 0 {
return fmt.Errorf("Nothing entered. Exiting...\n")
}
cmd_list = formatCommands(cmd_args)
} else {
cmd_list = formatCommands(args)
}
switchContext()
for i, command := range cmd_list {
if context == nil || !context.panicked {
color.Printf("@g%d) [Running]: %s@!\n", (i + 1), strings.Join(command, " "))
color.Println("\n@yBegin command output: ----------------------------\n@!")
Run(command, false)
color.Println("\n@yEnd command output: ------------------------------\n@!")
} else {
color.Printf("@r%d) [Skipping]: %s@!\n", (i + 1), strings.Join(command, " "))
}
}
if context.panicked {
err = fmt.Errorf("At least one command has reported an error\n")
}
switchContext()
return err
}
func formatCommands(args []string) [][]string {
var cmd []string
var cmd_array [][]string
for _, s := range args {
if s_trimmed := strings.TrimRight(s, ","); s_trimmed != s {
cmd = append(cmd, s_trimmed)
cmd_array = append(cmd_array, cmd)
cmd = []string{}
} else {
cmd = append(cmd, s)
}
}
if len(cmd) > 0 {
cmd_array = append(cmd_array, cmd)
}
return cmd_array
}
func makeCmdTaskRun() *commander.Command {
cmd := &commander.Command{
Run: aptlyTaskRun,
UsageLine: "run -filename=<filename> | <command1>, <command2>, ...",
Short: "run aptly tasks",
Long: `
Command helps origanise multiple aptly commands in one single aptly task, running as single thread.
Example:
$ aptly task run
> repo create local
> repo add local pkg1
> publish repo local
> serve
>
`,
}
cmd.Flag.String("filename", "", "specifies the filename that contains the commands to run")
return cmd
}
+1 -29
View File
@@ -1,38 +1,10 @@
package main package main
import ( import (
"fmt"
"github.com/smira/aptly/cmd" "github.com/smira/aptly/cmd"
"os" "os"
) )
func main() { func main() {
defer func() { cmd.Run(os.Args[1:], true)
if r := recover(); r != nil {
fatal, ok := r.(*cmd.FatalError)
if !ok {
panic(r)
}
fmt.Println("ERROR:", fatal.Message)
os.Exit(fatal.ReturnCode)
}
}()
command := cmd.RootCommand()
flags, args, err := command.ParseFlags(os.Args[1:])
if err != nil {
cmd.Fatal(err)
}
err = cmd.InitContext(flags)
if err != nil {
cmd.Fatal(err)
}
defer cmd.ShutdownContext()
err = command.Dispatch(args)
if err != nil {
cmd.Fatal(err)
}
} }
+1
View File
@@ -9,6 +9,7 @@ Commands:
repo manage local package repositories repo manage local package repositories
serve HTTP serve published repositories serve HTTP serve published repositories
snapshot manage snapshots of repositories snapshot manage snapshots of repositories
task manage aptly tasks
version display version version display version
Use "aptly help <command>" for more information about a command. Use "aptly help <command>" for more information about a command.
+34
View File
@@ -0,0 +1,34 @@
1) [Running]: repo list

Begin command output: ----------------------------
 
No local repositories found, create one with `aptly repo create ...`.
End command output: ------------------------------
 
2) [Running]: repo create local

Begin command output: ----------------------------
 
Local repo [local] successfully added.
You can run 'aptly repo add local ...' to add packages to repository.
End command output: ------------------------------
 
3) [Running]: repo drop local

Begin command output: ----------------------------
 
Local repo `local` has been removed.
End command output: ------------------------------
 
4) [Running]: version

Begin command output: ----------------------------
 
aptly version: 0.8~dev
End command output: ------------------------------
 
+5
View File
@@ -0,0 +1,5 @@
"""
Test aptly task run
"""
from .run import *
+9
View File
@@ -0,0 +1,9 @@
from lib import BaseTest
class RunTask1Test(BaseTest):
"""
output should match
"""
runCmd = "aptly task run repo list, repo create local, repo drop local, version"