| 306 | } |
| 307 | |
| 308 | func configInitRun(opts *ConfigInitOptions) error { |
| 309 | f := opts.Factory |
| 310 | |
| 311 | // Read secret from stdin if --app-secret-stdin is set |
| 312 | if opts.AppSecretStdin { |
| 313 | scanner := bufio.NewScanner(f.IOStreams.In) |
| 314 | if !scanner.Scan() { |
| 315 | if err := scanner.Err(); err != nil { |
| 316 | return errs.NewValidationError(errs.SubtypeInvalidArgument, "failed to read secret from stdin: %v", err).WithCause(err) |
| 317 | } |
| 318 | return errs.NewValidationError(errs.SubtypeInvalidArgument, "stdin is empty, expected app secret") |
| 319 | } |
| 320 | opts.appSecret = strings.TrimSpace(scanner.Text()) |
| 321 | if opts.appSecret == "" { |
| 322 | return errs.NewValidationError(errs.SubtypeInvalidArgument, "app secret read from stdin is empty") |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | existing, err := core.LoadMultiAppConfig() |
| 327 | if err != nil { |
| 328 | existing = nil // treat as empty |
| 329 | } |
| 330 | |
| 331 | // Validate --profile name if set |
| 332 | if opts.ProfileName != "" { |
| 333 | if err := core.ValidateProfileName(opts.ProfileName); err != nil { |
| 334 | return errs.NewValidationError(errs.SubtypeInvalidArgument, "%v", err).WithCause(err) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Mode 1: Non-interactive |
| 339 | if opts.AppID != "" && opts.appSecret != "" { |
| 340 | brand := parseBrand(opts.Brand) |
| 341 | secret, err := core.ForStorage(opts.AppID, core.PlainSecret(opts.appSecret), f.Keychain) |
| 342 | if err != nil { |
| 343 | return errs.NewInternalError(errs.SubtypeSDKError, "%v", err).WithCause(err) |
| 344 | } |
| 345 | if err := saveInitConfig(opts.ProfileName, existing, f, opts.AppID, secret, brand, opts.Lang); err != nil { |
| 346 | return wrapSaveConfigError(err) |
| 347 | } |
| 348 | output.PrintSuccess(f.IOStreams.ErrOut, fmt.Sprintf("Configuration saved to %s", core.GetConfigPath())) |
| 349 | printLangPreferenceConfirmation(opts) |
| 350 | output.PrintJson(f.IOStreams.Out, map[string]interface{}{"appId": opts.AppID, "appSecret": "****", "brand": brand}) |
| 351 | if err := runProbe(opts.Ctx, f, opts.AppID, opts.appSecret, brand); err != nil { |
| 352 | return err |
| 353 | } |
| 354 | return nil |
| 355 | } |
| 356 | |
| 357 | // For interactive modes, prompt language selection if --lang was not explicitly set. |
| 358 | // Picker offers 2 options (中文 / English) and drives BOTH opts.Lang |
| 359 | // (preference) and opts.UILang (TUI rendering). |
| 360 | if f.IOStreams.IsTerminal && !opts.langExplicit && !opts.hasAnyNonInteractiveFlag() { |
| 361 | lang, err := promptLangSelection() |
| 362 | if err != nil { |
| 363 | return langSelectionError(err) |
| 364 | } |
| 365 | opts.Lang = string(lang) |