~craftyguy/shellcheck-http

0aa8c9ff29a5796b6e78e242ebb68e7e7a629fb8 — Clayton Craft a month ago 819f4d3 master
fix formatting
1 files changed, 44 insertions(+), 44 deletions(-)

M main.go
M main.go => main.go +44 -44
@@ 3,71 3,71 @@
package main

import (
        "bytes"
	"bytes"
	"io"
	"io/ioutil"
	"log"
        "net/http"
        "os"
        "os/exec"
        "io/ioutil"

	"net/http"
	"os"
	"os/exec"
)

func indexHandler(resp http.ResponseWriter, req *http.Request) {
        log.Println("New connection: ", req.UserAgent())
        http.ServeFile(resp, req, "index.html")
	log.Println("New connection: ", req.UserAgent())
	http.ServeFile(resp, req, "index.html")

        return
	return
}

func exists(file string) bool {
        if _, err := os.Stat(file); err == nil {
                return true
        }
        return false
	if _, err := os.Stat(file); err == nil {
		return true
	}
	return false
}

func uploadHandler(resp http.ResponseWriter, req *http.Request) {

        if req.Method != "POST" {
                http.Error(resp, "400 oops", http.StatusBadRequest)
                return
        }
	if req.Method != "POST" {
		http.Error(resp, "400 oops", http.StatusBadRequest)
		return
	}

        // max size: 512KB
        if req.ContentLength > 1024 * 512 {
                http.Error(resp, "Script size too large, limit is 512KB", http.StatusBadRequest)
                return
        }
	// max size: 512KB
	if req.ContentLength > 1024*512 {
		http.Error(resp, "Script size too large, limit is 512KB", http.StatusBadRequest)
		return
	}

        file, _, err := req.FormFile("script")
	file, _, err := req.FormFile("script")
	if err != nil {
		http.Error(resp, err.Error(), http.StatusBadRequest)
		return
	}

        tmpFile, err := ioutil.TempFile("", "shellcheck-web")
        if err != nil {
                http.Error(resp, "Unable to process uploaded script: " + err.Error(), http.StatusInternalServerError)
        }
        defer os.Remove(tmpFile.Name())
	tmpFile, err := ioutil.TempFile("", "shellcheck-web")
	if err != nil {
		http.Error(resp, "Unable to process uploaded script: "+err.Error(), http.StatusInternalServerError)
	}
	defer os.Remove(tmpFile.Name())

	_, err = io.Copy(tmpFile, file)
        if err != nil {
                http.Error(resp, "Unable to process uploaded script: " + err.Error(), http.StatusInternalServerError)
        }

        cmd := exec.Command("shellcheck", tmpFile.Name())
        if !exists(cmd.Path) {
                http.Error(resp, "Unable to find the shellcheck command.", http.StatusInternalServerError)
        }

        output := &bytes.Buffer{}
        cmd.Stdout = output
        // err is ignored, since shellcheck will return != 0 if there are issues
        _ = cmd.Run()
        respString := string(output.Bytes())

        io.WriteString(resp, respString)
	if err != nil {
		http.Error(resp, "Unable to process uploaded script: "+err.Error(), http.StatusInternalServerError)
	}

	cmd := exec.Command("shellcheck", tmpFile.Name())
	if !exists(cmd.Path) {
		http.Error(resp, "Unable to find the shellcheck command.", http.StatusInternalServerError)
	}

	output := &bytes.Buffer{}
	cmd.Stdout = output
	// err is ignored, since shellcheck will return != 0 if there are issues
	_ = cmd.Run()
	respString := string(output.Bytes())

	io.WriteString(resp, respString)
}

func httpServer() {