aboutsummaryrefslogtreecommitdiff
path: root/internal/config
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 /internal/config
parent4137fae6df4e77198b8ddd9d9647a1ee0c7c49d3 (diff)
downloadcolabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.tar.gz
colabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.zip
Init
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go79
-rw-r--r--internal/config/sessions.go126
2 files changed, 205 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..a850386
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,79 @@
+package config
+
+import (
+ "encoding/json"
+ "errors"
+ "os"
+ "path/filepath"
+
+ "golang.org/x/oauth2"
+)
+
+// Dir returns the directory used to store all persistent state.
+func Dir() (string, error) {
+ home, err := os.UserHomeDir()
+ if err != nil {
+ return "", err
+ }
+ return filepath.Join(home, ".config", "colab"), nil
+}
+
+// TokenPath returns the path to the stored OAuth2 token file.
+func TokenPath() (string, error) {
+ dir, err := Dir()
+ if err != nil {
+ return "", err
+ }
+ return filepath.Join(dir, "token.json"), nil
+}
+
+// LoadToken reads the stored token from disk. Returns an error wrapping
+// os.ErrNotExist when no token has been saved yet.
+func LoadToken() (*oauth2.Token, error) {
+ path, err := TokenPath()
+ if err != nil {
+ return nil, err
+ }
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+
+ var tok oauth2.Token
+ if err := json.NewDecoder(f).Decode(&tok); err != nil {
+ return nil, err
+ }
+ return &tok, nil
+}
+
+// SaveToken writes the token to disk, creating parent directories as needed.
+func SaveToken(tok *oauth2.Token) error {
+ dir, err := Dir()
+ if err != nil {
+ return err
+ }
+ if err := os.MkdirAll(dir, 0700); err != nil {
+ return err
+ }
+ path := filepath.Join(dir, "token.json")
+ f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ return json.NewEncoder(f).Encode(tok)
+}
+
+// DeleteToken removes the stored token, effectively logging out.
+func DeleteToken() error {
+ path, err := TokenPath()
+ if err != nil {
+ return err
+ }
+ err = os.Remove(path)
+ if errors.Is(err, os.ErrNotExist) {
+ return nil
+ }
+ return err
+}
diff --git a/internal/config/sessions.go b/internal/config/sessions.go
new file mode 100644
index 0000000..cbc9ffa
--- /dev/null
+++ b/internal/config/sessions.go
@@ -0,0 +1,126 @@
+package config
+
+import (
+ "encoding/json"
+ "errors"
+ "os"
+ "path/filepath"
+ "time"
+)
+
+// SessionState is the on-disk record for one active Colab runtime.
+type SessionState struct {
+ Endpoint string `json:"endpoint"`
+ URL string `json:"url"`
+ Token string `json:"token"`
+ TokenExpiresAt time.Time `json:"tokenExpiresAt"`
+ KernelID string `json:"kernelId,omitempty"`
+}
+
+// Valid returns true if the session's proxy token has not yet expired.
+func (s *SessionState) Valid() bool {
+ return s.Token != "" && time.Now().Before(s.TokenExpiresAt)
+}
+
+type sessionFile struct {
+ Sessions map[string]*SessionState `json:"sessions"`
+}
+
+func sessionsPath() (string, error) {
+ dir, err := Dir()
+ if err != nil {
+ return "", err
+ }
+ return filepath.Join(dir, "sessions.json"), nil
+}
+
+func loadSessionFile() (*sessionFile, error) {
+ path, err := sessionsPath()
+ if err != nil {
+ return nil, err
+ }
+ f, err := os.Open(path)
+ if errors.Is(err, os.ErrNotExist) {
+ return &sessionFile{Sessions: map[string]*SessionState{}}, nil
+ }
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+
+ var sf sessionFile
+ if err := json.NewDecoder(f).Decode(&sf); err != nil {
+ return &sessionFile{Sessions: map[string]*SessionState{}}, nil
+ }
+ if sf.Sessions == nil {
+ sf.Sessions = map[string]*SessionState{}
+ }
+ return &sf, nil
+}
+
+func saveSessionFile(sf *sessionFile) error {
+ dir, err := Dir()
+ if err != nil {
+ return err
+ }
+ if err := os.MkdirAll(dir, 0700); err != nil {
+ return err
+ }
+ path, err := sessionsPath()
+ if err != nil {
+ return err
+ }
+ f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ return json.NewEncoder(f).Encode(sf)
+}
+
+// SaveSession persists a session under the given endpoint key.
+func SaveSession(s *SessionState) error {
+ sf, err := loadSessionFile()
+ if err != nil {
+ return err
+ }
+ sf.Sessions[s.Endpoint] = s
+ return saveSessionFile(sf)
+}
+
+// LoadSession returns the saved session for the given endpoint, or nil if not
+// found or the token has expired.
+func LoadSession(endpoint string) (*SessionState, error) {
+ sf, err := loadSessionFile()
+ if err != nil {
+ return nil, err
+ }
+ s, ok := sf.Sessions[endpoint]
+ if !ok {
+ return nil, nil
+ }
+ return s, nil
+}
+
+// ListSessions returns all saved sessions regardless of token validity.
+func ListSessions() ([]*SessionState, error) {
+ sf, err := loadSessionFile()
+ if err != nil {
+ return nil, err
+ }
+ out := make([]*SessionState, 0, len(sf.Sessions))
+ for _, s := range sf.Sessions {
+ out = append(out, s)
+ }
+ return out, nil
+}
+
+// DeleteSession removes a saved session by endpoint.
+func DeleteSession(endpoint string) error {
+ sf, err := loadSessionFile()
+ if err != nil {
+ return err
+ }
+ delete(sf.Sessions, endpoint)
+ return saveSessionFile(sf)
+}