mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-02 04:50:49 +00:00
Merge branch 'script_run_command' of https://github.com/queeno/aptly into queeno-script_run_command
This commit is contained in:
@@ -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/snappy-go/snappy', :commit => '12e4b4183793'
|
||||
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/mkrautz/goar', :commit => '36eb5f3452b1283a211fa35bc00c646fd0db5c4b'
|
||||
gom 'github.com/smira/commander', :commit => 'f408b00e68d5d6e21b9f18bd310978dafc604e47'
|
||||
|
||||
@@ -59,6 +59,7 @@ package environment to new version.`,
|
||||
makeCmdRepo(),
|
||||
makeCmdServe(),
|
||||
makeCmdSnapshot(),
|
||||
makeCmdTask(),
|
||||
makeCmdPublish(),
|
||||
makeCmdVersion(),
|
||||
},
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
type AptlyContext struct {
|
||||
flags *flag.FlagSet
|
||||
configLoaded bool
|
||||
panicked bool
|
||||
|
||||
progress aptly.Progress
|
||||
downloader aptly.Downloader
|
||||
@@ -40,6 +41,8 @@ type AptlyContext struct {
|
||||
}
|
||||
|
||||
var context *AptlyContext
|
||||
var savedContext *AptlyContext
|
||||
var tempContext *AptlyContext
|
||||
|
||||
// Check interface
|
||||
var _ aptly.PublishedStorageProvider = &AptlyContext{}
|
||||
@@ -57,9 +60,20 @@ func Fatal(err error) {
|
||||
if err == commander.ErrFlagError || err == commander.ErrCommandError {
|
||||
returnCode = 2
|
||||
}
|
||||
if context != nil {
|
||||
context.panicked = true
|
||||
}
|
||||
panic(&FatalError{ReturnCode: returnCode, Message: err.Error()})
|
||||
}
|
||||
|
||||
func switchContext() {
|
||||
|
||||
tempContext = context
|
||||
context = savedContext
|
||||
savedContext = tempContext
|
||||
|
||||
}
|
||||
|
||||
// Config loads and returns current configuration
|
||||
func (context *AptlyContext) Config() *utils.ConfigStructure {
|
||||
if !context.configLoaded {
|
||||
@@ -265,6 +279,7 @@ func InitContext(flags *flag.FlagSet) error {
|
||||
|
||||
context = &AptlyContext{
|
||||
flags: flags,
|
||||
panicked: false,
|
||||
dependencyOptions: -1,
|
||||
publishedStorages: map[string]aptly.PublishedStorage{},
|
||||
}
|
||||
|
||||
+42
@@ -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
@@ -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
@@ -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,38 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/smira/aptly/cmd"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func() {
|
||||
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)
|
||||
}
|
||||
cmd.Run(os.Args[1:], true)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ Commands:
|
||||
repo manage local package repositories
|
||||
serve HTTP serve published repositories
|
||||
snapshot manage snapshots of repositories
|
||||
task manage aptly tasks
|
||||
version display version
|
||||
|
||||
Use "aptly help <command>" for more information about a command.
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
[0;32;49m1) [Running]: repo list[1;39;49m
|
||||
[0m
|
||||
[0;33;49mBegin command output: ----------------------------
|
||||
[1;39;49m [0m
|
||||
No local repositories found, create one with `aptly repo create ...`.
|
||||
|
||||
[0;33;49mEnd command output: ------------------------------
|
||||
[1;39;49m [0m
|
||||
[0;32;49m2) [Running]: repo create local[1;39;49m
|
||||
[0m
|
||||
[0;33;49mBegin command output: ----------------------------
|
||||
[1;39;49m [0m
|
||||
|
||||
Local repo [local] successfully added.
|
||||
You can run 'aptly repo add local ...' to add packages to repository.
|
||||
|
||||
[0;33;49mEnd command output: ------------------------------
|
||||
[1;39;49m [0m
|
||||
[0;32;49m3) [Running]: repo drop local[1;39;49m
|
||||
[0m
|
||||
[0;33;49mBegin command output: ----------------------------
|
||||
[1;39;49m [0m
|
||||
Local repo `local` has been removed.
|
||||
|
||||
[0;33;49mEnd command output: ------------------------------
|
||||
[1;39;49m [0m
|
||||
[0;32;49m4) [Running]: version[1;39;49m
|
||||
[0m
|
||||
[0;33;49mBegin command output: ----------------------------
|
||||
[1;39;49m [0m
|
||||
aptly version: 0.8~dev
|
||||
|
||||
[0;33;49mEnd command output: ------------------------------
|
||||
[1;39;49m [0m
|
||||
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Test aptly task run
|
||||
"""
|
||||
|
||||
from .run import *
|
||||
@@ -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"
|
||||
Reference in New Issue
Block a user