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
2014-08-16 11:44:24 +00:00
parent b2779d7a88
commit 3c7696ef7e
2 changed files with 43 additions and 29 deletions

42
cmd/run.go Normal file
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)
}
}

30
main.go
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)
}