// Apply goes through the map of substitutions and replaces all instances of
// the keys with their respective values. It supports escaping substitutions
// with a leading '\'.
540253
func (e *Environment) Apply(data string) string {
	for key, val := range e.substitutions {
		matchKey := strings.Replace(key, `$`, `\$`, -1)
		replKey := strings.Replace(key, `$`, `$$`, -1)

		// "key" -> "val"
		data = regexp.MustCompile(`([^\\]|^)`+matchKey).ReplaceAllString(data, `${1}`+val)
		// "\key" -> "key"
		data = regexp.MustCompile(`\\`+matchKey).ReplaceAllString(data, replKey)
	}
	return data
}
1280035
func Apply(ctx context.Context, root string, r io.Reader, opts ...ApplyOpt) (int64, error) {
	root = filepath.Clean(root)

	var options ApplyOptions
	for _, opt := range opts {
		if err := opt(&options); err != nil {
			return 0, errors.Wrap(err, "failed to apply option")
		}
	}
	if options.Filter == nil {
		options.Filter = all
	}

	return apply(ctx, root, tar.NewReader(r), options)
}
561636
func (r replacer) Set(key, value string) {
	r.replacements["{"+key+"}"] = value
}
734723
func (cli *DockerCli) Apply(ops ...DockerCliOption) error {
	for _, op := range ops {
		if err := op(cli); err != nil {
			return err
		}
	}
	return nil
}
1365175
func (u *VariableUpdate) Apply(m *Variable) error {
	if u.Name != "" {
		m.Name = u.Name
	}

	if u.Selected != nil {
		m.Selected = u.Selected
	}

	if u.Arguments != nil {
		m.Arguments = u.Arguments
	}

	if u.Description != "" {
		m.Description = u.Description
	}

	return nil
}
853111
func substitute(
	obj map[string]interface{}, tokenMatcher *regexp.Regexp, replacer func(key string) string,
) map[string]interface{} {
	newObj := make(map[string]interface{})
	for k, v := range obj {
		switch v := v.(type) {
		case string:
			newObj[k] = tokenMatcher.ReplaceAllStringFunc(v, replacer)
		case []string:
			newV := make([]string, len(v))
			for i := 0; i < len(v); i++ {
				newV[i] = tokenMatcher.ReplaceAllStringFunc(v[i], replacer)
			}
			newObj[k] = newV
		default:
			newObj[k] = v
		}
	}
	return newObj
}
674526
func (p *Proxy) Apply(dbName string, i interface{}) {
	p.ApplyLazy(gsd.Lazy(dbName), i)
}
439860
func (s *Logging) Apply(c Config) error {
	err := s.SetFormat(c.Format)
	if err != nil {
		return err
	}

	if c.LogSpec == "" {
		c.LogSpec = os.Getenv("FABRIC_LOGGING_SPEC")
	}
	if c.LogSpec == "" {
		c.LogSpec = defaultLevel.String()
	}

	err = s.LoggerLevels.ActivateSpec(c.LogSpec)
	if err != nil {
		return err
	}

	if c.Writer == nil {
		c.Writer = os.Stderr
	}
	s.SetWriter(c.Writer)

	var formatter logging.Formatter
	switch s.Encoding() {
	case JSON, LOGFMT:
		formatter = SetFormat(defaultFormat)
	default:
		formatter = SetFormat(c.Format)
	}

	InitBackend(formatter, c.Writer)

	return nil
}
1179385
func (c Context) Replace(input, find, replacement string) string {
	return strings.Replace(input, find, replacement, -1)
}
7996
func (actor Actor) Apply(config ApplicationConfig, progressBar ProgressBar) (<-chan ApplicationConfig, <-chan Event, <-chan Warnings, <-chan error) {
	configStream := make(chan ApplicationConfig)
	eventStream := make(chan Event)
	warningsStream := make(chan Warnings)
	errorStream := make(chan error)

	go func() {
		log.Debug("starting apply go routine")
		defer close(configStream)
		defer close(eventStream)
		defer close(warningsStream)
		defer close(errorStream)

		var event Event
		var warnings Warnings
		var err error

		eventStream <- SettingUpApplication
		if config.UpdatingApplication() {
			config, event, warnings, err = actor.UpdateApplication(config)
		} else {
			config, event, warnings, err = actor.CreateApplication(config)
		}

		warningsStream <- warnings
		if err != nil {
			errorStream <- err
			return
		}
		eventStream <- event
		log.Debugf("desired application: %#v", config.DesiredApplication)

		if config.NoRoute {
			if len(config.CurrentRoutes) > 0 {
				eventStream <- UnmappingRoutes
				config, warnings, err = actor.UnmapRoutes(config)
				warningsStream <- warnings
				if err != nil {
					errorStream <- err
					return
				}
			}
		} else {
			eventStream <- CreatingAndMappingRoutes

			var createdRoutes bool
			config, createdRoutes, warnings, err = actor.CreateRoutes(config)
			warningsStream <- warnings
			if err != nil {
				errorStream <- err
				return
			}
			if createdRoutes {
				log.Debugf("updated desired routes: %#v", config.DesiredRoutes)
				eventStream <- CreatedRoutes
			}

			var boundRoutes bool
			config, boundRoutes, warnings, err = actor.MapRoutes(config)
			warningsStream <- warnings
			if err != nil {
				errorStream <- err
				return
			}
			if boundRoutes {
				log.Debugf("updated desired routes: %#v", config.DesiredRoutes)
				eventStream <- BoundRoutes
			}
		}

		if len(config.CurrentServices) != len(config.DesiredServices) {
			eventStream <- ConfiguringServices
			config, _, warnings, err = actor.BindServices(config)
			warningsStream <- warnings
			if err != nil {
				errorStream <- err
				return
			}

			log.Debugf("bound desired services: %#v", config.DesiredServices)
			eventStream <- BoundServices
		}

		if config.DropletPath != "" {
			for count := 0; count < PushRetries; count++ {
				warnings, err = actor.UploadDroplet(config, config.DropletPath, progressBar, eventStream)
				warningsStream <- warnings
				if _, ok := err.(ccerror.PipeSeekError); ok {
					eventStream <- RetryUpload
				} else {
					break
				}
			}

			if err != nil {
				if e, ok := err.(ccerror.PipeSeekError); ok {
					errorStream <- actionerror.UploadFailedError{Err: e.Err}
				} else {
					errorStream <- err
				}
				return
			}
		} else if config.DesiredApplication.DockerImage == "" {
			eventStream <- ResourceMatching
			config, warnings = actor.SetMatchedResources(config)
			warningsStream <- warnings

			if len(config.UnmatchedResources) > 0 {
				var archivePath string
				archivePath, err = actor.CreateArchive(config)
				if err != nil {
					errorStream <- err
					os.RemoveAll(archivePath)
					return
				}
				eventStream <- CreatingArchive
				defer os.RemoveAll(archivePath)

				for count := 0; count < PushRetries; count++ {
					warnings, err = actor.UploadPackageWithArchive(config, archivePath, progressBar, eventStream)
					warningsStream <- warnings
					if _, ok := err.(ccerror.PipeSeekError); ok {
						eventStream <- RetryUpload
					} else {
						break
					}
				}

				if err != nil {
					if e, ok := err.(ccerror.PipeSeekError); ok {
						errorStream <- actionerror.UploadFailedError{Err: e.Err}
					} else {
						errorStream <- err
					}
					return
				}
			} else {
				eventStream <- UploadingApplication
				warnings, err = actor.UploadPackage(config)
				warningsStream <- warnings
				if err != nil {
					errorStream <- err
					return
				}
			}
		} else {
			log.WithField("docker_image", config.DesiredApplication.DockerImage).Debug("skipping file upload")
		}

		configStream <- config

		log.Debug("completed apply")
		eventStream <- Complete
	}()

	return configStream, eventStream, warningsStream, errorStream
}
838924
func (s *SchemaValidator) Applies(source interface{}, kind reflect.Kind) bool {
	_, ok := source.(*spec.Schema)
	return ok
}
495557
func (t *Template) Apply(s string) (string, error) {
	var out bytes.Buffer
	tmpl, err := template.New("tmpl").
		Option("missingkey=error").
		Funcs(template.FuncMap{
			"time": func(s string) string {
				return time.Now().UTC().Format(s)
			},
		}).
		Parse(s)
	if err != nil {
		return "", err
	}

	err = tmpl.Execute(&out, t.fields)
	return out.String(), err
}
125422
func (p Patch) Apply(doc []byte) ([]byte, error) {
	return p.ApplyIndent(doc, "")
}
1335612
func apply(ctx context.Context, root string, tr *tar.Reader, options ApplyOptions) (size int64, err error) {
	return applyNaive(ctx, root, tr, options)
}
517906
func applyMap(dst reflect.Value, src reflect.Value) error {
	dstKeyType := dst.Type().Key()
	dstValType := dst.Type().Elem()

	dst.Set(reflect.MakeMap(dst.Type()))
	for _, k := range src.MapKeys() {
		if k.Type().Kind() == reflect.Interface {
			k = k.Elem()
		}
		var err error
		if k, err = convert(k, dstKeyType); err != nil {
			return fmt.Errorf("key '%v' invalid type: %s", k.Interface(), err)
		}

		v := src.MapIndex(k)
		if v, err = convert(v, dstValType); err != nil {
			return fmt.Errorf("value for key '%v' invalid type: %s", k.Interface(), err)
		}
		dst.SetMapIndex(k, v)
	}
	return nil
}
473591
func (cm *Validator) Apply(configEnv *cb.ConfigEnvelope) error {
	cm.AppliedConfigUpdateEnvelope = configEnv
	return cm.ApplyVal
}
912362
func (f *DurationFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.DurationFlag.Apply(set)
}