From 872acf3d55960a12f7724cfd6f6a91bab9dc9d65 Mon Sep 17 00:00:00 2001 From: FelixFan1992 Date: Wed, 8 Apr 2026 17:23:57 -0400 Subject: [PATCH 1/3] clean up setup envs --- bindings/bind/compile.go | 43 +++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/bindings/bind/compile.go b/bindings/bind/compile.go index b8d90450..10656397 100644 --- a/bindings/bind/compile.go +++ b/bindings/bind/compile.go @@ -968,41 +968,34 @@ type suiEnv struct { func setupSuiEnv(alias, rpcURL string) error { // Step 1 — Fetch all current envs via CLI + // Output format: [["alias", {"rpc": "...", "ws": ..., "basic_auth": ...}], ...] cmd := exec.Command("sui", "client", "envs", "--json") out, err := cmd.Output() if err != nil { return fmt.Errorf("failed to list Sui environments: %w", err) } - outStr := string(out) - idxFront := strings.Index(outStr, "testnet") - if idxFront == -1 { - return fmt.Errorf("testnet environment not found") - } - - idxBack := strings.LastIndex(outStr, "testnet") - if idxBack == -1 { - return fmt.Errorf("testnet environment not found") - } - outTrimmed := string(out[idxFront+len("testnet")+1:idxBack-5]) + "]" - var parsed []any - if err := json.Unmarshal([]byte(outTrimmed), &parsed); err != nil { - return fmt.Errorf("failed to parse envs JSON: %w\nOutput:\n%s", err, outTrimmed) + var rawEnvs [][]json.RawMessage + if err := json.Unmarshal(out, &rawEnvs); err != nil { + return fmt.Errorf("failed to parse envs JSON: %w\nOutput:\n%s", err, string(out)) } var envList []suiEnv - if arr, ok := parsed[0].([]any); ok { - for _, e := range arr { - data, _ := json.Marshal(e) - var env suiEnv - if err := json.Unmarshal(data, &env); err == nil { - envList = append(envList, env) - } else { - log.Printf("failed to unmarshal env: %+v\n", err) - } + for _, rawEnv := range rawEnvs { + if len(rawEnv) < 2 { + continue } - } else { - log.Printf("parsed[0] is not []any, got %T\n", parsed[0]) + var envAlias string + if err := json.Unmarshal(rawEnv[0], &envAlias); err != nil { + continue + } + var env suiEnv + if err := json.Unmarshal(rawEnv[1], &env); err != nil { + log.Printf("failed to unmarshal env config for alias %q: %v\n", envAlias, err) + continue + } + env.Alias = envAlias + envList = append(envList, env) } // Step 2 — Check for existing alias and remove it From fa9a9b35ed2fd3d9d9016ef643fd1afc14ec250c Mon Sep 17 00:00:00 2001 From: FelixFan1992 Date: Wed, 8 Apr 2026 17:30:28 -0400 Subject: [PATCH 2/3] update --- bindings/bind/compile.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/bindings/bind/compile.go b/bindings/bind/compile.go index 10656397..343ce7ef 100644 --- a/bindings/bind/compile.go +++ b/bindings/bind/compile.go @@ -968,34 +968,32 @@ type suiEnv struct { func setupSuiEnv(alias, rpcURL string) error { // Step 1 — Fetch all current envs via CLI - // Output format: [["alias", {"rpc": "...", "ws": ..., "basic_auth": ...}], ...] + // Output format: [[{env1}, {env2}, ...], "active_alias"] + // The CLI may also print non-JSON preamble (e.g. config initialization messages). cmd := exec.Command("sui", "client", "envs", "--json") out, err := cmd.Output() if err != nil { return fmt.Errorf("failed to list Sui environments: %w", err) } - var rawEnvs [][]json.RawMessage - if err := json.Unmarshal(out, &rawEnvs); err != nil { + // Strip any non-JSON preamble by finding the first '['. + jsonBytes := out + if idx := strings.IndexByte(string(out), '['); idx > 0 { + jsonBytes = out[idx:] + } + + var parsed []json.RawMessage + if err := json.Unmarshal(jsonBytes, &parsed); err != nil { return fmt.Errorf("failed to parse envs JSON: %w\nOutput:\n%s", err, string(out)) } + if len(parsed) == 0 { + return fmt.Errorf("empty envs JSON output") + } + // First element is the array of env objects. var envList []suiEnv - for _, rawEnv := range rawEnvs { - if len(rawEnv) < 2 { - continue - } - var envAlias string - if err := json.Unmarshal(rawEnv[0], &envAlias); err != nil { - continue - } - var env suiEnv - if err := json.Unmarshal(rawEnv[1], &env); err != nil { - log.Printf("failed to unmarshal env config for alias %q: %v\n", envAlias, err) - continue - } - env.Alias = envAlias - envList = append(envList, env) + if err := json.Unmarshal(parsed[0], &envList); err != nil { + return fmt.Errorf("failed to parse env list: %w\nOutput:\n%s", err, string(parsed[0])) } // Step 2 — Check for existing alias and remove it From 490140d289d5d4eae975cd04573054a6ec447a31 Mon Sep 17 00:00:00 2001 From: FelixFan1992 Date: Wed, 8 Apr 2026 17:36:40 -0400 Subject: [PATCH 3/3] update --- bindings/bind/compile.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/bindings/bind/compile.go b/bindings/bind/compile.go index 343ce7ef..ce3a3337 100644 --- a/bindings/bind/compile.go +++ b/bindings/bind/compile.go @@ -976,18 +976,20 @@ func setupSuiEnv(alias, rpcURL string) error { return fmt.Errorf("failed to list Sui environments: %w", err) } - // Strip any non-JSON preamble by finding the first '['. - jsonBytes := out - if idx := strings.IndexByte(string(out), '['); idx > 0 { - jsonBytes = out[idx:] - } - + // The CLI may print non-JSON preamble (e.g. "create one [Y/n]?") before + // the JSON array, so try parsing from each '[' until one succeeds. + outStr := string(out) var parsed []json.RawMessage - if err := json.Unmarshal(jsonBytes, &parsed); err != nil { - return fmt.Errorf("failed to parse envs JSON: %w\nOutput:\n%s", err, string(out)) + for i := 0; i < len(outStr); i++ { + if outStr[i] == '[' { + if err := json.Unmarshal([]byte(outStr[i:]), &parsed); err == nil { + break + } + parsed = nil + } } if len(parsed) == 0 { - return fmt.Errorf("empty envs JSON output") + return fmt.Errorf("failed to parse envs JSON from output:\n%s", outStr) } // First element is the array of env objects.