csctl/cmd/apply.go

110 lines
2.7 KiB
Go

package cmd
import (
"csctl/config"
"csctl/internal"
"encoding/json"
"fmt"
"net/http"
"os/user"
"strings"
"github.com/kataras/golog"
"github.com/logrusorgru/aurora/v3"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
)
type Response struct {
Status string `json:"status"`
Decision string `json:"decision"`
ApplicationID string `json:"id"`
ProcessedBy string `json:"processedBy"`
}
func ApplyHard(ctx *cli.Context) error {
u, _ := user.Current()
account, err := internal.FetchAccount(u.Username)
if err != nil {
internal.Error(err, "Unable to read account information.")
}
resp, err := http.Get(fmt.Sprintf("https://eds.libraryofcode.org/cs/t2/?userID=%s&auth=%s", account.UserID, config.FetchInternalKey()))
if err != nil {
internal.Error(err, "Unable to read information from EDS.")
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Status", "Application ID"})
if resp.StatusCode == 404 || resp.StatusCode == 400 || resp.StatusCode == 401 {
dataErr := [][]string{
{aurora.Yellow("PRE-DECLINED").String(), "N/A"},
}
table.AppendBulk(dataErr)
table.Render()
fmt.Println(tableString.String())
return nil
}
var response Response
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
internal.Error(err, "Unable to parse JSON from CSD.")
}
data := [][]string{
{response.Decision, response.ApplicationID},
}
table.AppendBulk(data)
table.Render()
fmt.Println(tableString.String())
return nil
}
func ApplySoft(ctx *cli.Context) error {
golog.Debug("Fetching account information.")
u, _ := user.Current()
account, err := internal.FetchAccount(u.Username)
if err != nil {
internal.Error(err, "Unable to read account information.")
}
golog.Info("Processing application, do not exit the CLI...")
resp, err := http.Get(fmt.Sprintf("https://eds.libraryofcode.org/cs/t2pre/?userID=%s&auth=%s", account.UserID, config.FetchInternalKey()))
if err != nil {
internal.Error(err, "Unable to read response from EDS.")
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Status", "Application ID"})
if resp.StatusCode == 404 || resp.StatusCode == 400 || resp.StatusCode == 401 {
dataErr := [][]string{
{aurora.Yellow("PRE-DECLINED").String(), "N/A"},
}
table.AppendBulk(dataErr)
table.Render()
fmt.Println(tableString.String())
return nil
}
var response Response
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
internal.Error(err, "Unable to parse JSON from CSD.")
}
data := [][]string{
{response.Decision, response.ApplicationID},
}
table.AppendBulk(data)
table.Render()
fmt.Println(tableString.String())
return nil
}