Skip to content

Commit

Permalink
Request to API
Browse files Browse the repository at this point in the history
  • Loading branch information
sachasmart committed Sep 23, 2023
1 parent 9a7547e commit a12e873
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 21 deletions.
20 changes: 20 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package common

import (
"log"
"os"

"github.com/joho/godotenv"
)

func EnvVariable(key string) string {

err := godotenv.Load(".env")

if err != nil {
log.Fatalf("Error loading .env file")
}
log.Println("Environment variables loaded")

return os.Getenv(key)
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/sachasmart/terrafir-github-action

go 1.20

require github.com/joho/godotenv v1.5.1 // indirect
require (
github.com/fatih/color v1.15.0
github.com/joho/godotenv v1.5.1
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
golang.org/x/sys v0.6.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
89 changes: 70 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,108 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"

"github.com/joho/godotenv"
"github.com/fatih/color"
"github.com/sachasmart/terrafir-github-action/types"
)

var (
apiKey = flag.String("api-key", os.Getenv("API_KEY"), "API key Terrafir API.")
email = flag.String("email", os.Getenv("EMAIL"), "Email address to send the request to.")
input = flag.String("input", os.Getenv("INPUT"), "Input to send to the API.")
verboseMode = flag.String("verbose", os.Getenv("VERBOSE"), "Verbose mode")
apiKey = flag.String("apiKey", "", "API key Terrafir API.")
email = flag.String("email", "", "Email address to send the request to.")
inputFilePath = flag.String("input", "", "Input file path to the plan that will be assessed.")
verboseMode = flag.String("verbose", "", "Verbose mode")
)

func main() {
godotenv.Load(".env")
flag.Parse()
checkEnvironmentVariables(*apiKey, *email, *input)
sendRequest(*apiKey, *email, *input)
preRequestCheck()
checkEnvironmentVariables(*apiKey, *email, *inputFilePath)
sendRequest(*apiKey, *email, *inputFilePath)
}

func preRequestCheck() {
response, err := http.Get(types.URL)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()

if response.StatusCode != 200 {
log.Fatal("API is not available")
}
fmt.Println("API is available")
color.Green(fmt.Sprintf("API is available %s", response.Status))
}

func sendRequest(apiKey string, email string, input string) {
preRequestCheck()
response, err := http.Post(types.URL, "application/json", nil)
func sendRequest(apiKey string, email string, inputFilePath string) {
color.Green(fmt.Sprintf("Using input file: %s", inputFilePath))

payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open(inputFilePath)
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("plan", filepath.Base(inputFilePath))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
log.Fatal(err)
fmt.Println(err)
return
}

client := &http.Client{}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/assessment/api", types.URL), payload)

if err != nil {
fmt.Println(err)
return
}
fmt.Println(response.Body)
req.Header.Add("email", email)
req.Header.Add("Authorization", apiKey)

req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
var formattedBody map[string]interface{}
if err := json.Unmarshal(body, &formattedBody); err != nil {
fmt.Println("Error unmarshaling JSON:", err)
return
}

fmt.Println(formattedBody)
}

func checkEnvironmentVariables(apiKey string, email string, input string) {
if apiKey == "" {
log.Fatalf("Environment variable 'API_KEY' not found.")
log.Fatalf("API key not provided.")
}
_, err := url.Parse(apiKey)
if err != nil {
log.Fatalf("Environment variable 'API_KEY' is not a valid.")
if email == "" {
log.Fatalf("Email address not provided.")
}
if input == "" {
log.Fatalf("Input not provided.")
}
}
2 changes: 1 addition & 1 deletion types/request.types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type Post struct {
Input string `json:"input"`
}

const URL string = "https://api.terrafir.com/"
const URL string = "https://api.terrafir.com"

0 comments on commit a12e873

Please sign in to comment.