Refactored main.go

The main function - whuch runs aptly commands - has been taken out from
main.go and included to the cmd package. This is useful for the aptly
script run command, which should use that behaviour.
This commit is contained in:
Simon Aquino committed 2014-08-16 13:55:13 +00:00
1 parent b2779d7a88
commit 3c7696ef7e
2 files changed
+43 -29

No files matched your search

+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)
}
}
+1 -29
View File
@@ -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)
}