aboutsummaryrefslogtreecommitdiff
path: root/list.go
blob: 0b1284631e6dc618016f5e51b531d367b166dac7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
	"fmt"
	"os"
	"text/tabwriter"
	"time"

	"github.com/spf13/cobra"

	"git.onurgunduz.com/colabctl/internal/auth"
	"git.onurgunduz.com/colabctl/internal/drive"
)

var listCmd = &cobra.Command{
	Use:     "list",
	Aliases: []string{"ls"},
	Short:   "List Colab notebooks from Google Drive",
	RunE: func(cmd *cobra.Command, _ []string) error {
		requireCredentials()

		httpClient, err := auth.Client(cmd.Context(), clientID, clientSecret)
		if err != nil {
			return err
		}

		driveClient, err := drive.New(cmd.Context(), httpClient)
		if err != nil {
			return err
		}

		notebooks, err := driveClient.ListNotebooks(cmd.Context())
		if err != nil {
			return err
		}

		if len(notebooks) == 0 {
			fmt.Println("No Colab notebooks found in your Drive.")
			return nil
		}

		w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
		fmt.Fprintln(w, "ID\tNAME\tMODIFIED")
		fmt.Fprintln(w, "--\t----\t--------")
		for _, nb := range notebooks {
			modified := nb.Modified
			if t, err := time.Parse(time.RFC3339, nb.Modified); err == nil {
				modified = t.Format("2006-01-02 15:04")
			}
			fmt.Fprintf(w, "%s\t%s\t%s\n", nb.ID, nb.Name, modified)
		}
		return w.Flush()
	},
}

func init() {
	rootCmd.AddCommand(listCmd)
}