get.porter.sh/porter@v1.3.0/cmd/porter/plugins.go (about) 1 package main 2 3 import ( 4 "get.porter.sh/porter/pkg/pkgmgmt" 5 "get.porter.sh/porter/pkg/plugins" 6 "get.porter.sh/porter/pkg/porter" 7 "github.com/spf13/cobra" 8 ) 9 10 func buildPluginsCommands(p *porter.Porter) *cobra.Command { 11 cmd := &cobra.Command{ 12 Use: "plugins", 13 Aliases: []string{"plugin"}, 14 Short: "Plugin commands. Plugins enable Porter to work on different cloud providers and systems.", 15 Annotations: map[string]string{ 16 "group": "resource", 17 }, 18 } 19 20 cmd.AddCommand(buildPluginsListCommand(p)) 21 cmd.AddCommand(buildPluginSearchCommand(p)) 22 cmd.AddCommand(buildPluginShowCommand(p)) 23 cmd.AddCommand(BuildPluginInstallCommand(p)) 24 cmd.AddCommand(BuildPluginUninstallCommand(p)) 25 cmd.AddCommand(buildPluginRunCommand(p)) 26 27 return cmd 28 } 29 30 func buildPluginsListCommand(p *porter.Porter) *cobra.Command { 31 opts := porter.PrintPluginsOptions{} 32 33 cmd := &cobra.Command{ 34 Use: "list", 35 Short: "List installed plugins", 36 PreRunE: func(cmd *cobra.Command, args []string) error { 37 return opts.ParseFormat() 38 }, 39 RunE: func(cmd *cobra.Command, args []string) error { 40 return p.PrintPlugins(cmd.Context(), opts) 41 }, 42 } 43 44 cmd.Flags().StringVarP(&opts.RawFormat, "output", "o", "plaintext", 45 "Output format, allowed values are: plaintext, json, yaml") 46 47 return cmd 48 } 49 50 func buildPluginSearchCommand(p *porter.Porter) *cobra.Command { 51 opts := porter.SearchOptions{ 52 Type: "plugin", 53 } 54 55 cmd := &cobra.Command{ 56 Use: "search [QUERY]", 57 Short: "Search available plugins", 58 Long: `Search available plugins. You can specify an optional plugin name query, where the results are filtered by plugins whose name contains the query term. 59 60 By default the community plugin index at https://cdn.porter.sh/plugins/index.json is searched. To search from a mirror, set the environment variable PORTER_MIRROR, or mirror in the Porter config file, with the value to replace https://cdn.porter.sh with.`, 61 Example: ` porter plugin search 62 porter plugin search azure 63 porter plugin search -o json`, 64 PreRunE: func(cmd *cobra.Command, args []string) error { 65 return opts.Validate(args) 66 }, 67 RunE: func(cmd *cobra.Command, args []string) error { 68 return p.SearchPackages(opts) 69 }, 70 } 71 72 flags := cmd.Flags() 73 flags.StringVarP(&opts.RawFormat, "output", "o", "plaintext", 74 "Output format, allowed values are: plaintext, json, yaml") 75 flags.StringVar(&opts.Mirror, "mirror", pkgmgmt.DefaultPackageMirror, 76 "Mirror of official Porter assets") 77 78 return cmd 79 } 80 81 func buildPluginShowCommand(p *porter.Porter) *cobra.Command { 82 opts := porter.ShowPluginOptions{} 83 84 cmd := &cobra.Command{ 85 Use: "show", 86 Short: "Show details about an installed plugin", 87 PreRunE: func(cmd *cobra.Command, args []string) error { 88 return opts.Validate(args) 89 }, 90 RunE: func(cmd *cobra.Command, args []string) error { 91 return p.ShowPlugin(cmd.Context(), opts) 92 }, 93 } 94 95 cmd.Flags().StringVarP(&opts.RawFormat, "output", "o", "plaintext", 96 "Output format, allowed values are: plaintext, json, yaml") 97 98 return cmd 99 } 100 101 func BuildPluginInstallCommand(p *porter.Porter) *cobra.Command { 102 opts := plugins.InstallOptions{} 103 cmd := &cobra.Command{ 104 Use: "install NAME", 105 Short: "Install plugins", 106 Long: ` 107 Porter offers two ways to install plugins. Users can install plugins one at a time or multiple plugins through a plugins definition file. 108 109 Below command will install one plugin: 110 111 porter plugins install NAME [flags] 112 113 To install multiple plugins at once, users can pass a file to the install command through --file flag: 114 115 porter plugins install --file plugins.yaml 116 117 The file format for the plugins.yaml can be found here: https://porter.sh/reference/file-formats/#plugins 118 119 By default plugins are downloaded from the official Porter plugin feed at https://cdn.porter.sh/plugins/atom.xml. To download from a mirror, set the environment variable PORTER_MIRROR, or mirror in the Porter config file, with the value to replace https://cdn.porter.sh with.`, 120 Example: ` porter plugin install azure 121 porter plugin install azure --url https://cdn.porter.sh/plugins/azure 122 porter plugin install azure --feed-url https://cdn.porter.sh/plugins/atom.xml 123 porter plugin install azure --version v0.8.2-beta.1 124 porter plugin install azure --version canary 125 porter plugin install --file plugins.yaml --feed-url https://cdn.porter.sh/plugins/atom.xml 126 porter plugin install --file plugins.yaml --mirror https://cdn.porter.sh`, 127 PreRunE: func(cmd *cobra.Command, args []string) error { 128 return opts.Validate(args, p.Context) 129 }, 130 RunE: func(cmd *cobra.Command, args []string) error { 131 return p.InstallPlugin(cmd.Context(), opts) 132 }, 133 } 134 135 flags := cmd.Flags() 136 flags.StringVarP(&opts.Version, "version", "v", "latest", 137 "The plugin version. This can either be a version number, or a tagged release like 'latest' or 'canary'") 138 flags.StringVar(&opts.URL, "url", "", 139 "URL from where the plugin can be downloaded, for example https://github.com/org/proj/releases/downloads") 140 flags.StringVar(&opts.FeedURL, "feed-url", "", 141 "URL of an atom feed where the plugin can be downloaded. Defaults to the official Porter plugin feed.") 142 flags.StringVar(&opts.Mirror, "mirror", pkgmgmt.DefaultPackageMirror, 143 "Mirror of official Porter assets") 144 flags.StringVarP(&opts.File, "file", "f", "", 145 "Path to porter plugins config file.") 146 147 return cmd 148 } 149 150 func BuildPluginUninstallCommand(p *porter.Porter) *cobra.Command { 151 opts := pkgmgmt.UninstallOptions{} 152 cmd := &cobra.Command{ 153 Use: "uninstall NAME", 154 Short: "Uninstall a plugin", 155 Example: ` porter plugin uninstall azure`, 156 PreRunE: func(cmd *cobra.Command, args []string) error { 157 return opts.Validate(args) 158 }, 159 RunE: func(cmd *cobra.Command, args []string) error { 160 return p.UninstallPlugin(cmd.Context(), opts) 161 }, 162 } 163 164 return cmd 165 } 166 167 func buildPluginRunCommand(p *porter.Porter) *cobra.Command { 168 var opts porter.RunInternalPluginOpts 169 cmd := &cobra.Command{ 170 Use: "run PLUGIN_KEY", 171 Short: "Serve internal plugins", 172 RunE: func(cmd *cobra.Command, args []string) error { 173 err := opts.ApplyArgs(args) 174 if err != nil { 175 return err 176 } 177 return p.RunInternalPlugins(cmd.Context(), opts) 178 }, 179 Hidden: true, // This should ALWAYS be hidden, it is not a user-facing command 180 } 181 182 return cmd 183 }