/
personality.go
68 lines (57 loc) · 1.73 KB
/
personality.go
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
package starlark
import (
"fmt"
"sort"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/spf13/pflag"
"golang.org/x/exp/maps"
)
var (
// Personalities are modes of running Starpach scripts, like "debug", "build", "test", etc.
// Scripts of a certain personality may have predefined bindings for that personality; debug
// has "dump()" predefined, for example.
personalities = map[string]Options{}
)
// personality is a type that holds a personality set from the command line.
type personality struct {
Name string
Options Options
}
var _ pflag.Value = (*personality)(nil)
func (f *personality) Set(x string) error {
p, ok := personalities[x]
if !ok {
ps := maps.Keys(personalities)
sort.Strings(ps)
return errors.Errorf("no personality %v; try one of %v", x, ps)
}
f.Name = x
f.Options = p
return nil
}
func (f *personality) String() string {
return f.Name
}
func (f *personality) Type() string {
return "personality"
}
// Personality is the personality selected from the command line.
var Personality = personality{
Name: "",
Options: Options{},
}
// RegisterPersonality registers a personality; intended to be called from the init() section of
// modules that provide starlark bindings.
func RegisterPersonality(name string, opts Options) {
if _, ok := personalities[name]; ok {
panic(fmt.Sprintf("personality %q already registered", name))
}
personalities[name] = opts
}
// UsePersonalityFlag registers DefaultPersonality with the provided flagset.
func UsePersonalityFlag(flagset *pflag.FlagSet) {
ps := maps.Keys(personalities)
sort.Strings(ps)
flagset.VarP(&Personality, "personality", "p",
fmt.Sprintf("Start the interpreter with the personality of a certain type of script; one of %v", ps))
}