mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-04-20 19:38:39 +00:00
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:
42
cmd/run.go
Normal file
42
cmd/run.go
Normal 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
30
main.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user