/
extensions.go
83 lines (70 loc) · 2.3 KB
/
extensions.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2023 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
// Package extensions contains a set of subcmd extensions that can
// be used by gleancli implementations. Each such extension is a
// submodule so that its dependencies are imported only by
// gleancli implementations that use it.
package config
import (
"context"
"cloudeng.io/cmdutil/subcmd"
"cloudeng.io/file/checkpoint"
"cloudeng.io/webapi/operations"
"gopkg.in/yaml.v3"
)
type ExtensionOptions struct {
Glean
CreateStoreFS func(ctx context.Context, path string, cfg yaml.Node) (operations.FS, error)
CreateCheckpointOp func(ctx context.Context, path string, cfg yaml.Node) (checkpoint.Operation, error)
}
type Extension interface {
subcmd.Extension
SetOptions(ExtensionOptions)
Options() ExtensionOptions
AuthConfigType() any
ServiceConfigType() any
}
type extension struct {
subcmd.Extension
options ExtensionOptions
authCfgType any
serviceCfgType any
}
func (e *extension) SetOptions(opts ExtensionOptions) {
e.options = opts
}
func (e *extension) Options() ExtensionOptions {
return e.options
}
func (e *extension) AuthConfigType() any {
return e.authCfgType
}
func (e *extension) ServiceConfigType() any {
return e.serviceCfgType
}
type ExtensionSpec struct {
Name string // Name of the extension
CmdSpec string // YAML subcmd spec
AuthCfg any // used for describing the auth configuration
ServiceCfg any // used for describing the service configuration
// called to add the command to its parent cmdSet.
AddFunc func(extension Extension, cmdSet *subcmd.CommandSetYAML, parents []string) error
}
func NewExtensionC(spec ExtensionSpec, parents []string) Extension {
ext := &extension{
authCfgType: spec.AuthCfg,
serviceCfgType: spec.ServiceCfg,
}
ext.Extension = subcmd.NewExtension(spec.Name, spec.CmdSpec, func(cmdSet *subcmd.CommandSetYAML) error {
return spec.AddFunc(ext, cmdSet, parents)
})
return ext
}
func NewExtension(name, spec string, authCfgType, serviceCfgType any, appendFn func(cmdSet *subcmd.CommandSetYAML) error) Extension {
return &extension{
Extension: subcmd.NewExtension(name, spec, appendFn),
serviceCfgType: serviceCfgType,
authCfgType: authCfgType,
}
}