github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/workdir/plugin_dirs.go (about) 1 package workdir 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 ) 9 10 const PluginPathFilename = "plugin_path" 11 12 // ProviderLocalCacheDir returns the directory we'll use as the 13 // working-directory-specific local cache of providers. 14 // 15 // The provider installer's job is to make sure that all providers needed for 16 // a particular working directory are available in this cache directory. No 17 // other component may write here, and in particular a Dir object itself 18 // never reads or writes into this directory, instead just delegating all of 19 // that responsibility to other components. 20 // 21 // Typically, the caller will ultimately pass the result of this method either 22 // directly or indirectly into providercache.NewDir, to get an object 23 // responsible for managing the contents. 24 func (d *Dir) ProviderLocalCacheDir() string { 25 return filepath.Join(d.dataDir, "providers") 26 } 27 28 // ForcedPluginDirs returns a list of directories to use to find plugins, 29 // instead of the default locations. 30 // 31 // Returns an zero-length list and no error in the normal case where there 32 // are no overridden search directories. If ForcedPluginDirs returns a 33 // non-empty list with no errors then the result totally replaces the default 34 // search directories. 35 func (d *Dir) ForcedPluginDirs() ([]string, error) { 36 raw, err := ioutil.ReadFile(filepath.Join(d.dataDir, PluginPathFilename)) 37 if os.IsNotExist(err) { 38 return nil, nil 39 } 40 41 if err != nil { 42 return nil, err 43 } 44 45 var pluginPath []string 46 if err := json.Unmarshal(raw, &pluginPath); err != nil { 47 return nil, err 48 } 49 return pluginPath, nil 50 } 51 52 // SetForcedPluginDirs records an overridden list of directories to search 53 // to find plugins, instead of the default locations. See ForcePluginDirs 54 // for more information. 55 // 56 // Pass a zero-length list to deactivate forced plugin directories altogether, 57 // thus allowing the working directory to return to using the default 58 // search directories. 59 func (d *Dir) SetForcedPluginDirs(dirs []string) error { 60 61 filePath := filepath.Join(d.dataDir, PluginPathFilename) 62 switch { 63 case len(dirs) == 0: 64 err := os.Remove(filePath) 65 if !os.IsNotExist(err) { 66 return err 67 } 68 return nil 69 default: 70 // We'll ignore errors from this one, because if we fail to create 71 // the directory then we'll fail to create the file below too, 72 // and that subsequent error will more directly reflect what we 73 // are trying to do here. 74 d.ensureDataDir() 75 76 raw, err := json.MarshalIndent(dirs, "", " ") 77 if err != nil { 78 return err 79 } 80 81 return ioutil.WriteFile(filePath, raw, 0644) 82 } 83 }