flame/tools.go
func EnsureApplication(store *coal.Store, name, key, secret string) (string, error) {
	// copy store
	s := store.Copy()
	defer s.Close()

	// count main applications
	var apps []Application
	err := s.C(&Application{}).Find(bson.M{
		coal.F(&Application{}, "Name"): name,
	}).All(&apps)
	if err != nil {
		return "", err
	}

	// check existence
	if len(apps) > 1 {
		return "", errors.New("to many applications with that name")
	} else if len(apps) == 1 {
		return apps[0].Key, nil
	}

	// application is missing

	// create application
	app := coal.Init(&Application{}).(*Application)
	app.Key = key
	app.Name = name
	app.Secret = secret

	// validate model
	err = app.Validate()
	if err != nil {
		return "", err
	}

	// save application
	err = s.C(app).Insert(app)
	if err != nil {
		return "", err
	}

	return app.Key, nil
}
func EnsureApplication(store *coal.Store, name, key, secret string) (string, error) {
	// copy store
	s := store.Copy()
	defer s.Close()

	// count main applications
	var apps []Application
	err := s.C(&Application{}).Find(bson.M{
		coal.F(&Application{}, "Name"): name,
	}).All(&apps)
	if err != nil {
		return "", err
	}

	// check existence
	if len(apps) > 1 {
		return "", errors.New("to many applications with that name")
	} else if len(apps) == 1 {
		return apps[0].Key, nil
	}

	// application is missing

	// create application
	app := coal.Init(&Application{}).(*Application)
	app.Key = key
	app.Name = name
	app.Secret = secret

	// validate model
	err = app.Validate()
	if err != nil {
		return "", err
	}

	// save application
	err = s.C(app).Insert(app)
	if err != nil {
		return "", err
	}

	return app.Key, nil
}
// EnsureApplication will ensure that an application with the provided name
// exists and returns its key.