package workflowhelpers

import (
	"time"
)

type userContext interface {
	SetCfHomeDir() (string, string)
	UnsetCfHomeDir(string, string)
	Login()
	Logout()
	TargetSpace()
}

func AsUser(uc userContext, timeout time.Duration, actions func()) {
	originalCfHomeDir, currentCfHomeDir := uc.SetCfHomeDir()
	uc.Login()
	defer uc.Logout()
	defer uc.UnsetCfHomeDir(originalCfHomeDir, currentCfHomeDir)

	uc.TargetSpace()
	actions()
}

package workflowhelpers

import (
	"time"
)

type userContext interface {
	SetCfHomeDir() (string, string)
	UnsetCfHomeDir(string, string)
	Login()
	Logout()
	TargetSpace()
}

var AsUser = func(uc userContext, timeout time.Duration, actions func()) {
	originalCfHomeDir, currentCfHomeDir := uc.SetCfHomeDir()
	uc.Login()
	defer func() {
		uc.Logout()
		uc.UnsetCfHomeDir(originalCfHomeDir, currentCfHomeDir)
	}()

	uc.TargetSpace()
	actions()
}

--- 
+++ 
@@ -12,13 +12,11 @@
 	TargetSpace()
 }
 
-var AsUser = func(uc userContext, timeout time.Duration, actions func()) {
+func AsUser(uc userContext, timeout time.Duration, actions func()) {
 	originalCfHomeDir, currentCfHomeDir := uc.SetCfHomeDir()
 	uc.Login()
-	defer func() {
-		uc.Logout()
-		uc.UnsetCfHomeDir(originalCfHomeDir, currentCfHomeDir)
-	}()
+	defer uc.Logout()
+	defer uc.UnsetCfHomeDir(originalCfHomeDir, currentCfHomeDir)
 
 	uc.TargetSpace()
 	actions()
Refactor variable declaration as function

Signed-off-by: Alexander Berezovsky <a12e46294a7d630b60bbba114755367d6bafe2fb@pivotal.io>
