context.go 808 B

1234567891011121314151617181920212223242526272829303132333435
  1. package context
  2. import (
  3. gocontext "context"
  4. "github.com/sirupsen/logrus"
  5. "golang.org/x/net/context"
  6. )
  7. type currentContextKey struct{}
  8. func WithCurrentContext(ctx gocontext.Context, configName string, contextName string) (context.Context, error) {
  9. config, err := LoadConfigFile(configName, "config.json")
  10. if err != nil {
  11. return ctx, err
  12. }
  13. currentContext := contextName
  14. if currentContext == "" {
  15. currentContext = config.CurrentContext
  16. }
  17. if currentContext == "" {
  18. currentContext = "default"
  19. }
  20. logrus.Debugf("Current context %q", currentContext)
  21. return context.WithValue(ctx, currentContextKey{}, currentContext), nil
  22. }
  23. // CurrentContext returns the current context name
  24. func CurrentContext(ctx context.Context) string {
  25. cc, _ := ctx.Value(currentContextKey{}).(string)
  26. return cc
  27. }