Skip to content

Commit

Permalink
refactor(packmanager): streamline package manager initialization
Browse files Browse the repository at this point in the history
Create a singleton for the umbrella package manager and use it instead
of global package-scoped maps. Use more dependency injection.

Move the scattered init() calls to RegisterPackageManager into one
place. Create a bootstrap package for handling flag/zip registration.

Use the bootstrapping package in main(). Propagate errors up and report
them if initialization fails.

This should make things a bit more testable in the future.

GitHub-Fixes: #425

Signed-off-by: Jakub Ciolek <jakub@ciolek.dev>
  • Loading branch information
jake-ciolek committed Aug 15, 2023
1 parent 047e915 commit a5f93a0
Show file tree
Hide file tree
Showing 27 changed files with 271 additions and 147 deletions.
11 changes: 6 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (

zip "api.zip"
"k8s.io/apimachinery/pkg/api/resource"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

machinev1alpha1 "kraftkit.sh/api/machine/v1alpha1"
networkv1alpha1 "kraftkit.sh/api/network/v1alpha1"
volumev1alpha1 "kraftkit.sh/api/volume/v1alpha1"
)

func init() {
utilruntime.Must(zip.Register(
gob.Register(resource.Quantity{})
}

func RegisterSchemes() error {
return zip.Register(
machinev1alpha1.AddToScheme,
networkv1alpha1.AddToScheme,
volumev1alpha1.AddToScheme,
))

gob.Register(resource.Quantity{})
)
}
5 changes: 2 additions & 3 deletions cmd/kraft/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ func New() *cobra.Command {
}

func (opts *Build) Pre(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

if len(args) == 0 {
opts.workdir, err = os.Getwd()
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ func New() *cobra.Command {
}

func (opts *Clean) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

opts.Platform = platform.PlatformByName(opts.Platform).String()

Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ func New() *cobra.Command {
}

func (opts *Fetch) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

opts.Platform = platform.PlatformByName(opts.Platform).String()

Expand Down
6 changes: 6 additions & 0 deletions cmd/kraft/kraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/bootstrap"
"kraftkit.sh/internal/cli"
kitupdate "kraftkit.sh/internal/update"
kitversion "kraftkit.sh/internal/version"
Expand Down Expand Up @@ -140,5 +141,10 @@ func main() {
}
}

if err := bootstrap.InitKraftkit(ctx); err != nil {
log.G(ctx).Errorf("could not init kraftkit: %v", err)
os.Exit(1)
}

cmdfactory.Main(ctx, cmd)
}
5 changes: 2 additions & 3 deletions cmd/kraft/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ func New() *cobra.Command {
}

func (opts *Menu) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

opts.Platform = platform.PlatformByName(opts.Platform).String()

Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ func New() *cobra.Command {
}

func (*List) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ func (opts *Pkg) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("the `--arch` and `--plat` options are not supported in addition to `--target`")
}

ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

opts.Platform = platform.PlatformByName(opts.Platform).String()

Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/pkg/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ func New() *cobra.Command {
}

func (opts *Pull) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

opts.Platform = platform.PlatformByName(opts.Platform).String()

Expand Down
11 changes: 7 additions & 4 deletions cmd/kraft/pkg/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ func New() *cobra.Command {
}

func (opts *Push) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down Expand Up @@ -111,7 +110,11 @@ func (opts *Push) Run(cmd *cobra.Command, args []string) error {

var pmananger packmanager.PackageManager
if opts.Format != "auto" {
pmananger = packmanager.PackageManagers()[pack.PackageFormat(opts.Format)]
umbrella, err := packmanager.PackageManagers()
if err != nil {
return err
}
pmananger = umbrella[pack.PackageFormat(opts.Format)]
if pmananger == nil {
return errors.New("invalid package format specified")
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ func New() *cobra.Command {
}

func (*Source) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/pkg/unsource/unsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ func New() *cobra.Command {
}

func (*Unsource) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ func New() *cobra.Command {
}

func (*Update) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/prepare/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ func New() *cobra.Command {
}

func (opts *Prepare) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

opts.Platform = platform.PlatformByName(opts.Platform).String()

Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/properclean/properclean.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ func New() *cobra.Command {
}

func (*ProperClean) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/kraft/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,19 @@ func (opts *Run) Pre(cmd *cobra.Command, _ []string) error {

if opts.RunAs == "" || !set.NewStringSet("kernel", "project").Contains(opts.RunAs) {
// Set use of the global package manager.
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)
}

if opts.RunAs != "" {
runners := runnersByName()
runners, err := runnersByName()
if err != nil {
return err
}
if _, ok = runners[opts.RunAs]; !ok {
choices := make([]string, len(runners))
i := 0
Expand Down Expand Up @@ -229,7 +232,10 @@ func (opts *Run) Run(cmd *cobra.Command, args []string) error {

var run runner
var errs []error
runners := runners()
runners, err := runners()
if err != nil {
return err
}

// Iterate through the list of built-in runners which sequentially tests and
// first test whether the --as flag has been set to force a specific runner or
Expand Down
20 changes: 14 additions & 6 deletions cmd/kraft/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,37 @@ type runner interface {
// runners is the list of built-in runners which are checked sequentially for
// capability. The first to test positive via Runnable is used with the
// controller.
func runners() []runner {
func runners() ([]runner, error) {
r := []runner{
&runnerLinuxu{},
&runnerKernel{},
&runnerProject{},
}

for _, pm := range packmanager.PackageManagers() {
umbrella, err := packmanager.PackageManagers()
if err != nil {
return nil, err
}

for _, pm := range umbrella {
r = append(r, &runnerPackage{
pm: pm,
})
}

return r
return r, nil
}

// runnersByName is a utility method that returns a map of the available runners
// such that their alias name can be quickly looked up.
func runnersByName() map[string]runner {
runners := runners()
func runnersByName() (map[string]runner, error) {
runners, err := runners()
if err != nil {
return nil, err
}
ret := make(map[string]runner, len(runners))
for _, runner := range runners {
ret[runner.String()] = runner
}
return ret
return ret, nil
}
5 changes: 2 additions & 3 deletions cmd/kraft/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ func New() *cobra.Command {
}

func (*Set) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kraft/unset/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ func New() *cobra.Command {
}

func (*Unset) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
cmd.SetContext(ctx)

return nil
}
Expand Down
Loading

0 comments on commit a5f93a0

Please sign in to comment.