func DownloadLatestRelease(repository string) string { client := github.NewClient(nil) releases, _, err := client.Repositories.ListReleases("appstax", repository, nil) fail.Handle(err) if len(releases) == 0 { fail.Handle(errors.New("No releases found for '" + repository + "'")) } latest := releases[0] version := *latest.TagName log.Debugf("Latest release: %s", version) path := dir() + repository + "/" + version + "/" if _, err := os.Stat(path); err == nil { log.Debugf("Release already downloaded at %s", path) return path } asset, err := getAssetByName(latest, repository+".zip") fail.Handle(err) err = DownloadAndUnzip(*asset.BrowserDownloadURL, path) fail.Handle(err) return path }
func ReadSessionID() string { sessionID, _ := ioutil.ReadFile(dir() + "/session") if len(sessionID) == 0 { log.Debugf("Session id is empty") } else { log.Debugf("Read session id starting with %s", string(sessionID)[0:5]+"...") } return string(sessionID) }
func createServerDir() { dir := config.Read().ServerDir if _, err := os.Stat(dir); os.IsNotExist(err) { err = os.MkdirAll(dir, 0700) fail.Handle(err) log.Debugf("Created server directory '%s'", dir) } else { log.Debugf("Not creating server directory. '%s' already exists.", dir) } }
func Put(url string, data interface{}) ([]byte, *http.Response, error) { log.Debugf("HTTP PUT: %s", url) json, err := json.Marshal(data) fail.Handle(err) log.Debugf("HTTP PUT JSON: %s", json) client := &http.Client{} req, err := http.NewRequest("PUT", url, bytes.NewBuffer(json)) addHeaders(req) resp, err := client.Do(req) fail.Handle(err) return handleResult(resp, err) }
func selectSubdomain(appID string, required bool) { app, _ := account.GetAppByID(appID) log.Debugf("Subdomain app: %v", app) for { if required { app.HostingSubdomain = term.GetString("Choose a *.appstax.io subdomain for hosting") } else { term.Println("Choose a *.appstax.io subdomain for hosting:") term.Println("(Leave this blank if you wish to decide this later.)") app.HostingSubdomain = term.GetString("Subdomain") } if app.HostingSubdomain != "" { err1 := account.SaveApp(app) if err1 != nil { term.Println(err1.Error()) } err2 := account.AddCorsOrigin(appID, fmt.Sprintf("http://%s.appstax.io", app.HostingSubdomain)) if err2 != nil { term.Println(err2.Error()) } if err1 == nil && err2 == nil { term.Printf("Successfully configured %s.appstax.io\n", app.HostingSubdomain) return } } else if !required { return } } }
func DownloadAndUnzip(remoteUrl string, localPath string) error { log.Debugf("Downloading %s to %s", remoteUrl, localPath) tmp, err := download(remoteUrl) if err != nil { return err } log.Debugf("zip temporarily saved: %s", tmp) err = unzip(tmp, localPath) if err != nil { return err } return nil }
func Get(url string) ([]byte, *http.Response, error) { log.Debugf("HTTP GET: %s", url) client := &http.Client{} req, err := http.NewRequest("GET", url, nil) addHeaders(req) resp, err := client.Do(req) fail.Handle(err) return handleResult(resp, err) }
func getAssetByName(release github.RepositoryRelease, name string) (github.ReleaseAsset, error) { for _, asset := range release.Assets { log.Debugf("Available asset: '%s'", *asset.Name) if *asset.Name == name { return asset, nil } } return github.ReleaseAsset{}, errors.New("Asset '" + name + "' not found") }
func handleResult(resp *http.Response, err error) ([]byte, *http.Response, error) { defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) log.Debugf("HTTP response (%d): %s", resp.StatusCode, body) if err == nil && resp.StatusCode/100 == 2 { return body, resp, nil } else { return nil, nil, errors.New(getErrorMessage(resp, body, err)) } }
func Write(values map[string]string) { config := Read() config.AppKey = values["AppKey"] config.PublicDir = values["PublicDir"] config.ServerDir = values["ServerDir"] encoded, err := json.MarshalIndent(config, "", " ") fail.Handle(err) ioutil.WriteFile(fileName, encoded, 0644) log.Debugf("Wrote config file: %s", encoded) }
func Read() Config { var config Config dat, err := ioutil.ReadFile(FilePath()) if err != nil { log.Debugf("Could not find appstax.conf") } else { err = json.Unmarshal(dat, &config) fail.Handle(err) } insertDefaults(&config) return config }
func copy(src string, dst string) { log.Debugf("Copy '%s' to '%s'", src, dst) filepath.Walk(src, func(srcPath string, fileInfo os.FileInfo, err error) error { fail.Handle(err) if !fileInfo.IsDir() { log.Debugf("Copying file from '%s'", srcPath) dstPath := filepath.Join(dst, srcPath[len(src):]) log.Debugf("... to destination '%s'", dstPath) srcFile, err := os.Open(srcPath) fail.Handle(err) defer srcFile.Close() os.MkdirAll(filepath.Dir(dstPath), 0755) dstFile, err := os.Create(dstPath) fail.Handle(err) defer dstFile.Close() _, err = io.Copy(dstFile, srcFile) fail.Handle(err) } return nil }) }
func Install(template Template) { if template.Repository == "" { return } releasePath := download.DownloadLatestRelease(template.Repository) log.Debugf("Downloaded release path: %s", releasePath) sourcePath := filepath.Join(releasePath, template.SourcePath) copy(sourcePath, config.ResolvePath(template.DestinationPath)) insertAppKey(template) }
func ShowProgressBar(totalBytes int64) *pb.ProgressBar { log.Debugf("Creating progress bar for %d bytes", totalBytes) bar := pb.New64(totalBytes) bar.SetMaxWidth(70) bar.ShowCounters = false bar.ShowSpeed = true bar.SetUnits(pb.U_BYTES) bar.Format("[##-]") bar.Prefix(" ") bar.Start() return bar }
func addAllToArchive(fullRootPath string, tarWriter *tar.Writer) error { log.Debugf("Creating archive by walking from root path %s", fullRootPath) return filepath.Walk(fullRootPath, func(path string, fileInfo os.FileInfo, err error) error { if err != nil { return err } if !fileInfo.IsDir() && fileInfo.Name()[:1] != "." { err := addFileToArchive(path, path[len(fullRootPath+"/"):], tarWriter, fileInfo) if err != nil { return err } } return nil }) }
func PostFile(url string, path string, progressWriter io.Writer) ([]byte, *http.Response, error) { log.Debugf("HTTP POST FILE: %s", url) file, err := os.Open(path) fail.Handle(err) defer file.Close() fileReader := bufio.NewReader(file) teeReader := io.TeeReader(fileReader, progressWriter) client := &http.Client{} req, err := http.NewRequest("POST", url, teeReader) addHeaders(req) resp, err := client.Do(req) fail.Handle(err) return handleResult(resp, err) }
func DoOpen(c *cli.Context) { useOptions(c) dest := c.Args().First() log.Debugf("Open destination: %s", dest) url := "" message := "" switch dest { default: fallthrough case "deployed": app, err := account.GetCurrentApp() if err != nil { message = "Sorry, could not find a deployed app to open." } url = "http://" + app.HostingSubdomain + ".appstax.io" break case "admin": url = "http://appstax.com/admin/#/dashboard" break case "local": url = "http://localhost:9000/" break } if url != "" { term.Printf("Opening %s in your browser.\n", url) err := open.Start(url) if err != nil { message = "Ooops! Something went wrong." } } if message != "" { term.Section() term.Println(message) } }
func WriteAccountID(accountID string) { path := dir() + "/account" err := ioutil.WriteFile(path, []byte(accountID), 0644) fail.Handle(err) log.Debugf("Wrote account id to %s", path) }
func WriteUserID(userID string) { path := dir() + "/user" err := ioutil.WriteFile(path, []byte(userID), 0644) fail.Handle(err) log.Debugf("Wrote user id to %s", path) }
func WriteSessionID(sessionID string) { path := dir() + "/session" err := ioutil.WriteFile(path, []byte(sessionID), 0644) fail.Handle(err) log.Debugf("Wrote session id to %s", path) }