ci / test (push) Canceled after 0s
ci / cross-build (amd64, darwin) (push) Canceled after 0s
ci / cross-build (amd64, linux) (push) Canceled after 0s
ci / cross-build (amd64, windows) (push) Canceled after 0s
ci / cross-build (arm64, darwin) (push) Canceled after 0s
ci / cross-build (arm64, linux) (push) Canceled after 0s
ci / cross-build (arm64, windows) (push) Canceled after 0s
- apiclient: AuthSignIn (/signin/email-password) + AuthRefresh (/token) - login: interactive email+password prompt, --email/--password for CI, --token paste - resolveClient: auto-refresh expired session tokens using the stored refresh token - credentials store: refresh_token + auth_url
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
// Package gyconfig persists the CLI's local state: login credentials and the
|
|
// per-project link file that ties a directory to a deployed app.
|
|
package gyconfig
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Credentials are stored at ~/.config/ghaymah/credentials.json (0600).
|
|
type Credentials struct {
|
|
Token string `json:"token"`
|
|
RefreshToken string `json:"refresh_token,omitempty"`
|
|
BaseURL string `json:"base_url,omitempty"`
|
|
AuthURL string `json:"auth_url,omitempty"`
|
|
}
|
|
|
|
// Link ties a project directory to a deployment. Stored at .gy/link.json.
|
|
type Link struct {
|
|
ProjectID string `json:"project_id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Dir returns the config directory (respects XDG_CONFIG_HOME).
|
|
func Dir() string {
|
|
base := os.Getenv("XDG_CONFIG_HOME")
|
|
if base == "" {
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
base = filepath.Join(home, ".config")
|
|
}
|
|
}
|
|
return filepath.Join(base, "ghaymah")
|
|
}
|
|
|
|
func credsPath() string { return filepath.Join(Dir(), "credentials.json") }
|
|
|
|
// LoadCredentials reads the stored credentials (empty on any error).
|
|
func LoadCredentials() (Credentials, error) {
|
|
data, err := os.ReadFile(credsPath())
|
|
if err != nil {
|
|
return Credentials{}, err
|
|
}
|
|
var c Credentials
|
|
if err := json.Unmarshal(data, &c); err != nil {
|
|
return Credentials{}, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// SaveCredentials writes credentials with restrictive permissions.
|
|
func SaveCredentials(c Credentials) error {
|
|
if err := os.MkdirAll(Dir(), 0o700); err != nil {
|
|
return err
|
|
}
|
|
data, _ := json.MarshalIndent(c, "", " ")
|
|
return os.WriteFile(credsPath(), data, 0o600)
|
|
}
|
|
|
|
// ClearCredentials removes stored credentials.
|
|
func ClearCredentials() error { return os.Remove(credsPath()) }
|
|
|
|
func linkPath(projectDir string) string { return filepath.Join(projectDir, ".gy", "link.json") }
|
|
|
|
// LoadLink reads the project link file (zero Link on any error).
|
|
func LoadLink(projectDir string) (Link, error) {
|
|
data, err := os.ReadFile(linkPath(projectDir))
|
|
if err != nil {
|
|
return Link{}, err
|
|
}
|
|
var l Link
|
|
if err := json.Unmarshal(data, &l); err != nil {
|
|
return Link{}, err
|
|
}
|
|
return l, nil
|
|
}
|
|
|
|
// SaveLink writes the project link file (creates .gy/).
|
|
func SaveLink(projectDir string, l Link) error {
|
|
p := linkPath(projectDir)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
return err
|
|
}
|
|
data, _ := json.MarshalIndent(l, "", " ")
|
|
return os.WriteFile(p, data, 0o644)
|
|
}
|