forked from toolshed/abra
		
	refactor: move function into web package
This commit is contained in:
		@ -3,43 +3,16 @@ package cli
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"net/http"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"coopcloud.tech/abra/cli/internal"
 | 
						"coopcloud.tech/abra/cli/internal"
 | 
				
			||||||
	"coopcloud.tech/abra/pkg/config"
 | 
						"coopcloud.tech/abra/pkg/config"
 | 
				
			||||||
 | 
						"coopcloud.tech/abra/pkg/web"
 | 
				
			||||||
	"github.com/sirupsen/logrus"
 | 
						"github.com/sirupsen/logrus"
 | 
				
			||||||
	"github.com/urfave/cli/v2"
 | 
						"github.com/urfave/cli/v2"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// downloadFile downloads a file brah
 | 
					 | 
				
			||||||
func downloadFile(filepath string, url string) (err error) {
 | 
					 | 
				
			||||||
	out, err := os.Create(filepath)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer out.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	resp, err := http.Get(url)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer resp.Body.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if resp.StatusCode != http.StatusOK {
 | 
					 | 
				
			||||||
		return fmt.Errorf("bad status: %s", resp.Status)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	_, err = io.Copy(out, resp.Body)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// AutoCompleteCommand helps people set up auto-complete in their shells
 | 
					// AutoCompleteCommand helps people set up auto-complete in their shells
 | 
				
			||||||
var AutoCompleteCommand = &cli.Command{
 | 
					var AutoCompleteCommand = &cli.Command{
 | 
				
			||||||
	Name:    "autocomplete",
 | 
						Name:    "autocomplete",
 | 
				
			||||||
@ -94,7 +67,7 @@ Supported shells are as follows:
 | 
				
			|||||||
		if _, err := os.Stat(autocompletionFile); err != nil && os.IsNotExist(err) {
 | 
							if _, err := os.Stat(autocompletionFile); err != nil && os.IsNotExist(err) {
 | 
				
			||||||
			url := fmt.Sprintf("https://git.coopcloud.tech/coop-cloud/abra/raw/branch/main/scripts/autocomplete/%s", shellType)
 | 
								url := fmt.Sprintf("https://git.coopcloud.tech/coop-cloud/abra/raw/branch/main/scripts/autocomplete/%s", shellType)
 | 
				
			||||||
			logrus.Infof("fetching %s", url)
 | 
								logrus.Infof("fetching %s", url)
 | 
				
			||||||
			if err := downloadFile(autocompletionFile, url); err != nil {
 | 
								if err := web.GetFile(autocompletionFile, url); err != nil {
 | 
				
			||||||
				logrus.Fatal(err)
 | 
									logrus.Fatal(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -3,6 +3,10 @@ package web
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -20,3 +24,29 @@ func ReadJSON(url string, target interface{}) error {
 | 
				
			|||||||
	defer res.Body.Close()
 | 
						defer res.Body.Close()
 | 
				
			||||||
	return json.NewDecoder(res.Body).Decode(target)
 | 
						return json.NewDecoder(res.Body).Decode(target)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetFile downloads a file and saves it to a filepath
 | 
				
			||||||
 | 
					func GetFile(filepath string, url string) (err error) {
 | 
				
			||||||
 | 
						out, err := os.Create(filepath)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer out.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resp, err := http.Get(url)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
							return fmt.Errorf("bad status: %s", resp.Status)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err = io.Copy(out, resp.Body)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user