incorporate pr review
This commit is contained in:
parent
46710be0d8
commit
7be39689f2
2 changed files with 6 additions and 41 deletions
|
|
@ -2,9 +2,7 @@ package onboard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -51,6 +49,7 @@ func runWeixinOnboard(baseURL, proxy string, timeout time.Duration) error {
|
||||||
weixin.AuthFlowOpts{
|
weixin.AuthFlowOpts{
|
||||||
BaseURL: baseURL,
|
BaseURL: baseURL,
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
|
Proxy: proxy,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -91,15 +90,9 @@ func runWeixinOnboard(baseURL, proxy string, timeout time.Duration) error {
|
||||||
func saveWeixinConfig(token, baseURL, proxy string) error {
|
func saveWeixinConfig(token, baseURL, proxy string) error {
|
||||||
cfgPath := internal.GetConfigPath()
|
cfgPath := internal.GetConfigPath()
|
||||||
|
|
||||||
raw, err := os.ReadFile(cfgPath)
|
cfg, err := config.LoadConfig(cfgPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Config doesn't exist yet — create a minimal one
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
return writeMinimalWeixinConfig(cfgPath, token, baseURL, proxy)
|
|
||||||
}
|
|
||||||
|
|
||||||
var cfg config.Config
|
|
||||||
if err := json.Unmarshal(raw, &cfg); err != nil {
|
|
||||||
return fmt.Errorf("failed to parse config: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Channels.Weixin.Enabled = true
|
cfg.Channels.Weixin.Enabled = true
|
||||||
|
|
@ -112,36 +105,7 @@ func saveWeixinConfig(token, baseURL, proxy string) error {
|
||||||
cfg.Channels.Weixin.Proxy = proxy
|
cfg.Channels.Weixin.Proxy = proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.SaveConfig(cfgPath, &cfg)
|
return config.SaveConfig(cfgPath, cfg)
|
||||||
}
|
|
||||||
|
|
||||||
func writeMinimalWeixinConfig(cfgPath, token, baseURL, proxy string) error {
|
|
||||||
if err := os.MkdirAll(internal.GetPicoclawHome(), 0o755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
weixinCfg := map[string]any{"enabled": true, "token": token}
|
|
||||||
const defaultBase = "https://ilinkai.weixin.qq.com/"
|
|
||||||
if baseURL != "" && baseURL != defaultBase {
|
|
||||||
weixinCfg["base_url"] = baseURL
|
|
||||||
}
|
|
||||||
if proxy != "" {
|
|
||||||
weixinCfg["proxy"] = proxy
|
|
||||||
}
|
|
||||||
|
|
||||||
minimal := map[string]any{
|
|
||||||
"channels": map[string]any{"weixin": weixinCfg},
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := json.MarshalIndent(minimal, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(cfgPath, data, 0o600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("✓ Created config at %s\n", cfgPath)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printManualWeixinConfig(token, baseURL string) {
|
func printManualWeixinConfig(token, baseURL string) {
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ type AuthFlowOpts struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
BotType string
|
BotType string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
Proxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
// PerformLoginInteractive starts the Weixin QR login flow and blocks until login is successful or times out.
|
// PerformLoginInteractive starts the Weixin QR login flow and blocks until login is successful or times out.
|
||||||
|
|
@ -35,7 +36,7 @@ func PerformLoginInteractive(
|
||||||
opts.Timeout = 5 * time.Minute
|
opts.Timeout = 5 * time.Minute
|
||||||
}
|
}
|
||||||
|
|
||||||
api, err := NewApiClient(opts.BaseURL, "", "")
|
api, err := NewApiClient(opts.BaseURL, "", opts.Proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", "", "", fmt.Errorf("failed to create api client: %w", err)
|
return "", "", "", "", fmt.Errorf("failed to create api client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue