@@ -86,6 +86,7 @@ func NewClient(host string, token string, endpoint string, timeout time.Duration
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -106,9 +107,11 @@ func (c *Client) Do(
|
||||
if len(resp.Errors) > 0 {
|
||||
var msg strings.Builder
|
||||
msg.WriteString(resp.Errors[0].Message)
|
||||
|
||||
for _, e := range resp.Errors[1:] {
|
||||
msg.WriteString("; " + e.Message)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("GraphQL error: %s", msg.String())
|
||||
}
|
||||
|
||||
@@ -171,6 +174,7 @@ func (c *Client) doRequest(
|
||||
}
|
||||
|
||||
reqURL := host + c.endpoint
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot create HTTP request: %w", err)
|
||||
@@ -184,6 +188,7 @@ func (c *Client) doRequest(
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot send HTTP request: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
@@ -217,9 +222,11 @@ func (c *Client) DoUpload(
|
||||
if len(resp.Errors) > 0 {
|
||||
var msg strings.Builder
|
||||
msg.WriteString(resp.Errors[0].Message)
|
||||
|
||||
for _, e := range resp.Errors[1:] {
|
||||
msg.WriteString("; " + e.Message)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("GraphQL error: %s", msg.String())
|
||||
}
|
||||
|
||||
@@ -234,6 +241,7 @@ func (c *Client) doUploadRequest(
|
||||
file io.Reader,
|
||||
) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
writer := multipart.NewWriter(&buf)
|
||||
|
||||
// Part 1: operations
|
||||
@@ -281,6 +289,7 @@ func (c *Client) doUploadRequest(
|
||||
}
|
||||
|
||||
reqURL := host + c.endpoint
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqURL, &buf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create HTTP request: %w", err)
|
||||
@@ -294,6 +303,7 @@ func (c *Client) doUploadRequest(
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot send HTTP request: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
@@ -347,6 +357,7 @@ func (c *Client) tryRefreshToken() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot send refresh request: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
|
||||
@@ -58,6 +58,7 @@ func Paginate[T any](
|
||||
if remaining <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
vars["first"] = remaining
|
||||
|
||||
data, err := client.Do(query, vars)
|
||||
|
||||
@@ -93,15 +93,18 @@ func (c *Config) Set(key, value string) error {
|
||||
if value != "enabled" && value != "disabled" {
|
||||
return fmt.Errorf("valid values for prompt are 'enabled' or 'disabled'")
|
||||
}
|
||||
|
||||
c.Prompt = value
|
||||
case "http_timeout":
|
||||
if _, err := time.ParseDuration(value); err != nil {
|
||||
return fmt.Errorf("invalid duration for http_timeout: %w", err)
|
||||
}
|
||||
|
||||
c.HTTPTimeout = value
|
||||
default:
|
||||
return fmt.Errorf("unknown configuration key: %s", key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -123,6 +126,7 @@ func configDir() (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot determine config directory: %w", err)
|
||||
}
|
||||
|
||||
return filepath.Join(dir, "prb"), nil
|
||||
}
|
||||
|
||||
@@ -131,6 +135,7 @@ func configPath() (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(dir, "config.yaml"), nil
|
||||
}
|
||||
|
||||
@@ -145,6 +150,7 @@ func Load() (*Config, error) {
|
||||
if os.IsNotExist(err) {
|
||||
return &Config{Hosts: make(map[string]*HostConfig)}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("cannot read config file: %w", err)
|
||||
}
|
||||
|
||||
@@ -161,6 +167,7 @@ func Load() (*Config, error) {
|
||||
for host, hc := range cfg.Hosts {
|
||||
normalized[normalizeHost(host)] = hc
|
||||
}
|
||||
|
||||
cfg.Hosts = normalized
|
||||
|
||||
if cfg.ActiveHost != "" {
|
||||
@@ -206,13 +213,16 @@ func normalizeHost(host string) string {
|
||||
func (c *Config) DefaultHost() (string, *HostConfig, error) {
|
||||
if host := os.Getenv("PROBO_HOST"); host != "" {
|
||||
host = normalizeHost(host)
|
||||
|
||||
hc := &HostConfig{}
|
||||
if saved, ok := c.Hosts[host]; ok {
|
||||
*hc = *saved
|
||||
}
|
||||
|
||||
if token := os.Getenv("PROBO_TOKEN"); token != "" {
|
||||
hc.Token = token
|
||||
}
|
||||
|
||||
return host, hc, nil
|
||||
}
|
||||
|
||||
@@ -224,6 +234,7 @@ func (c *Config) DefaultHost() (string, *HostConfig, error) {
|
||||
}
|
||||
|
||||
host := hosts[0]
|
||||
|
||||
if c.ActiveHost != "" {
|
||||
if _, ok := c.Hosts[c.ActiveHost]; ok {
|
||||
host = c.ActiveHost
|
||||
|
||||
Reference in New Issue
Block a user