mirror of
https://codeberg.org/woodpecker-plugins/go-plugin.git
synced 2025-05-09 19:29:30 +02:00
Adds two custom cli flags: - `StringMapFlag` parses JSON to string map used for plugin map options - `StringSliceFlag` parses comma-separated plugin slice options to slice and supports escaping Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/48 Reviewed-by: Patrick Schratz <pat-s@noreply.codeberg.org> Co-authored-by: Robert Kaussow <mail@thegeeklab.de> Co-committed-by: Robert Kaussow <mail@thegeeklab.de>
85 lines
1.6 KiB
Go
85 lines
1.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// StringMapFlag is a flag type which supports JSON string maps.
|
|
type (
|
|
StringMapFlag = cli.FlagBase[map[string]string, StringMapConfig, StringMap]
|
|
)
|
|
|
|
// StringMapConfig defines the configuration for string map flags.
|
|
type StringMapConfig struct {
|
|
// Any config options can be added here if needed
|
|
}
|
|
|
|
// StringMap implements the Value and ValueCreator interfaces for string maps.
|
|
type StringMap struct {
|
|
destination *map[string]string
|
|
}
|
|
|
|
// Create implements the ValueCreator interface.
|
|
func (s StringMap) Create(v map[string]string, p *map[string]string, _ StringMapConfig) cli.Value {
|
|
*p = map[string]string{}
|
|
|
|
if v != nil {
|
|
*p = v
|
|
}
|
|
|
|
return &StringMap{
|
|
destination: p,
|
|
}
|
|
}
|
|
|
|
// ToString implements the ValueCreator interface.
|
|
func (s StringMap) ToString(v map[string]string) string {
|
|
if len(v) == 0 {
|
|
return ""
|
|
}
|
|
|
|
jsonBytes, err := json.Marshal(v)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return string(jsonBytes)
|
|
}
|
|
|
|
// Set implements the flag.Value interface.
|
|
func (s *StringMap) Set(v string) error {
|
|
*s.destination = map[string]string{}
|
|
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(v), s.destination)
|
|
if err != nil {
|
|
(*s.destination)["*"] = v
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Get implements the flag.Value interface.
|
|
func (s *StringMap) Get() any {
|
|
return *s.destination
|
|
}
|
|
|
|
// String implements the flag.Value interface.
|
|
func (s *StringMap) String() string {
|
|
if s.destination == nil || len(*s.destination) == 0 {
|
|
return ""
|
|
}
|
|
|
|
jsonBytes, err := json.Marshal(*s.destination)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return string(jsonBytes)
|
|
}
|