Esempio n. 1
0
func (h *herokuLink) createNewLink(e *parsecli.Env, herokuAppName string) (string, error) {
	var l parsecli.Login
	_, err := l.AuthUserWithToken(e, true)
	if err != nil {
		return "", stackerr.Wrap(err)
	}

	req, err := http.NewRequest(
		"POST",
		"herokuLink",
		ioutil.NopCloser(
			jsonpipe.Encode(
				map[string]string{"herokuAppName": herokuAppName},
			),
		),
	)
	if err != nil {
		return "", stackerr.Wrap(err)
	}
	req.Header = make(http.Header)
	req.Header.Set("X-Parse-Application-Id", h.parseAppID)
	req.Header.Set("X-Parse-Account-Key", l.Credentials.Token)

	resp, err := e.ParseAPIClient.RoundTrip(req)
	if err != nil {
		if herokuAppNameTaken(err) {
			fmt.Fprintln(e.Err, `
Please provide a unique name that might not already be taken on Heroku.
`)
		}
		return "", stackerr.Wrap(err)
	}
	result := &struct {
		Name string `json:"name"`
		ID   string `json:"id"`
	}{}
	if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
		return "", stackerr.Wrap(err)
	}
	if result.Name != herokuAppName || result.ID == "" {
		return "", stackerr.New("could not create heroku app link")
	}
	return result.ID, nil
}
Esempio n. 2
0
func (h *herokuLink) getAppLinks(e *parsecli.Env) (nameIDs, error) {
	var l parsecli.Login
	_, err := l.AuthUserWithToken(e, true)
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequest(
		"GET",
		"herokuLink",
		nil,
	)
	if err != nil {
		return nil, stackerr.Wrap(err)
	}
	req.Header = make(http.Header)
	req.Header.Set("X-Parse-Application-Id", h.parseAppID)
	req.Header.Set("X-Parse-Account-Key", l.Credentials.Token)

	resp, err := e.ParseAPIClient.RoundTrip(req)
	if err != nil {
		return nil, stackerr.Wrap(err)
	}
	type herokuAppID struct {
		ID string `json:"herokuAppId,omitempty"`
	}
	result := &struct {
		Apps []*herokuAppID `json:"herokuApps"`
	}{}
	if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
		return nil, stackerr.Wrap(err)
	}
	var ret []string
	for _, herokuApp := range result.Apps {
		if herokuApp.ID != "" {
			ret = append(ret, herokuApp.ID)
		}
	}
	names, deletedIDs, err := h.herokuAppNames(ret, e)
	h.removeStaleLinks(e, l.Credentials.Token, deletedIDs)
	return names, err
}