Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Last active August 12, 2019 11:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slmcmahon/b1e8e8fa1d1cefba840423ae1c2a2390 to your computer and use it in GitHub Desktop.
Save slmcmahon/b1e8e8fa1d1cefba840423ae1c2a2390 to your computer and use it in GitHub Desktop.
Read an excel document, create a JSON message and send it to an Azure function.
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"io/ioutil"
"net/http"
"os"
"strings"
)
type UserFunction struct {
Alias string `json:"alias"`
Function string `json:"function"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: ", os.Args[0], "/path/to/excel.xlsx")
return
}
f, err := excelize.OpenFile(strings.Join(os.Args[1:], " "))
if err != nil {
fmt.Println(err)
return
}
var userFunctions []*UserFunction
rows := f.GetRows("SU_Function_Mapping")
for i, row := range rows {
if i < 1 || row[0] == "" || row[1] == "" {
continue
}
userFunctions = append(userFunctions, &UserFunction{row[0], row[1]})
}
if len(userFunctions) < 1 {
fmt.Println("No data found.")
return
}
sendToServer(userFunctions)
}
func sendToServer(funcList []*UserFunction) {
jsonInfo, _ := json.Marshal(funcList)
fmt.Printf("%s\n\n", jsonInfo)
url := os.Getenv("SUSERVICEURL")
key := os.Getenv("SUSERVICEKEY")
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonInfo))
if err != nil {
fmt.Printf("Create request failed with error %s\n", err)
}
req.Header.Add("X-Functions-Key", key)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
} else {
if resp.StatusCode == 200 {
fmt.Println("Posted successfully.")
} else {
data, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(data))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment