Refactor commands to subpackage.

This commit is contained in:
Andrey Smirnov
2014-02-19 14:59:00 +04:00
parent 35c2178074
commit eb18b04c40
12 changed files with 144 additions and 36 deletions

68
cmd/context.go Normal file
View File

@@ -0,0 +1,68 @@
package cmd
import (
"fmt"
"github.com/gonuts/commander"
"github.com/smira/aptly/aptly"
"github.com/smira/aptly/console"
"github.com/smira/aptly/database"
"github.com/smira/aptly/debian"
"github.com/smira/aptly/files"
"github.com/smira/aptly/http"
"github.com/smira/aptly/utils"
"path/filepath"
"strings"
)
// Common context shared by all commands
var context struct {
progress aptly.Progress
downloader aptly.Downloader
database database.Storage
packagePool aptly.PackagePool
publishedStorage aptly.PublishedStorage
dependencyOptions int
architecturesList []string
}
func InitContext(cmd *commander.Command) error {
var err error
context.dependencyOptions = 0
if utils.Config.DepFollowSuggests || cmd.Flag.Lookup("dep-follow-suggests").Value.Get().(bool) {
context.dependencyOptions |= debian.DepFollowSuggests
}
if utils.Config.DepFollowRecommends || cmd.Flag.Lookup("dep-follow-recommends").Value.Get().(bool) {
context.dependencyOptions |= debian.DepFollowRecommends
}
if utils.Config.DepFollowAllVariants || cmd.Flag.Lookup("dep-follow-all-variants").Value.Get().(bool) {
context.dependencyOptions |= debian.DepFollowAllVariants
}
if utils.Config.DepFollowSource || cmd.Flag.Lookup("dep-follow-source").Value.Get().(bool) {
context.dependencyOptions |= debian.DepFollowSource
}
context.architecturesList = utils.Config.Architectures
optionArchitectures := cmd.Flag.Lookup("architectures").Value.String()
if optionArchitectures != "" {
context.architecturesList = strings.Split(optionArchitectures, ",")
}
context.progress = console.NewProgress()
context.progress.Start()
defer context.progress.Shutdown()
context.downloader = http.NewDownloader(utils.Config.DownloadConcurrency, context.progress)
defer context.downloader.Shutdown()
context.database, err = database.OpenDB(filepath.Join(utils.Config.RootDir, "db"))
if err != nil {
return fmt.Errorf("can't open database: %s", err)
}
defer context.database.Close()
context.packagePool = files.NewPackagePool(utils.Config.RootDir)
context.publishedStorage = files.NewPublishedStorage(utils.Config.RootDir)
return nil
}