(cobraCmd *cobra.Command, args []string)
| 79 | var commandOnce sync.Once |
| 80 | |
| 81 | func SetPluginCommand(cobraCmd *cobra.Command, args []string) { |
| 82 | commandOnce.Do(func() { |
| 83 | pluginContextLock.Lock() |
| 84 | defer pluginContextLock.Unlock() |
| 85 | |
| 86 | if cobraCmd == nil { |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | osArgsBytes, err := json.Marshal(os.Args) |
| 91 | if err != nil { |
| 92 | logErrorf("marshal os args: %v", err) |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | pluginContext[CommandEnv] = cobraCmd.Use |
| 97 | pluginContext[CommandLineEnv] = cobraCmd.UseLine() |
| 98 | pluginContext[OsArgsEnv] = string(osArgsBytes) |
| 99 | |
| 100 | // Flags |
| 101 | flags := []string{} |
| 102 | cobraCmd.Flags().Visit(func(f *pflag.Flag) { |
| 103 | flags = append(flags, "--"+f.Name) |
| 104 | flags = append(flags, f.Value.String()) |
| 105 | }) |
| 106 | if len(flags) > 0 { |
| 107 | flagsStr, err := json.Marshal(flags) |
| 108 | if err != nil { |
| 109 | logErrorf("marshal flags: %v", err) |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | pluginContext[CommandFlagsEnv] = string(flagsStr) |
| 114 | } |
| 115 | |
| 116 | // Args |
| 117 | if len(args) > 0 { |
| 118 | argsStr, err := json.Marshal(args) |
| 119 | if err != nil { |
| 120 | logErrorf("marshal args: %v", err) |
| 121 | return |
| 122 | } |
| 123 | if string(argsStr) != "" { |
| 124 | pluginContext[CommandArgsEnv] = string(argsStr) |
| 125 | } |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | var configOnce sync.Once |
| 131 |
no test coverage detected