<commit_before>package main

import (
	"fmt"
	"github.com/mgutz/ansi"
	"github.com/cloudfoundry/cli/plugin"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	)

/*
*	This is the struct implementing the interface defined by the core CLI. It can
*	be found at  "github.com/cloudfoundry/cli/plugin/plugin.go"
*
 */
type downloadPlugin struct{}

/*
*	This function must be implemented by any plugin because it is part of the
*	plugin interface defined by the core CLI.
*
*	Run(....) is the entry point when the core CLI is invoking a command defined
*	by a plugin. The first parameter, plugin.CliConnection, is a struct that can
*	be used to invoke cli commands. The second paramter, args, is a slice of
*	strings. args[0] will be the name of the command, and will be followed by
*	any additional arguments a cli user typed in.
*
*	Any error handling should be handled with the plugin itself (this means printing
*	user facing errors). The CLI will exit 0 if the plugin exits 0 and will exit
*	1 should the plugin exits nonzero.
 */
func (c *downloadPlugin) Run(cliConnection plugin.CliConnection, args []string) {
	// Ensure that we called the command basic-plugin-command
	if args[0] == "download" {
		if(len(args) != 2){
			fmt.Println("\nError: Missing App Name")
        	os.Exit(1)
		}

		appName := args[1]
		// Invoke the cf command passed as the set of arguments
		// after the first argument.
		//
		// Calls to plugin.CliCommand([]string) must be done after the invocation
		// of plugin.Start() to ensure the environment is bootstrapped.

		// cf app APP_NAME --guid
		appGuidSlice, err := cliConnection.CliCommandWithoutTerminalOutput("app", appName, "--guid")
		check(err)
		appGuid := strings.TrimSpace(appGuidSlice[0])

		url := fmt.Sprintf("/v2/apps/%s/instances/%d/files/%s", appGuid, 0, "/app")
		fmt.Println(url)
		curlRequest := []string{"curl", url}
		output, err := cliConnection.CliCommandWithoutTerminalOutput(curlRequest...)
		check(err)

		// Print the output returned from the CLI command.
		fmt.Println(output)
		/*fmt.Println("---------- Command output from the plugin ----------")
		for index, val := range output {
			fmt.Println("#", index, " value: ", val)
		}
		fmt.Println("----------              FIN               -----------")*/
		fmt.Println("Starting file download!")
		
		pull()
	}
}

func pull() {
	response, err := http.Get("http://golang.org/")
	if err != nil {
		fmt.Printf("%s", err)
		os.Exit(1)
	} else {
		defer response.Body.Close()

		contents, err := ioutil.ReadAll(response.Body)
		check(err)

		dir, err := os.Getwd()
		check(err)

		dir += "/temp/"
		//create dir if does not exist
		err = os.MkdirAll(dir, 0755)
		check(err)

		// write file to dir
		filename := "google.html"
		dir += filename
		fmt.Printf("Writing directory: %s\n", dir)
	    err = ioutil.WriteFile(dir, contents, 0644)
	    check(err)

	    msg := ansi.Color("File Successfully Downloaded!", "green+b")
	    defer fmt.Println(msg)
	}<commit_msg>refactor<commit_after>package main

import (
	"fmt"
	"github.com/mgutz/ansi"
	"github.com/cloudfoundry/cli/plugin"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	)

/*
*	This is the struct implementing the interface defined by the core CLI. It can
*	be found at  "github.com/cloudfoundry/cli/plugin/plugin.go"
*
 */
type downloadPlugin struct{}

/*
*	This function must be implemented by any plugin because it is part of the
*	plugin interface defined by the core CLI.
*
*	Run(....) is the entry point when the core CLI is invoking a command defined
*	by a plugin. The first parameter, plugin.CliConnection, is a struct that can
*	be used to invoke cli commands. The second paramter, args, is a slice of
*	strings. args[0] will be the name of the command, and will be followed by
*	any additional arguments a cli user typed in.
*
*	Any error handling should be handled with the plugin itself (this means printing
*	user facing errors). The CLI will exit 0 if the plugin exits 0 and will exit
*	1 should the plugin exits nonzero.
 */
func (c *downloadPlugin) Run(cliConnection plugin.CliConnection, args []string) {
	// Ensure that we called the command basic-plugin-command
	if args[0] == "download" {
		if(len(args) != 2){
			fmt.Println("\nError: Missing App Name")
        	os.Exit(1)
		}

		appName := args[1]

		output, err := getDirString(cliConnection, appName)
		check(err)

		// Print the output returned from the CLI command.
		files, dirs := parseDir(output)

		fmt.Println("---------- Files ----------")
		for index, val := range files {
			fmt.Println("#", index, " value: ", val)
		}
		fmt.Println("")

		fmt.Println("---------- Directories ----------")
		for index, val := range dirs {
			fmt.Println("#", index, " value: ", val)
		}
		fmt.Println("")

		fmt.Println("Starting file download!")
		pull()
	}
}

func getDirString(cliConnection plugin.CliConnection, appName string) (string, error) {
	appGuidSlice, err := cliConnection.CliCommandWithoutTerminalOutput("app", appName, "--guid")
	check(err)
	appGuid := strings.TrimSpace(appGuidSlice[0])

	url := fmt.Sprintf("/v2/apps/%s/instances/%d/files/%s", appGuid, 0, "/app")
	fmt.Println(url)
	curlRequest := []string{"curl", url}
	output, err := cliConnection.CliCommandWithoutTerminalOutput(curlRequest...)
	check(err)

	return output[0], nil
}

func parseDir(dir string) ([]string, []string) {
// PRE: takes in a string of the directory and filesizes
// POST: FCTVAL==[]string of only filenames and folders without filesizes
    filesSlice := strings.Fields(dir)
    var files, dirs []string
    for i := 0; i < len(filesSlice); i+=2 {
    	if strings.HasSuffix(filesSlice[i], "/") {
    		dirs = append(dirs, filesSlice[i])
    	} else {
			files = append(files, filesSlice[i])
    	}
            
    }
    return files, dirs
}

func pull() {
	response, err := http.Get("http://golang.org/")
	if err != nil {
		fmt.Printf("%s", err)
		os.Exit(1)
	} else {
		defer response.Body.Close()

		contents, err := ioutil.ReadAll(response.Body)
		check(err)

		dir, err := os.Getwd()
		check(err)

		dir += "/temp/"
		//create dir if does not exist
		err = os.MkdirAll(dir, 0755)
		check(err)

		// write file to dir
		filename := "google.html"
		dir += filename
		fmt.Printf("Writing directory: %s\n", dir)
	    err = ioutil.WriteFile(dir, contents, 0644)
	    check(err)

	    msg := ansi.Color("File Successfully Downloaded!", "green+b")
	    defer fmt.Println(msg)
	}
{'fraction_non_alphanumeric': 0.14424280350438048, 'fraction_numerical': 0.006727158948685858, 'mean_word_length': 3.270541082164329, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1911467', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2078, 'n_words': 675}