-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add timetable sub-command, add error messages to GetTimetable
- Loading branch information
Showing
2 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cartabinaria/unibo-go/timetable" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cmdTimetable = &cobra.Command{ | ||
Use: "timetable", | ||
Short: "fetches the timetable of a degree course", | ||
Args: cobra.RangeArgs(3, 4), | ||
Aliases: []string{"t"}, | ||
Run: runTimetable, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(cmdTimetable) | ||
} | ||
|
||
var ( | ||
yellowFmt = color.New(color.FgYellow).SprintFunc() | ||
greenFmt = color.New(color.FgGreen).SprintFunc() | ||
redFmt = color.New(color.FgRed).SprintFunc() | ||
grayFmt = color.New(color.Faint).SprintFunc() | ||
) | ||
|
||
func runTimetable(cmd *cobra.Command, args []string) { | ||
|
||
courseType := args[0] | ||
courseId := args[1] | ||
yearStr := args[2] | ||
|
||
year64, err := strconv.ParseInt(yearStr, 10, 32) | ||
if err != nil { | ||
cmd.PrintErrln(fmt.Errorf("year must be a number")) | ||
return | ||
} | ||
|
||
year := int(year64) | ||
|
||
var curriculum string | ||
if len(args) == 4 { | ||
curriculum = args[3] | ||
} | ||
|
||
today := time.Now().Truncate(24 * time.Hour) | ||
|
||
interval := &timetable.Interval{Start: today, End: today} | ||
tt, err := timetable.FetchTimetable(courseType, courseId, curriculum, year, interval) | ||
if err != nil { | ||
cmd.PrintErrln(fmt.Errorf("error fetching timetable: %v", err)) | ||
return | ||
} | ||
|
||
if len(tt) == 0 { | ||
cmd.Println(yellowFmt("No lessons found")) | ||
return | ||
} | ||
|
||
for _, e := range tt { | ||
cmd.Printf("- %s -> %s: %-50s %-30s (%s)\n", | ||
greenFmt(e.Start.Format("15:04")), redFmt(e.End.Format("15:04")), | ||
e.Title, yellowFmt(e.Teacher), grayFmt(e.CodModulo)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters