1
0
mirror of synced 2026-02-13 13:06:56 +00:00

Centralise adapter types

Since the generic commands tend to share a few of the methods they
need -- at least AsClientObject -- it's worth having just one wrapper
struct for each API type, and adding methods to it where necessary.

For the automation types, I put these in auto.go.

While doing this I also did some tidying:

 - I changed the name of the wrappers to `<type>Adapter`, and the
   generic adapter to `universalAdapter` (it's only needed for delete,
   so far).

 - I de-exported and renamed some interface methods e.g.,
   `exportItem`. They aren't needed outside the package.

Signed-off-by: Michael Bridgen <michael@weave.works>
This commit is contained in:
Michael Bridgen
2020-12-07 17:30:41 +00:00
parent f316aff2d3
commit 2bb09697ce
14 changed files with 167 additions and 170 deletions

View File

@@ -48,21 +48,21 @@ func init() {
// exportable represents a type that you can fetch from the Kubernetes
// API, then tidy up for serialising.
type exportable interface {
objectContainer
Export() interface{}
adapter
export() interface{}
}
// exportableAt represents a type that has a list of values, each of
// exportableList represents a type that has a list of values, each of
// which is exportable.
type exportableAt interface {
objectContainer
Len() int
ExportAt(i int) interface{}
type exportableList interface {
adapter
len() int
exportItem(i int) interface{}
}
type exportCommand struct {
object exportable
list exportableAt
list exportableList
}
func (export exportCommand) run(cmd *cobra.Command, args []string) error {
@@ -79,18 +79,18 @@ func (export exportCommand) run(cmd *cobra.Command, args []string) error {
}
if exportAll {
err = kubeClient.List(ctx, export.list.AsClientObject(), client.InNamespace(namespace))
err = kubeClient.List(ctx, export.list.asRuntimeObject(), client.InNamespace(namespace))
if err != nil {
return err
}
if export.list.Len() == 0 {
if export.list.len() == 0 {
logger.Failuref("no objects found in %s namespace", namespace)
return nil
}
for i := 0; i < export.list.Len(); i++ {
if err = printExport(export.list.ExportAt(i)); err != nil {
for i := 0; i < export.list.len(); i++ {
if err = printExport(export.list.exportItem(i)); err != nil {
return err
}
}
@@ -100,11 +100,11 @@ func (export exportCommand) run(cmd *cobra.Command, args []string) error {
Namespace: namespace,
Name: name,
}
err = kubeClient.Get(ctx, namespacedName, export.object.AsClientObject())
err = kubeClient.Get(ctx, namespacedName, export.object.asRuntimeObject())
if err != nil {
return err
}
return printExport(export.object.Export())
return printExport(export.object.export())
}
return nil
}