|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/doneill/er-cli/api" |
| 10 | + "github.com/doneill/er-cli/config" |
| 11 | + "github.com/olekukonko/tablewriter" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var daysAgo int |
| 16 | + |
| 17 | +// ---------------------------------------------- |
| 18 | +// subjects command |
| 19 | +// ---------------------------------------------- |
| 20 | + |
| 21 | +var subjectsCmd = &cobra.Command{ |
| 22 | + Use: "subjects", |
| 23 | + Short: "Get updated subjects data", |
| 24 | + Long: `Return subject data updated within specified number of days ago`, |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + subjects() |
| 27 | + }, |
| 28 | +} |
| 29 | + |
| 30 | +// ---------------------------------------------- |
| 31 | +// functions |
| 32 | +// ---------------------------------------------- |
| 33 | + |
| 34 | +func subjects() { |
| 35 | + updatedSince := time.Now().AddDate(0, 0, -daysAgo).UTC().Format("2006-01-02T15:04:05.000") |
| 36 | + client := api.ERClient(config.Sitename(), config.Token()) |
| 37 | + |
| 38 | + subjectsResponse, err := client.Subjects(updatedSince) |
| 39 | + if err != nil { |
| 40 | + log.Fatalf("Error getting subjects: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + if subjectsResponse == nil || len(subjectsResponse.Data) == 0 { |
| 44 | + fmt.Println("No subjects found") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + table := configureSubjectsTable() |
| 49 | + for _, subject := range subjectsResponse.Data { |
| 50 | + table.Append(formatSubjectData(&subject)) |
| 51 | + } |
| 52 | + |
| 53 | + table.Render() |
| 54 | +} |
| 55 | + |
| 56 | +func formatSubjectData(subject *api.Subject) []string { |
| 57 | + coordinates := "N/A" |
| 58 | + |
| 59 | + if len(subject.LastPosition.Geometry.Coordinates) >= 2 { |
| 60 | + coordinates = fmt.Sprintf("%.6f, %.6f", |
| 61 | + subject.LastPosition.Geometry.Coordinates[1], |
| 62 | + subject.LastPosition.Geometry.Coordinates[0], |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + return []string{ |
| 67 | + subject.Name, |
| 68 | + subject.ID, |
| 69 | + subject.SubjectType, |
| 70 | + subject.SubjectSubtype, |
| 71 | + subject.LastPositionDate, |
| 72 | + coordinates, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func configureSubjectsTable() *tablewriter.Table { |
| 77 | + table := tablewriter.NewWriter(os.Stdout) |
| 78 | + table.SetHeader([]string{ |
| 79 | + "Name", |
| 80 | + "ID", |
| 81 | + "Type", |
| 82 | + "Subtype", |
| 83 | + "Last Position Date", |
| 84 | + "Last Position (lat, lon)", |
| 85 | + }) |
| 86 | + table.SetBorders(tablewriter.Border{ |
| 87 | + Left: true, |
| 88 | + Top: true, |
| 89 | + Right: true, |
| 90 | + Bottom: true, |
| 91 | + }) |
| 92 | + table.SetCenterSeparator("|") |
| 93 | + return table |
| 94 | +} |
| 95 | + |
| 96 | +// ---------------------------------------------- |
| 97 | +// initialize |
| 98 | +// ---------------------------------------------- |
| 99 | + |
| 100 | +func init() { |
| 101 | + rootCmd.AddCommand(subjectsCmd) |
| 102 | + subjectsCmd.Flags().IntVarP(&daysAgo, "updated-since", "u", 3, "Number of days ago to query updates from") |
| 103 | +} |
0 commit comments