Files
ghaymah_dev2 a993c45893
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
Add email/password login via auth.ghaymah.systems + auto-refresh
- 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
2026-10-25 02:55:10 -04:00

131 lines
3.2 KiB
Go

// Package cli implements the gy command surface.
package cli
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
"golang.org/x/term"
"gitlab.com/ghaymah/gy/internal/apiclient"
"gitlab.com/ghaymah/gy/internal/gyconfig"
)
var (
flagAPIURL string
flagToken string
flagPlain bool
flagJSON bool
)
// errNotLoggedIn is returned when no token is configured.
var errNotLoggedIn = errors.New("not logged in — run `gy login` or set GY_TOKEN")
// Execute runs the CLI and returns any fatal error (printed by main).
func Execute() error {
root := &cobra.Command{
Use: "gy",
Short: "Deploy and manage apps on Ghaymah",
Long: "gy is the Ghaymah CLI — the fastest way from code to a live URL.\nRun `gy deploy` in any project directory.",
SilenceUsage: true,
SilenceErrors: true,
}
root.PersistentFlags().StringVar(&flagAPIURL, "api-url", "", "Ghaymah gateway URL (env GY_API_URL)")
root.PersistentFlags().StringVar(&flagToken, "token", "", "Ghaymah access token (env GY_TOKEN)")
root.PersistentFlags().BoolVar(&flagPlain, "plain", false, "disable the interactive TUI")
root.PersistentFlags().BoolVar(&flagJSON, "json", false, "machine-readable JSON output")
root.AddCommand(
newLoginCmd(),
newLogoutCmd(),
newWhoamiCmd(),
newDeployCmd(),
newLogsCmd(),
newStatusCmd(),
newOpenCmd(),
newListCmd(),
newVersionCmd(),
)
return root.Execute()
}
// resolveClient builds a client from flags, env and stored credentials.
func resolveClient() *apiclient.Client {
token := flagToken
if token == "" {
token = os.Getenv("GY_TOKEN")
}
base := flagAPIURL
if base == "" {
base = os.Getenv("GY_API_URL")
}
authURL := os.Getenv("GY_AUTH_URL")
var refreshToken string
if creds, err := gyconfig.LoadCredentials(); err == nil {
if token == "" {
token = creds.Token
}
if base == "" {
base = creds.BaseURL
}
if authURL == "" {
authURL = creds.AuthURL
}
refreshToken = creds.RefreshToken
}
// Refresh a session token that is expired (or about to expire).
if token != "" && refreshToken != "" && jwtExpired(token) {
if fresh, err := apiclient.AuthRefresh(authURL, refreshToken); err == nil && fresh != "" {
token = fresh
_ = gyconfig.SaveCredentials(gyconfig.Credentials{
Token: token,
RefreshToken: refreshToken,
BaseURL: base,
AuthURL: authURL,
})
}
}
if base == "" {
base = apiclient.DefaultBaseURL
}
return apiclient.New(base, token)
}
// requireClient returns a client or the not-logged-in error.
func requireClient() (*apiclient.Client, error) {
c := resolveClient()
if c.Token == "" {
return nil, errNotLoggedIn
}
return c, nil
}
func isTTY() bool {
return term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd()))
}
func inCI() bool {
return os.Getenv("CI") != "" || os.Getenv("GITHUB_ACTIONS") != "" ||
os.Getenv("GITLAB_CI") != "" || os.Getenv("BUILDKITE") != "" || os.Getenv("CIRCLECI") != ""
}
func useTUI() bool {
if flagPlain || flagJSON || inCI() {
return false
}
return isTTY()
}
// printJSON emits v as JSON.
func printJSON(v any) error {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}