Skip to content

Commit

Permalink
engine: resources: pkg: Add a small helper for simple installs
Browse files Browse the repository at this point in the history
Add this utility function for doing single package installs.
  • Loading branch information
purpleidea committed Sep 25, 2024
1 parent 80e8c9c commit 8aac770
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions engine/resources/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,36 @@ func ReturnSvcInFileList(fileList []string) []string {
func stateIsVersion(state string) bool {
return (state != PkgStateInstalled && state != PkgStateUninstalled && state != PkgStateNewest) // must be a ver. string
}

// InstallOnePackage is a helper function which uses the minimum resource
// mechanics to install a single package. Keep this up-to-date with the resource
// internals since we may make some concessions in this helper function in that
// we don't call the full API.
func InstallOnePackage(ctx context.Context, name string) error {
res, err := engine.NewNamedResource("pkg", name)
if err != nil {
return err
}
pkg, ok := res.(*PkgRes)
if !ok {
return fmt.Errorf("error casting PkgRes")
}
pkg.State = PkgStateInstalled

init := &engine.Init{
Debug: false,
Logf: func(format string, v ...interface{}) {
// noop
},
}
if err := res.Init(init); err != nil {
return err
}

apply := true
if _, err := res.CheckApply(ctx, apply); err != nil {
return err
}

return res.Cleanup()
}

0 comments on commit 8aac770

Please sign in to comment.