aboutsummaryrefslogtreecommitdiff
path: root/sessions.go
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-28 06:16:07 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-28 06:16:07 +0200
commit9eacbb42f9a5931bf33d968f775fc806c46b11b0 (patch)
treebf9a34182b2db560023ba0deb060146bdbcc0d34 /sessions.go
parent4137fae6df4e77198b8ddd9d9647a1ee0c7c49d3 (diff)
downloadcolabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.tar.gz
colabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.zip
Init
Diffstat (limited to 'sessions.go')
-rw-r--r--sessions.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/sessions.go b/sessions.go
new file mode 100644
index 0000000..c3f0bf1
--- /dev/null
+++ b/sessions.go
@@ -0,0 +1,85 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "text/tabwriter"
+
+ "github.com/spf13/cobra"
+
+ "github.com/tetsuo/colabctl/internal/auth"
+ "github.com/tetsuo/colabctl/internal/colab"
+ "github.com/tetsuo/colabctl/internal/config"
+)
+
+var sessionsCmd = &cobra.Command{
+ Use: "sessions",
+ Aliases: []string{"ps"},
+ Short: "List active Colab runtimes on your account",
+ RunE: func(cmd *cobra.Command, _ []string) error {
+ requireCredentials()
+
+ httpClient, err := auth.Client(cmd.Context(), clientID, clientSecret)
+ if err != nil {
+ return err
+ }
+ client := colab.New(httpClient)
+
+ runtimes, err := client.ListAssignments(cmd.Context())
+ if err != nil {
+ return err
+ }
+
+ if len(runtimes) == 0 {
+ fmt.Println("No active runtimes.")
+ return nil
+ }
+
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
+ fmt.Fprintln(w, "ENDPOINT\tACCELERATOR\tURL")
+ fmt.Fprintln(w, "--------\t-----------\t---")
+ for _, r := range runtimes {
+ accel := r.Accelerator
+ if accel == "" {
+ accel = "CPU"
+ }
+ fmt.Fprintf(w, "%s\t%s\t%s\n", r.Endpoint, accel, r.URL)
+ }
+ return w.Flush()
+ },
+}
+
+var stopCmd = &cobra.Command{
+ Use: "stop <endpoint>",
+ Short: "Stop and release an active Colab runtime",
+ Long: `Terminates the runtime identified by its endpoint ID.
+Get the endpoint from:
+
+ colabctl sessions`,
+ Args: cobra.ExactArgs(1),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ requireCredentials()
+
+ endpoint := args[0]
+ ctx := cmd.Context()
+
+ httpClient, err := auth.Client(ctx, clientID, clientSecret)
+ if err != nil {
+ return err
+ }
+ client := colab.New(httpClient)
+
+ fmt.Fprintf(os.Stderr, "Stopping runtime %s …\n", endpoint)
+ if err := client.Unassign(ctx, endpoint); err != nil {
+ return fmt.Errorf("stop runtime: %w", err)
+ }
+ _ = config.DeleteSession(endpoint)
+ fmt.Println("Runtime stopped.")
+ return nil
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(sessionsCmd)
+ rootCmd.AddCommand(stopCmd)
+}