func getTransaction(context *hookstate.Context) (*configstate.Transaction, error) { context.Lock() defer context.Unlock() // Extract the transaction from the context. If none, make one and cache it // in the context. transaction, ok := context.Cached(cachedTransaction{}).(*configstate.Transaction) if !ok { var err error transaction, err = configstate.NewTransaction(context.State()) if err != nil { return nil, fmt.Errorf("cannot create transaction: %s", err) } context.OnDone(func() error { transaction.Commit() return nil }) context.Cache(cachedTransaction{}, transaction) } return transaction, nil }