Skip to content

Variable unused + proposition of improvement #4

@tristandostaler

Description

@tristandostaler

The following variable is not used:

var orgId = "orgId"

Here are my proposed modifications:

// (...)

var orgIds = []string{}

//(...)
// Replace all SHUFFLE_ORGID for SHUFFLE_ORGIDS

if len(os.Getenv("SHUFFLE_ORGIDS")) > 0 {
    orgIds = strings.Split(os.Getenv("SHUFFLE_ORGIDS"), ",")
}

// (...)

func GenerateAppRepo(folderpath string) error {
	log.Printf("[DEBUG] Uploading app from %#v: ", folderpath)

	// Walk the path and add
	allFiles := []string{}
	err := filepath.Walk(folderpath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if !info.IsDir() {
			allFiles = append(allFiles, path)
		}

		return nil
	})

	if err != nil {
		log.Printf("[ERROR] Problem walking path: %s", err)
		return err
	}

	zipLocation := fmt.Sprintf("%s/upload.zip", folderpath)
	err = ZipFiles(zipLocation, allFiles)
	if err != nil {
		log.Printf("[ERROR] Problem zipping files: %s", err)
		return err
	}

	return nil
}

// (...)

func UploadAppFromRepo(folderpath string, orgId string) error {
	zipLocation := fmt.Sprintf("%s/upload.zip", folderpath)

	newUrl := fmt.Sprintf("%s/api/v1/apps/upload", uploadUrl)
	log.Printf("\n\n[INFO] Zipped files to %s. Starting upload to %s. This may take a while, as validation will take place on cloud.", zipLocation, newUrl)

	// Add file to request
	file, err := os.Open(zipLocation)
	if err != nil {
		log.Printf("[ERROR] Problem opening file: %s", err)
		return err
	}

	defer file.Close()
	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)

	// Add the file to the form with the field name "shuffle_file"
	part, err := writer.CreateFormFile("shuffle_file", filepath.Base(zipLocation))
	if err != nil {
		return err
	}

	// Copy the file into the form
	_, err = io.Copy(part, file)
	if err != nil {
		return err
	}

	// Close the multipart writer to finalize the form
	err = writer.Close()
	if err != nil {
		return err
	}

	client := &http.Client{}
	req, err := http.NewRequest(
		"POST",
		newUrl,
		body,
	)

	if err != nil {
		log.Printf("[ERROR] Problem creating request: %s", err)
		return err
	}

	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
	req.Header.Set("Content-Type", writer.FormDataContentType())
        req.Header.Set("Org-Id", orgId)

	// Upload the file
	resp, err := client.Do(req)
	if err != nil {
		log.Printf("[ERROR] Problem uploading file: %s", err)
		return err
	}

	outputBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Printf("[ERROR] Problem reading response body: %s", err)
		return err
	}

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("Bad status: %s. Raw: %s", resp.Status, string(outputBody))
	}

	log.Printf("[INFO] File uploaded successfully: %s", resp.Status)

	return nil
}

// (...)
// in var uploadApp = &cobra.Command{ Run definition:

                log.Printf("INPUT: %#v", input)
		if strings.ToUpper(input) != "Y" {
			log.Println("[INFO] Aborting upload.")
			return
		}

		// Generate the app repo
		log.Printf("[INFO] Generating app repo for %s", args[0])
		err = GenerateAppRepo(args[0])
		if err != nil {
			log.Printf("[ERROR] Problem generating app repo: %s", err)
			return
		}

		for _, orgId := range orgIds {
			// Upload the app
			log.Printf("[INFO] Uploading app to orgId %s", orgId)
			err = UploadAppFromRepo(args[0], orgId)
			if err != nil {
				log.Printf("[ERROR] Problem uploading app: %s", err)
				return
			}

			log.Printf("[INFO] App uploaded successfully to orgId %s", orgId)
		}

// (...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions