-
Notifications
You must be signed in to change notification settings - Fork 22
/
inventory_plugin.go
150 lines (130 loc) · 6.2 KB
/
inventory_plugin.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"github.com/spf13/cobra"
"github.com/vmware-tanzu/tanzu-cli/cmd/plugin/builder/inventory"
"github.com/vmware-tanzu/tanzu-cli/pkg/carvelhelpers"
)
// newInventoryPluginCmd creates a new command for plugin inventory operations.
func newInventoryPluginCmd() *cobra.Command {
var inventoryPluginCmd = &cobra.Command{
Use: "plugin",
Short: "Plugin Inventory Operations",
}
inventoryPluginCmd.AddCommand(
newInventoryPluginAddCmd(),
newInventoryPluginActivateCmd(),
newInventoryPluginDeactivateCmd(),
)
return inventoryPluginCmd
}
type inventoryPluginAddFlags struct {
Repository string
InventoryImageTag string
ManifestFile string
Publisher string
Vendor string
InventoryDBFile string
DeactivatePlugins bool
ValidateOnly bool
}
func newInventoryPluginAddCmd() *cobra.Command {
var ipaFlags = &inventoryPluginAddFlags{}
var pluginAddCmd = &cobra.Command{
Use: "add",
Short: "Add the plugin to the inventory database available on the remote repository",
SilenceUsage: true,
Example: ``,
RunE: func(cmd *cobra.Command, args []string) error {
paOptions := inventory.InventoryPluginUpdateOptions{
Repository: ipaFlags.Repository,
InventoryImageTag: ipaFlags.InventoryImageTag,
ManifestFile: ipaFlags.ManifestFile,
Vendor: ipaFlags.Vendor,
Publisher: ipaFlags.Publisher,
DeactivatePlugins: ipaFlags.DeactivatePlugins,
InventoryDBFile: ipaFlags.InventoryDBFile,
ValidateOnly: ipaFlags.ValidateOnly,
ImageOperationsImpl: carvelhelpers.NewImageOperationsImpl(),
}
return paOptions.PluginAdd()
},
}
pluginAddCmd.Flags().StringVarP(&ipaFlags.Repository, "repository", "", "", "repository to publish plugin inventory image")
pluginAddCmd.Flags().StringVarP(&ipaFlags.InventoryImageTag, "plugin-inventory-image-tag", "", "latest", "tag to which plugin inventory image needs to be published")
pluginAddCmd.Flags().StringVarP(&ipaFlags.ManifestFile, "manifest", "", "", "manifest file specifying plugin details that needs to be processed")
pluginAddCmd.Flags().StringVarP(&ipaFlags.Vendor, "vendor", "", "", "name of the vendor")
pluginAddCmd.Flags().StringVarP(&ipaFlags.Publisher, "publisher", "", "", "name of the publisher")
pluginAddCmd.Flags().StringVarP(&ipaFlags.InventoryDBFile, "plugin-inventory-db-file", "", "", "local file for the inventory database")
pluginAddCmd.Flags().BoolVarP(&ipaFlags.DeactivatePlugins, "deactivate", "", false, "mark plugins as deactivated")
pluginAddCmd.Flags().BoolVarP(&ipaFlags.ValidateOnly, "validate", "", false, "validate whether plugins already exists in the plugin inventory or not")
_ = pluginAddCmd.MarkFlagRequired("repository")
_ = pluginAddCmd.MarkFlagRequired("vendor")
_ = pluginAddCmd.MarkFlagRequired("publisher")
_ = pluginAddCmd.MarkFlagRequired("manifest")
return pluginAddCmd
}
type inventoryPluginActivateDeactivateFlags struct {
Repository string
InventoryImageTag string
ManifestFile string
Publisher string
Vendor string
InventoryDBFile string
}
func newInventoryPluginActivateCmd() *cobra.Command { //nolint:dupl
pluginActivateCmd, flags := getActivateDeactivateBaseCmd()
pluginActivateCmd.Use = "activate" //nolint:goconst
pluginActivateCmd.Short = "Activate the existing plugin in the inventory database available on the remote repository"
pluginActivateCmd.Example = ""
pluginActivateCmd.RunE = func(cmd *cobra.Command, args []string) error {
piOptions := inventory.InventoryPluginUpdateOptions{
Repository: flags.Repository,
InventoryImageTag: flags.InventoryImageTag,
ManifestFile: flags.ManifestFile,
Vendor: flags.Vendor,
Publisher: flags.Publisher,
InventoryDBFile: flags.InventoryDBFile,
DeactivatePlugins: false,
ImageOperationsImpl: carvelhelpers.NewImageOperationsImpl(),
}
return piOptions.UpdatePluginActivationState()
}
return pluginActivateCmd
}
func newInventoryPluginDeactivateCmd() *cobra.Command { //nolint:dupl
pluginDeactivateCmd, flags := getActivateDeactivateBaseCmd()
pluginDeactivateCmd.Use = "deactivate" //nolint:goconst
pluginDeactivateCmd.Short = "Deactivate the existing plugin in the inventory database available on the remote repository"
pluginDeactivateCmd.Example = ""
pluginDeactivateCmd.RunE = func(cmd *cobra.Command, args []string) error {
piOptions := inventory.InventoryPluginUpdateOptions{
Repository: flags.Repository,
InventoryImageTag: flags.InventoryImageTag,
ManifestFile: flags.ManifestFile,
Vendor: flags.Vendor,
Publisher: flags.Publisher,
InventoryDBFile: flags.InventoryDBFile,
DeactivatePlugins: true,
ImageOperationsImpl: carvelhelpers.NewImageOperationsImpl(),
}
return piOptions.UpdatePluginActivationState()
}
return pluginDeactivateCmd
}
func getActivateDeactivateBaseCmd() (*cobra.Command, *inventoryPluginActivateDeactivateFlags) {
var flags = &inventoryPluginActivateDeactivateFlags{}
var activateDeactivateCmd = &cobra.Command{}
activateDeactivateCmd.SilenceUsage = true
activateDeactivateCmd.Flags().StringVarP(&flags.Repository, "repository", "", "", "repository to publish plugin inventory image")
activateDeactivateCmd.Flags().StringVarP(&flags.InventoryImageTag, "plugin-inventory-image-tag", "", "latest", "tag to which plugin inventory image needs to be published")
activateDeactivateCmd.Flags().StringVarP(&flags.ManifestFile, "manifest", "", "", "manifest file specifying plugin details that needs to be processed")
activateDeactivateCmd.Flags().StringVarP(&flags.Vendor, "vendor", "", "", "name of the vendor")
activateDeactivateCmd.Flags().StringVarP(&flags.Publisher, "publisher", "", "", "name of the publisher")
activateDeactivateCmd.Flags().StringVarP(&flags.InventoryDBFile, "plugin-inventory-db-file", "", "", "local file for the inventory database")
_ = activateDeactivateCmd.MarkFlagRequired("vendor")
_ = activateDeactivateCmd.MarkFlagRequired("publisher")
_ = activateDeactivateCmd.MarkFlagRequired("manifest")
return activateDeactivateCmd, flags
}