@@ -43,6 +43,7 @@ import (
4343type PluginsCommand struct {
4444 service * service.PluginService
4545 pluginRegistryClient registry.Client
46+ pluginDirectory string
4647
4748 logger * dkplog.Logger
4849}
@@ -63,7 +64,8 @@ type pluginsListData struct {
6364
6465func NewPluginsCommand (logger * dkplog.Logger ) * PluginsCommand {
6566 return & PluginsCommand {
66- logger : logger ,
67+ pluginDirectory : flags .DeckhousePluginsDir ,
68+ logger : logger ,
6769 }
6870}
6971
@@ -77,6 +79,20 @@ func NewCommand(logger *dkplog.Logger) *cobra.Command {
7779 PersistentPreRun : func (_ * cobra.Command , _ []string ) {
7880 // init plugin services for subcommands after flags are parsed
7981 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+ }
8096 },
8197 }
8298
@@ -151,15 +167,15 @@ func (pc *PluginsCommand) preparePluginsListData(ctx context.Context, showInstal
151167
152168// fetchInstalledPlugins retrieves installed plugins from filesystem
153169func (pc * PluginsCommand ) fetchInstalledPlugins () ([]pluginDisplayInfo , error ) {
154- plugins , err := os .ReadDir (path .Join (flags . DeckhousePluginsDir , "plugins" ))
170+ plugins , err := os .ReadDir (path .Join (pc . pluginDirectory , "plugins" ))
155171 if err != nil {
156172 return nil , fmt .Errorf ("failed to read plugins directory: %w" , err )
157173 }
158174
159175 res := make ([]pluginDisplayInfo , 0 , len (plugins ))
160176
161177 for _ , plugin := range plugins {
162- pluginBinaryPath := path .Join (flags . DeckhousePluginsDir , "plugins" , plugin .Name (), "current" )
178+ pluginBinaryPath := path .Join (pc . pluginDirectory , "plugins" , plugin .Name (), "current" )
163179 cmd := exec .Command (pluginBinaryPath , "--version" )
164180
165181 output , err := cmd .Output ()
@@ -203,7 +219,7 @@ func (pc *PluginsCommand) fetchInstalledPlugins() ([]pluginDisplayInfo, error) {
203219}
204220
205221func (pc * PluginsCommand ) getInstalledPluginContract (pluginName string ) (* internal.Plugin , error ) {
206- contractFile := path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" , pluginName + ".json" )
222+ contractFile := path .Join (pc . pluginDirectory , "cache" , "contracts" , pluginName + ".json" )
207223
208224 file , err := os .Open (contractFile )
209225 if err != nil {
@@ -519,29 +535,10 @@ func (pc *PluginsCommand) InstallPlugin(ctx context.Context, pluginName, version
519535}
520536
521537func (pc * PluginsCommand ) installPlugin (ctx context.Context , pluginName string , version * semver.Version , useMajor int ) error {
522- // to check we can create directories here
523- // we try to create root plugins folder
524- err := os .MkdirAll (flags .DeckhousePluginsDir + "/plugins" , 0755 )
525- // if permission failed
526- if errors .Is (err , os .ErrPermission ) {
527- 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 ))
528-
529- flags .DeckhousePluginsDir , err = os .UserHomeDir ()
530- if err != nil {
531- return fmt .Errorf ("failed to receive home dir to create plugins dir: %w" , err )
532- }
533-
534- flags .DeckhousePluginsDir = path .Join (flags .DeckhousePluginsDir , ".deckhouse-cli" )
535- }
536-
537- if err != nil {
538- return fmt .Errorf ("failed to create plugin root directory: %w" , err )
539- }
540-
541538 // create plugin directory if it doesn't exist
542539 // example path: /opt/deckhouse/lib/deckhouse-cli/plugins/example-plugin
543- pluginDir := path .Join (flags . DeckhousePluginsDir , "plugins" , pluginName )
544- err = os .MkdirAll (pluginDir , 0755 )
540+ pluginDir := path .Join (pc . pluginDirectory , "plugins" , pluginName )
541+ err : = os .MkdirAll (pluginDir , 0755 )
545542 if err != nil {
546543 return fmt .Errorf ("failed to create plugin directory: %w" , err )
547544 }
@@ -635,7 +632,7 @@ func (pc *PluginsCommand) installPlugin(ctx context.Context, pluginName string,
635632
636633 // cache contract
637634 // example path: /opt/deckhouse/lib/deckhouse-cli/cache/contracts
638- contractDir := path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" )
635+ contractDir := path .Join (pc . pluginDirectory , "cache" , "contracts" )
639636 err = os .MkdirAll (contractDir , 0755 )
640637 if err != nil {
641638 return fmt .Errorf ("failed to create contract directory: %w" , err )
@@ -727,7 +724,7 @@ func (pc *PluginsCommand) pluginsUpdateAllCommand() *cobra.Command {
727724
728725 fmt .Println ("Updating all installed plugins..." )
729726
730- plugins , err := os .ReadDir (path .Join (flags . DeckhousePluginsDir , "plugins" ))
727+ plugins , err := os .ReadDir (path .Join (pc . pluginDirectory , "plugins" ))
731728 if err != nil {
732729 return fmt .Errorf ("failed to read plugins directory: %w" , err )
733730 }
@@ -759,7 +756,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
759756 pluginName := args [0 ]
760757 fmt .Printf ("Removing plugin: %s\n " , pluginName )
761758
762- pluginDir := path .Join (flags . DeckhousePluginsDir , "plugins" , pluginName )
759+ pluginDir := path .Join (pc . pluginDirectory , "plugins" , pluginName )
763760 fmt .Printf ("Removing plugin from: %s\n " , pluginDir )
764761
765762 err := os .RemoveAll (pluginDir )
@@ -769,7 +766,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
769766
770767 fmt .Println ("Cleaning up plugin files..." )
771768
772- os .Remove (path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" , pluginName + ".json" ))
769+ os .Remove (path .Join (pc . pluginDirectory , "cache" , "contracts" , pluginName + ".json" ))
773770
774771 fmt .Printf ("✓ Plugin '%s' successfully removed!\n " , pluginName )
775772
@@ -791,15 +788,15 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
791788 RunE : func (_ * cobra.Command , _ []string ) error {
792789 fmt .Println ("Removing all installed plugins..." )
793790
794- plugins , err := os .ReadDir (path .Join (flags . DeckhousePluginsDir , "plugins" ))
791+ plugins , err := os .ReadDir (path .Join (pc . pluginDirectory , "plugins" ))
795792 if err != nil {
796793 return fmt .Errorf ("failed to read plugins directory: %w" , err )
797794 }
798795
799796 fmt .Println ("Found" , len (plugins ), "plugins to remove:" )
800797
801798 for _ , plugin := range plugins {
802- pluginDir := path .Join (flags . DeckhousePluginsDir , "plugins" , plugin .Name ())
799+ pluginDir := path .Join (pc . pluginDirectory , "plugins" , plugin .Name ())
803800 fmt .Printf ("Removing plugin from: %s\n " , pluginDir )
804801
805802 err := os .RemoveAll (pluginDir )
@@ -809,7 +806,7 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
809806
810807 fmt .Printf ("Cleaning up plugin files for '%s'...\n " , plugin .Name ())
811808
812- os .Remove (path .Join (flags . DeckhousePluginsDir , "cache" , "contracts" , plugin .Name ()+ ".json" ))
809+ os .Remove (path .Join (pc . pluginDirectory , "cache" , "contracts" , plugin .Name ()+ ".json" ))
813810
814811 fmt .Printf ("✓ Plugin '%s' successfully removed!\n " , plugin .Name ())
815812 }
0 commit comments