Track bundled vendor runtime sources
This commit is contained in:
60
vendor/aigcpanel/cli/internal/client.go
vendored
Normal file
60
vendor/aigcpanel/cli/internal/client.go
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// DoRequest sends a POST request to the local AigcPanel HTTP server.
|
||||
func DoRequest(cfg *AuthConfig, urlPath string, body any) (map[string]any, error) {
|
||||
var reqBody io.Reader
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request body: %w", err)
|
||||
}
|
||||
reqBody = bytes.NewReader(b)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d%s", cfg.Port, urlPath)
|
||||
req, err := http.NewRequest(http.MethodPost, url, reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+cfg.Token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request failed: %w (is AigcPanel running?)", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return nil, fmt.Errorf("unauthorized: token mismatch, restart AigcPanel and try again")
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(respBytes, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// PrintJSON outputs a value as indented JSON to stdout.
|
||||
func PrintJSON(v any) error {
|
||||
b, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
return nil
|
||||
}
|
||||
66
vendor/aigcpanel/cli/internal/config.go
vendored
Normal file
66
vendor/aigcpanel/cli/internal/config.go
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// AuthConfig holds the port and token read from cli-auth.json
|
||||
type AuthConfig struct {
|
||||
Port int `json:"port"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// userDataDir returns the Electron userData directory path matching app.getPath('userData')
|
||||
// which uses the app name "aigcpanel".
|
||||
func userDataDir() (string, error) {
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, "Library", "Application Support", "aigcpanel"), nil
|
||||
case "windows":
|
||||
appData := os.Getenv("APPDATA")
|
||||
if appData == "" {
|
||||
return "", fmt.Errorf("APPDATA environment variable not set")
|
||||
}
|
||||
return filepath.Join(appData, "aigcpanel"), nil
|
||||
default:
|
||||
// Linux: XDG_CONFIG_HOME or ~/.config
|
||||
configDir := os.Getenv("XDG_CONFIG_HOME")
|
||||
if configDir == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
configDir = filepath.Join(home, ".config")
|
||||
}
|
||||
return filepath.Join(configDir, "aigcpanel"), nil
|
||||
}
|
||||
}
|
||||
|
||||
// LoadAuthConfig reads cli-auth.json from the aigcpanel userData directory.
|
||||
func LoadAuthConfig() (*AuthConfig, error) {
|
||||
dir, err := userDataDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot determine userData directory: %w", err)
|
||||
}
|
||||
filePath := filepath.Join(dir, "cli-auth.json")
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read %s: %w (is AigcPanel running?)", filePath, err)
|
||||
}
|
||||
var cfg AuthConfig
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("invalid cli-auth.json: %w", err)
|
||||
}
|
||||
if cfg.Port == 0 || cfg.Token == "" {
|
||||
return nil, fmt.Errorf("cli-auth.json is incomplete (port=%d, token empty=%v)", cfg.Port, cfg.Token == "")
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user