Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jose1984 committed Mar 15, 2024
1 parent 220d3f8 commit 3e321b1
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions osint/osint.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func PrintNORADInfo(norad string, name string) {
PrintTLE(tle)
}

func fetchData(page int, pageSize int) ([]string, error) {
func fetchData(page int, pageSize int) ([]Satellite, error) {
vals := url.Values{}
vals.Add("identity", os.Getenv("SPACE_TRACK_USERNAME"))
vals.Add("password", os.Getenv("SPACE_TRACK_PASSWORD"))
Expand All @@ -70,47 +70,43 @@ func fetchData(page int, pageSize int) ([]string, error) {

resp, err := client.PostForm(authURL, vals)
if err != nil {
return nil, fmt.Errorf(" [!] ERROR: API REQUEST TO SPACE TRACK")
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf(" [!] ERROR: API REQUEST TO SPACE TRACK")
return nil, err
}

var sats []Satellite
if err := json.NewDecoder(resp.Body).Decode(&sats); err != nil {
return nil, fmt.Errorf(" [!] ERROR: API REQUEST TO SPACE TRACK")
return nil, err
}

var items []string
for _, sat := range sats {
items = append(items, sat.SATNAME+" ("+sat.NORAD_CAT_ID+")")
}
return items, nil
return sats, nil
}

func SelectSatellite() string {
const pageSize = 10
page := 0
var satStrings []string
var result string
var index int

for {
items, err := fetchData(page, pageSize)
if err != nil {
fmt.Println(color.Ize(color.Red, err.Error()))
fmt.Println(color.Ize(color.Red, " [!] ERROR: API REQUEST TO SPACE TRACK"))
break
}
satStrings = append(satStrings, items...)
for _, sat := range items {
satStrings = append(satStrings, fmt.Sprintf("%s (%s)", sat.SATNAME, sat.NORAD_CAT_ID))
}
prompt := promptui.Select{
Label: "Select a Satellite 🛰",
Items: append(satStrings, fmt.Sprintf("Load next %d results", pageSize)),
Size: 5,
HideSelected: true,
}
cursorPos := len(satStrings) - pageSize
index, result, err = prompt.RunCursorAt(cursorPos, cursorPos-prompt.Size+1)
index, result, err := prompt.RunCursorAt(cursorPos, cursorPos-prompt.Size+1)
if err != nil {
fmt.Println(color.Ize(color.Red, " [!] PROMPT FAILED"))
break
Expand Down

0 comments on commit 3e321b1

Please sign in to comment.