GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ucloud-cli/hack/snapshot/completion.go at master · ucloud/ucloud-cli Expand file tree/ Copy pathcompletion.goMore file actions 114 lines (103 loc) · 3.97 KB / Copy pathcompletion.goFile metadata and controls114 lines (103 loc) · 3.97 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 package snapshot import ( "fmt" "sort" "strings" "github.com/spf13/cobra" "github.com/spf13/pflag" ) // completionResult holds the outcome of classifying a flag's completion func. type completionResult struct { registered bool // false → no completion func registered; skip. isDynamic bool // true → completion requires network (BizClient); record as "dynamic". candidates []string // non-nil when isDynamic=false and registered=true. } // classifyFlag invokes the completion func for the named flag and classifies it // as static (fixed candidate set) or dynamic (requires network). // // Dynamic detection: SetCompletion closures touch the network-backing globals // which the test nils after tree construction, causing a nil-pointer panic. // Platform (cmd) closures dereference platform.BizClient; product (products/mysql) // closures go through cli.NewServiceClient, which builds an SDK client from // platform.ClientConfig — so the test nils both (see TestWriteCompletionBaseline). // We recover from the panic and mark the flag dynamic. A closure may also // signal dynamic explicitly by returning cobra.ShellCompDirectiveError. // SetFlagValues closures return a fixed slice and never touch those globals, so // they succeed without panicking and are recorded as static. func classifyFlag(c *cobra.Command, flagName string) completionResult { fn, ok := c.GetFlagCompletionFunc(flagName) if !ok { return completionResult{} } var isDynamic bool var candidates []string func() { defer func() { if r := recover(); r != nil { isDynamic = true } }() results, directive := fn(c, []string{}, "") if directive == cobra.ShellCompDirectiveError { isDynamic = true return } candidates = results }() return completionResult{registered: true, isDynamic: isDynamic, candidates: candidates} } // RenderCompletion returns a deterministic text dump of completion registrations // for the entire cobra command tree rooted at root. // // Format (one line per flag that has a registered completion func): // // <CommandPath>\t<flagName>\tstatic[\t<comma-joined sorted candidates>] // <CommandPath>\t<flagName>\tdynamic // // Flags with no registered completion are omitted. // Subcommands are visited in sorted order; flags are visited in sorted order. func RenderCompletion(root *cobra.Command) string { return RenderCompletionPlatform(root, nil) } // RenderCompletionPlatform is RenderCompletion minus the top-level subtrees // named in skip. Product-claimed top-level commands are guarded by their own // goldens under products/<name>/testdata/, so the platform golden must not // duplicate them; only direct children of root are ever pruned. func RenderCompletionPlatform(root *cobra.Command, skip map[string]bool) string { var b strings.Builder var walk func(c *cobra.Command, depth int) walk = func(c *cobra.Command, depth int) { // Collect all flags on this command (non-persistent only; persistent flags // are registered on the defining command and appear there too). var fs []*pflag.Flag c.Flags().VisitAll(func(f *pflag.Flag) { fs = append(fs, f) }) sort.Slice(fs, func(i, j int) bool { return fs[i].Name < fs[j].Name }) for _, f := range fs { r := classifyFlag(c, f.Name) if !r.registered { continue // no completion func → skip. } if r.isDynamic { // Registered but requires network/BizClient. fmt.Fprintf(&b, "%s\t%s\tdynamic\n", c.CommandPath(), f.Name) } else { // Static enum — sort candidates for determinism. sorted := append([]string(nil), r.candidates...) sort.Strings(sorted) if len(sorted) == 0 { fmt.Fprintf(&b, "%s\t%s\tstatic\n", c.CommandPath(), f.Name) continue } fmt.Fprintf(&b, "%s\t%s\tstatic\t%s\n", c.CommandPath(), f.Name, strings.Join(sorted, ",")) } } ch := c.Commands() sort.Slice(ch, func(i, j int) bool { return ch[i].Use < ch[j].Use }) for _, x := range ch { if depth == 0 && skip[x.Name()] { continue } walk(x, depth+1) } } walk(root, 0) return b.String() } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |