@@ -19,6 +19,7 @@ package plugins
1919import (
2020 "context"
2121 "encoding/json"
22+ "errors"
2223 "fmt"
2324 "log/slog"
2425 "os"
@@ -42,6 +43,7 @@ import (
4243type PluginsCommand struct {
4344 service * service.PluginService
4445 pluginRegistryClient registry.Client
46+ pluginDirectory string
4547
4648 logger * dkplog.Logger
4749}
@@ -62,7 +64,8 @@ type pluginsListData struct {
6264
6365func NewPluginsCommand (logger * dkplog.Logger ) * PluginsCommand {
6466 return & PluginsCommand {
65- logger : logger ,
67+ pluginDirectory : flags .DeckhousePluginsDir ,
68+ logger : logger ,
6669 }
6770}
6871
@@ -76,6 +79,20 @@ func NewCommand(logger *dkplog.Logger) *cobra.Command {
7679 PersistentPreRun : func (_ * cobra.Command , _ []string ) {
7780 // init plugin services for subcommands after flags are parsed
7881 pc .InitPluginServices ()
82+
83+ err := os .MkdirAll (flags .DeckhousePluginsDir + "/plugins" , 0755 )
84+ // if permission failed
85+ if errors .Is (err , os .ErrPermission ) {
86+ pc .logger .Warn ("use homedir instead of default d8 plugins path in '/opt/deckhouse/lib/deckhouse-cli'" , slog .String ("new_path" , flags .DeckhousePluginsDir ), dkplog .Err (err ))
87+
88+ newPluginDirectory , err := os .UserHomeDir ()
89+ if err != nil {
90+ logger .Warn ("failed to receive home dir to create plugins dir" , slog .String ("error" , err .Error ()))
91+ return
92+ }
93+
94+ pc .pluginDirectory = path .Join (newPluginDirectory , ".deckhouse-cli" )
95+ }
7996 },
8097 }
8198
@@ -150,15 +167,15 @@ func (pc *PluginsCommand) preparePluginsListData(ctx context.Context, showInstal
150167
151168// fetchInstalledPlugins retrieves installed plugins from filesystem
152169func (pc * PluginsCommand ) fetchInstalledPlugins () ([]pluginDisplayInfo , error ) {
153- plugins , err := os .ReadDir (path .Join (flags . DeckhousePluginsDir , "plugins" ))
170+ plugins , err := os .ReadDir (path .Join (pc . pluginDirectory , "plugins" ))
154171 if err != nil {
155172 return nil , fmt .Errorf ("failed to read plugins directory: %w" , err )
156173 }
157174
158175 res := make ([]pluginDisplayInfo , 0 , len (plugins ))
159176
160177 for _ , plugin := range plugins {
161- pluginBinaryPath := path .Join (flags . DeckhousePluginsDir , "plugins" , plugin .Name (), "current" )
178+ pluginBinaryPath := path .Join (pc . pluginDirectory , "plugins" , plugin .Name (), "current" )
162179 cmd := exec .Command (pluginBinaryPath , "--version" )
163180
164181 output , err := cmd .Output ()
@@ -202,7 +219,7 @@ func (pc *PluginsCommand) fetchInstalledPlugins() ([]pluginDisplayInfo, error) {
202219}
203220
204221func (pc * PluginsCommand ) getInstalledPluginContract (pluginName string ) (* internal.Plugin , error ) {
205- contractFile := path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" , pluginName + ".json" )
222+ contractFile := path .Join (pc . pluginDirectory , "cache" , "contracts" , pluginName + ".json" )
206223
207224 file , err := os .Open (contractFile )
208225 if err != nil {
@@ -520,7 +537,7 @@ func (pc *PluginsCommand) InstallPlugin(ctx context.Context, pluginName, version
520537func (pc * PluginsCommand ) installPlugin (ctx context.Context , pluginName string , version * semver.Version , useMajor int ) error {
521538 // create plugin directory if it doesn't exist
522539 // example path: /opt/deckhouse/lib/deckhouse-cli/plugins/example-plugin
523- pluginDir := path .Join (flags . DeckhousePluginsDir , "plugins" , pluginName )
540+ pluginDir := path .Join (pc . pluginDirectory , "plugins" , pluginName )
524541 err := os .MkdirAll (pluginDir , 0755 )
525542 if err != nil {
526543 return fmt .Errorf ("failed to create plugin directory: %w" , err )
@@ -615,7 +632,7 @@ func (pc *PluginsCommand) installPlugin(ctx context.Context, pluginName string,
615632
616633 // cache contract
617634 // example path: /opt/deckhouse/lib/deckhouse-cli/cache/contracts
618- contractDir := path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" )
635+ contractDir := path .Join (pc . pluginDirectory , "cache" , "contracts" )
619636 err = os .MkdirAll (contractDir , 0755 )
620637 if err != nil {
621638 return fmt .Errorf ("failed to create contract directory: %w" , err )
@@ -707,7 +724,7 @@ func (pc *PluginsCommand) pluginsUpdateAllCommand() *cobra.Command {
707724
708725 fmt .Println ("Updating all installed plugins..." )
709726
710- plugins , err := os .ReadDir (path .Join (flags . DeckhousePluginsDir , "plugins" ))
727+ plugins , err := os .ReadDir (path .Join (pc . pluginDirectory , "plugins" ))
711728 if err != nil {
712729 return fmt .Errorf ("failed to read plugins directory: %w" , err )
713730 }
@@ -739,7 +756,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
739756 pluginName := args [0 ]
740757 fmt .Printf ("Removing plugin: %s\n " , pluginName )
741758
742- pluginDir := path .Join (flags . DeckhousePluginsDir , "plugins" , pluginName )
759+ pluginDir := path .Join (pc . pluginDirectory , "plugins" , pluginName )
743760 fmt .Printf ("Removing plugin from: %s\n " , pluginDir )
744761
745762 err := os .RemoveAll (pluginDir )
@@ -749,7 +766,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
749766
750767 fmt .Println ("Cleaning up plugin files..." )
751768
752- os .Remove (path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" , pluginName + ".json" ))
769+ os .Remove (path .Join (pc . pluginDirectory , "cache" , "contracts" , pluginName + ".json" ))
753770
754771 fmt .Printf ("✓ Plugin '%s' successfully removed!\n " , pluginName )
755772
@@ -771,15 +788,15 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
771788 RunE : func (_ * cobra.Command , _ []string ) error {
772789 fmt .Println ("Removing all installed plugins..." )
773790
774- plugins , err := os .ReadDir (path .Join (flags . DeckhousePluginsDir , "plugins" ))
791+ plugins , err := os .ReadDir (path .Join (pc . pluginDirectory , "plugins" ))
775792 if err != nil {
776793 return fmt .Errorf ("failed to read plugins directory: %w" , err )
777794 }
778795
779796 fmt .Println ("Found" , len (plugins ), "plugins to remove:" )
780797
781798 for _ , plugin := range plugins {
782- pluginDir := path .Join (flags . DeckhousePluginsDir , "plugins" , plugin .Name ())
799+ pluginDir := path .Join (pc . pluginDirectory , "plugins" , plugin .Name ())
783800 fmt .Printf ("Removing plugin from: %s\n " , pluginDir )
784801
785802 err := os .RemoveAll (pluginDir )
@@ -789,7 +806,7 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
789806
790807 fmt .Printf ("Cleaning up plugin files for '%s'...\n " , plugin .Name ())
791808
792- os .Remove (path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" , plugin .Name ()+ ".json" ))
809+ os .Remove (path .Join (pc . pluginDirectory , "cache" , "contracts" , plugin .Name ()+ ".json" ))
793810
794811 fmt .Printf ("✓ Plugin '%s' successfully removed!\n " , plugin .Name ())
795812 }
0 commit comments