本文整理匯總了Golang中github.com/cloudfoundry-incubator/pat/context.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: checkLoggedIn
func checkLoggedIn(ctx context.Context, then func(token string) error) error {
if _, exists := ctx.GetString("token"); !exists {
return errors.New("Error: not logged in")
}
token, _ := ctx.GetString("token")
return then(token)
}
示例2: uploadAppBitsSuccessfully
func (r *rest) uploadAppBitsSuccessfully(ctx context.Context, appUri string, then func() error) error {
apiEndpoint, _ := ctx.GetString("apiEndpoint")
return checkLoggedIn(ctx, func(token string) error {
return withGeneratedAppBits(func(b *bytes.Buffer, m *multipart.Writer) error {
return r.MultipartPutSuccessfully(token, m, fmt.Sprintf("%s%s/bits", apiEndpoint, appUri), b, nil, func(reply Reply) error {
return then()
})
})
})
}
示例3: start
func (r *rest) start(ctx context.Context, appUri string, then func() error) error {
apiEndpoint, _ := ctx.GetString("apiEndpoint")
input := make(map[string]interface{})
input["state"] = "STARTED"
return checkLoggedIn(ctx, func(token string) error {
return r.PutSuccessfully(token, fmt.Sprintf("%s%s", apiEndpoint, appUri), input, nil, func(reply Reply) error {
return then()
})
})
}
示例4: Delete
func Delete(ctx context.Context) error {
appNames, _ := ctx.GetString("appNames")
if appNames == "" {
return errors.New("No app to delete")
}
appNamesArray := strings.Split(appNames, ",")
appNameToDelete := appNamesArray[len(appNamesArray)-1]
appNames = strings.Replace(appNames, ","+appNameToDelete, "", -1)
appNames = strings.Replace(appNames, appNameToDelete, "", -1)
ctx.PutString("appNames", appNames)
return expectCfToSay("Deleting app", "delete", appNameToDelete, "-f")
}
示例5: Dummy
func Dummy(ctx context.Context) error {
guid, _ := uuid.NewV4()
appName := "pats-" + guid.String()
appNames, _ := ctx.GetString("appNames")
if appNames != "" {
appNames += fmt.Sprintf(",%s", appName)
} else {
appNames = appName
}
ctx.PutString("appNames", appNames)
time.Sleep(time.Duration(random(1, 5)) * time.Second)
return nil
}
示例6: PopulateAppContext
func PopulateAppContext(appPath string, manifestPath string, ctx context.Context) error {
normalizedAppPath, err := normalizePath(appPath)
if err != nil {
return err
}
ctx.PutString("app", normalizedAppPath)
normalizedManifestPath, err := normalizePath(manifestPath)
if err != nil {
return err
}
ctx.PutString("app:manifest", normalizedManifestPath)
return nil
}
示例7: createAppSuccessfully
func (r *rest) createAppSuccessfully(ctx context.Context, thenWithLocation func(appUri string) error) error {
apiEndpoint, _ := ctx.GetString("apiEndpoint")
space_guid, _ := ctx.GetString("space_guid")
uuid, _ := uuid.NewV4()
createApp := struct {
Name string `json:"name"`
SpaceGuid string `json:"space_guid"`
}{uuid.String(), space_guid}
return checkLoggedIn(ctx, func(token string) error {
return r.PostSuccessfully(token, fmt.Sprintf("%s/v2/apps", apiEndpoint), createApp, nil, func(reply Reply) error {
return thenWithLocation(reply.Location)
})
})
}
示例8: ExecuteConcurrently
func ExecuteConcurrently(schedule <-chan int, tasks <-chan func(context.Context), workloadCtx context.Context) {
var wg sync.WaitGroup
indexCounter := 0
for increment := range schedule {
for i := 0; i < increment; i++ {
wg.Add(1)
go func(t <-chan func(context.Context), ctx context.Context) {
defer wg.Done()
for task := range t {
ctx.PutInt("iterationIndex", indexCounter)
indexCounter++
task(ctx)
}
}(tasks, workloadCtx.Clone())
}
}
wg.Wait()
}
示例9: checkTargetted
func checkTargetted(ctx context.Context, then func(loginEndpoint string, apiEndpoint string) error) error {
if _, exists := ctx.GetString("loginEndpoint"); !exists {
return errors.New("Not targetted")
}
if _, exists := ctx.GetString("apiEndpoint"); !exists {
return errors.New("Not targetted")
}
apiEndpoint, _ := ctx.GetString("apiEndpoint")
loginEndpoint, _ := ctx.GetString("loginEndpoint")
return then(loginEndpoint, apiEndpoint)
}
示例10: trackAppStart
func (r *rest) trackAppStart(ctx context.Context, appUri string) error {
return checkLoggedIn(ctx, func(token string) error {
apiEndpoint, _ := ctx.GetString("apiEndpoint")
for {
decoded := make(map[string]interface{})
reply := r.client.Get(token, fmt.Sprintf("%s%s/instances", apiEndpoint, appUri), nil, &decoded)
if reply.Code < 400 || decoded["error_code"] != "CF-NotStaged" {
if decoded["error_code"] != nil {
return errors.New("App Failed to Stage")
}
break
}
time.Sleep(2 * time.Second)
}
return nil
})
}
示例11: GenerateAndPush
func GenerateAndPush(ctx context.Context) error {
pathToApp, _ := ctx.GetString("app")
pathToManifest, _ := ctx.GetString("app:manifest")
guid, _ := uuid.NewV4()
rand.Seed(time.Now().UTC().UnixNano())
salt := strconv.FormatInt(rand.Int63(), 10)
dstDir := path.Join(os.TempDir(), salt)
defer os.RemoveAll(dstDir)
err := CopyAndReplaceText(pathToApp, dstDir, "$RANDOM_TEXT", salt)
if err != nil {
return err
}
if pathToManifest == "" {
return expectCfToSay("App started", "push", "pats-"+guid.String(), "-m", "64M", "-p", pathToApp)
} else {
return expectCfToSay("App started", "push", "pats-"+guid.String(), "-p", pathToApp, "-f", pathToManifest)
}
}
示例12: Target
func (r *rest) Target(ctx context.Context) error {
var target string
if _, ok := ctx.GetString("rest:target"); ok {
target, _ = ctx.GetString("rest:target")
} else {
return errors.New("argument rest:target does not exist")
}
body := &TargetResponse{}
return r.GetSuccessfully("", target+"/v2/info", nil, body, func(reply Reply) error {
ctx.PutString("loginEndpoint", body.LoginEndpoint)
ctx.PutString("apiEndpoint", target)
return nil
})
}
示例13: Push
func Push(ctx context.Context) error {
guid, _ := uuid.NewV4()
pathToApp, _ := ctx.GetString("app")
pathToManifest, _ := ctx.GetString("app:manifest")
appName := "pats-" + guid.String()
appNames, _ := ctx.GetString("appNames")
if appNames != "" {
appNames += fmt.Sprintf(",%s", appName)
} else {
appNames = appName
}
ctx.PutString("appNames", appNames)
if pathToManifest == "" {
return expectCfToSay("App started", "push", appName, "-m", "64M", "-p", pathToApp)
} else {
return expectCfToSay("App started", "push", appName, "-p", pathToApp, "-f", pathToManifest)
}
}
示例14: targetSpace
func (r *rest) targetSpace(ctx context.Context) error {
apiEndpoint, _ := ctx.GetString("apiEndpoint")
var space string
if _, ok := ctx.GetString("rest:space"); ok {
space, _ = ctx.GetString("rest:space")
} else {
return errors.New("argument rest:space does not exist")
}
replyBody := &SpaceResponse{}
return checkLoggedIn(ctx, func(token string) error {
return r.GetSuccessfully(token, fmt.Sprintf("%s/v2/spaces?q=name:%s", apiEndpoint, space), nil, replyBody, func(reply Reply) error {
return checkSpaceExists(replyBody, func() error {
ctx.PutString("space_guid", replyBody.Resources[0].Metadata.Guid)
return nil
})
})
})
}
示例15:
package context_test
import (
"github.com/cloudfoundry-incubator/pat/context"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Context map", func() {
var (
localContext context.Context
)
JustBeforeEach(func() {
localContext = context.New()
})
Context("String values in context map", func() {
It("uses a key to identify store fields", func() {
localContext.PutString("key1", "abc")
localContext.PutString("key2", "123")
result, exists := localContext.GetString("key1")
Ω(result).Should(Equal("abc"))
Ω(exists).Should(Equal(true))
result, exists = localContext.GetString("key2")
Ω(result).Should(Equal("123"))
Ω(exists).Should(Equal(true))
})