func updateAppInfoToLocal(appInfo *AppInfo) error { var jsonObj *simplejson.Json infoPath := filepath.Join(utils.HomeDir(), ".leancloud", "app_keys") content, err := ioutil.ReadFile(infoPath) if err != nil { if !os.IsNotExist(err) { return err } jsonObj = simplejson.New() } else { jsonObj, err = simplejson.NewJson([]byte(content)) if err != nil { return err } } jsonObj.Set(appInfo.AppID, map[string]string{ "appKey": appInfo.AppKey, "masterKey": appInfo.MasterKey, }) body, err := jsonObj.EncodePretty() if err != nil { return err } return ioutil.WriteFile(infoPath, body, 0600) }
func getAppInfoFromLocal(appID string) (*AppInfo, error) { infoPath := filepath.Join(utils.HomeDir(), ".leancloud", "app_keys") content, err := ioutil.ReadFile(infoPath) if err != nil { if os.IsNotExist(err) { return nil, ErrAppInfoNotFound } return nil, err } jsonObj, err := simplejson.NewJson(content) if err != nil { return nil, err } _, err = jsonObj.Get(appID).Map() if err != nil { return nil, ErrAppInfoNotFound } return &AppInfo{ AppID: appID, AppKey: jsonObj.Get(appID).Get("appKey").MustString(), MasterKey: jsonObj.Get(appID).Get("masterKey").MustString(), }, nil }
func getCookies() ([]*http.Cookie, error) { content, err := ioutil.ReadFile(filepath.Join(utils.HomeDir(), ".leancloud", "cookies")) if err != nil { return nil, err } cookies := cookieparser.Parse(string(content)) return cookies, nil }
// saveCookies saves the cookies to `${HOME}/.leancloud/cookies` func saveCookies(cookies []*http.Cookie) error { os.Mkdir(filepath.Join(utils.HomeDir(), ".leancloud"), 0700) content := []byte(cookieparser.ToString(cookies)) return ioutil.WriteFile(filepath.Join(utils.HomeDir(), ".leancloud", "cookies"), content, 0600) }