Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
func PrepareArchive(rootPath string) (string, int64, error) {
	file, err := ioutil.TempFile("", "")
	fail.Handle(err)
	defer file.Close()
	fileWriter := bufio.NewWriter(file)
	defer fileWriter.Flush()
	gzipWriter, err := gzip.NewWriterLevel(fileWriter, gzip.BestCompression)
	fail.Handle(err)
	defer gzipWriter.Close()
	tarWriter := tar.NewWriter(gzipWriter)
	defer tarWriter.Close()

	fullRootPath, err := filepath.Abs(rootPath)
	fail.Handle(err)
	err = addAllToArchive(fullRootPath, tarWriter)
	if err != nil {
		return "", 0, err
	}

	tarWriter.Close()
	gzipWriter.Close()
	fileWriter.Flush()
	file.Close()

	fileInfo, err := os.Stat(file.Name())
	fail.Handle(err)
	return file.Name(), fileInfo.Size(), nil
}
Ejemplo n.º 3
0
func dir() string {
	home, err := homedir.Dir()
	fail.Handle(err)
	dir := home + "/.appstax/session"
	err = os.MkdirAll(dir, 0700)
	fail.Handle(err)
	return dir
}
Ejemplo n.º 4
0
func Post(url string, data interface{}) ([]byte, *http.Response, error) {
	log.Debugf("HTTP POST: %s", url)
	json, err := json.Marshal(data)
	fail.Handle(err)
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(json))
	addHeaders(req)
	resp, err := client.Do(req)
	fail.Handle(err)
	return handleResult(resp, err)
}
Ejemplo n.º 5
0
func insertAppKey(template Template) {
	if template.AppKeyInFile == "" {
		return
	}
	path := config.ResolvePath(template.AppKeyInFile)
	bytes, err := ioutil.ReadFile(path)
	fail.Handle(err)
	text := string(bytes)

	text = strings.Replace(text, "<<appstax-app-key>>", config.Read().AppKey, -1)
	err = ioutil.WriteFile(path, []byte(text), 0644)
	fail.Handle(err)
}
Ejemplo n.º 6
0
func addFileToArchive(filePath string, addPath string, tarWriter *tar.Writer, fileInfo os.FileInfo) error {
	addPath = filepath.ToSlash(addPath)

	if isSymlink(fileInfo) {
		link, err := filepath.EvalSymlinks(filePath)
		if err != nil {
			return err
		}
		filePath = link
		fileInfo, err = os.Lstat(filePath)
		if err != nil {
			return err
		}
	}

	header := new(tar.Header)
	header.Name = addPath
	header.Size = fileInfo.Size()
	header.Mode = int64(fileInfo.Mode())
	header.ModTime = fileInfo.ModTime()

	fileReader, err := os.Open(filePath)
	if err != nil {
		return err
	}
	defer fileReader.Close()
	err = tarWriter.WriteHeader(header)
	fail.Handle(err)
	_, err = io.Copy(tarWriter, fileReader)
	return err
}
Ejemplo n.º 7
0
func selectSubdomainIfNeeded() {
	app, err := account.GetCurrentApp()
	fail.Handle(err)
	if app.HostingSubdomain == "" {
		selectSubdomain(app.AppID, true)
	}
}
Ejemplo n.º 8
0
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)
}
Ejemplo n.º 9
0
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)
}
Ejemplo n.º 10
0
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)
	}
}
Ejemplo n.º 11
0
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)
}
Ejemplo n.º 12
0
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
	})
}
Ejemplo n.º 13
0
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
}
Ejemplo n.º 14
0
func DoInfo(c *cli.Context) {
	useOptions(c)
	loginIfNeeded()
	if !config.Exists() {
		term.Println("No app configured in current directory. (Missing appstax.conf)")
	} else {
		app, err := account.GetCurrentApp()
		if err != nil {
			term.Println("You don't have access to the currently selected app")
		} else {
			term.Println("App name:    " + app.AppName)
			term.Println("Description: " + app.AppDescription)
			term.Println("App key:     " + app.AppKey)
			term.Println("Collections: " + strings.Join(app.CollectionNames(), ", "))
			term.Println("Hosting:     " + account.FormatHostingUrl(app))
			term.Section()
		}
	}
	user, err := account.GetUser()
	fail.Handle(err)
	term.Printf("Logged in as %s %s (%s)\n", user.FirstName, user.LastName, user.Email)
}
Ejemplo n.º 15
0
func ReadAccountID() string {
	acc, err := ioutil.ReadFile(dir() + "/account")
	fail.Handle(err)
	return string(acc)
}
Ejemplo n.º 16
0
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)
}
Ejemplo n.º 17
0
func ReadUserID() string {
	user, err := ioutil.ReadFile(dir() + "/user")
	fail.Handle(err)
	return string(user)
}
Ejemplo n.º 18
0
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)
}
Ejemplo n.º 19
0
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)
}