Skip to content

CLI instance is now created and initialized lazily #1615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions cli/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,41 @@ import (
)

var tr = i18n.Tr
var inst *rpc.Instance = nil
var instanceInitialized = false

// CreateAndInit return a new initialized instance.
// If Create fails the CLI prints an error and exits since
// to execute further operations a valid Instance is mandatory.
// If Init returns errors they're printed only.
func CreateAndInit() *rpc.Instance {
instance, err := Create()
if instanceInitialized {
return inst
}
var err error
inst, err = Create()
if err != nil {
feedback.Errorf(tr("Error creating instance: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
for _, err := range Init(instance) {
for _, err := range Init(inst) {
feedback.Errorf(tr("Error initializing instance: %v"), err)
}
return instance
return inst
}

// Create and return a new Instance.
func Create() (*rpc.Instance, error) {
if inst != nil {
// Instance already exists, no need to create a new one
return inst, nil
}
res, err := commands.Create(&rpc.CreateRequest{})
if err != nil {
return nil, err
}
return res.Instance, nil
inst = res.Instance
return inst, nil
}

// Init initializes instance by loading installed libraries and platforms.
Expand All @@ -65,8 +76,13 @@ func Create() (*rpc.Instance, error) {
func Init(instance *rpc.Instance) []error {
errs := []error{}

if instanceInitialized {
// Instance has already been initialized, nothing to do
return errs
}

// In case the CLI is executed for the first time
if err := FirstUpdate(instance); err != nil {
if err := firstUpdate(instance); err != nil {
return append(errs, err)
}

Expand Down Expand Up @@ -96,9 +112,9 @@ func Init(instance *rpc.Instance) []error {
return errs
}

// FirstUpdate downloads libraries and packages indexes if they don't exist.
// firstUpdate downloads libraries and packages indexes if they don't exist.
// This ideally is only executed the first time the CLI is run.
func FirstUpdate(instance *rpc.Instance) error {
func firstUpdate(instance *rpc.Instance) error {
// Gets the data directory to verify if library_index.json and package_index.json exist
dataDir := paths.New(configuration.Settings.GetString("directories.data"))

Expand Down Expand Up @@ -158,7 +174,7 @@ func CreateInstanceAndRunFirstUpdate() *rpc.Instance {
// to make it work correctly, we must do this explicitly in this command since
// we must use instance.Create instead of instance.CreateAndInit for the
// reason stated above.
if err := FirstUpdate(inst); err != nil {
if err := firstUpdate(inst); err != nil {
feedback.Errorf(tr("Error updating indexes: %v"), status)
os.Exit(errorcodes.ErrGeneric)
}
Expand Down