func uploadBits(server, appGuid string, zipFile *os.File, bloblets []bloblet.Bloblet, presentFiles []resources.AppFileResource) { apiUrl := fmt.Sprintf("http://%s/v2/apps/%s/bits", server, appGuid) fileutils.TempFile("requests", func(requestFile *os.File, err error) { cliutil.Check(err) // json.Marshal represents a nil value as "null" instead of an empty slice "[]" if presentFiles == nil { presentFiles = []resources.AppFileResource{} } presentFilesJSON, err := json.Marshal(presentFiles) cliutil.Check(err) boundary, err := writeUploadBody(zipFile, requestFile, bloblets, presentFilesJSON) cliutil.Check(err) var request *Request request, err = newRequestForFile("PUT", apiUrl, requestFile) cliutil.Check(err) contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary) request.HttpReq.Header.Set("Content-Type", contentType) response := &resources.Resource{} _, err = performPollingRequestForJSONResponse(server, request, response, DefaultAppUploadBitsTimeout) cliutil.Check(err) }) }
func (cmd *Push) uploadApp(appGuid string, appDir string) (apiErr error) { fileutils.TempDir("apps", func(uploadDir string, err error) { if err != nil { apiErr = err return } presentFiles, hasFileToUpload, err := cmd.actor.GatherFiles(appDir, uploadDir) if err != nil { apiErr = err return } fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if hasFileToUpload { err = cmd.zipAppFiles(zipFile, appDir, uploadDir) if err != nil { apiErr = err return } } err = cmd.actor.UploadApp(appGuid, zipFile, presentFiles) if err != nil { apiErr = err return } }) return }) return }
func (cmd *Push) uploadApp(appGuid string, appDir string) (apiErr error) { log.SetPrefix("cli") log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) log.Println("uploadApp started") fileutils.TempDir("apps", func(uploadDir string, err error) { if err != nil { apiErr = err return } presentFiles, hasFileToUpload, err := cmd.actor.GatherFiles(appDir, uploadDir) if err != nil { apiErr = err return } log.Printf("presentFiles = %#v\n", presentFiles) fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if hasFileToUpload { log.Println("about to zipAppFiles") err = cmd.zipAppFiles(zipFile, appDir, uploadDir) if err != nil { apiErr = err return } log.Println("zipAppFiles completed") } log.Println("about to UploadApp") err = cmd.actor.UploadApp(appGuid, zipFile, presentFiles) if err != nil { apiErr = err return } log.Println("UploadApp completed") }) return }) log.Println("uploadApp ending") return }
func main() { if len(os.Args) != 3 || len(os.Args) > 1 && os.Args[1] == "help" { fmt.Println("Usage: client host:port application-path") os.Exit(1) } log.Println("Push started") client := &http.Client{} server := os.Args[1] scanner.Scan(os.Args[2], func(appDir string, condensate bloblet.Condensate, appFiles []models.AppFileFields) { log.Println("Resource matching started") request := resmatch.ResMatchRequest(server, integrityFields(appFiles)) log.Println("Resource matching sending request") response := cliutil.Converse(client, request) log.Println("Resource matching received response") presentIffs := resmatch.ProcessResponse(response) log.Println("Resource matching response demarshalled") presentFiles := intersectAppFilesIntegrityFields(appFiles, presentIffs) log.Println("Resource matching complete") appFilesToUpload := make([]models.AppFileFields, len(appFiles)) copy(appFilesToUpload, appFiles) for _, file := range presentFiles { appFilesToUpload = deleteAppFile(appFilesToUpload, file.Path) } // Populate file modes var err error presentFiles, err = PopulateFileMode(appDir, presentFiles) cliutil.Check(err) hasFileToUpload := len(appFilesToUpload) > 0 fileutils.TempDir("upload-dir", func(uploadDir string, err error) { cliutil.Check(err) var blobletsToUpload []bloblet.Bloblet blobletsToUpload, appFilesToUpload = condensate.Bloblets(appFilesToUpload) cliutil.Check(app_files.ApplicationFiles{}.CopyFiles(appFilesToUpload, appDir, uploadDir)) fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if hasFileToUpload { log.Println("Zipping application files") cliutil.Check(zipWithBetterErrors(uploadDir, zipFile)) log.Println("Zipped application files") } log.Println("Uploading application") appbits.UploadApp(server, "test-guid", zipFile, blobletsToUpload, presentFiles) log.Println("Uploaded application") log.Println("Deleting uploads file") }) log.Println("Deleting upload directory") }) }) log.Println("Push completed") }