-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
plugin_dirs.go
86 lines (75 loc) · 2.58 KB
/
plugin_dirs.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package workdir
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
const PluginPathFilename = "plugin_path"
// ProviderLocalCacheDir returns the directory we'll use as the
// working-directory-specific local cache of providers.
//
// The provider installer's job is to make sure that all providers needed for
// a particular working directory are available in this cache directory. No
// other component may write here, and in particular a Dir object itself
// never reads or writes into this directory, instead just delegating all of
// that responsibility to other components.
//
// Typically, the caller will ultimately pass the result of this method either
// directly or indirectly into providercache.NewDir, to get an object
// responsible for managing the contents.
func (d *Dir) ProviderLocalCacheDir() string {
return filepath.Join(d.dataDir, "providers")
}
// ForcedPluginDirs returns a list of directories to use to find plugins,
// instead of the default locations.
//
// Returns an zero-length list and no error in the normal case where there
// are no overridden search directories. If ForcedPluginDirs returns a
// non-empty list with no errors then the result totally replaces the default
// search directories.
func (d *Dir) ForcedPluginDirs() ([]string, error) {
raw, err := ioutil.ReadFile(filepath.Join(d.dataDir, PluginPathFilename))
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
var pluginPath []string
if err := json.Unmarshal(raw, &pluginPath); err != nil {
return nil, err
}
return pluginPath, nil
}
// SetForcedPluginDirs records an overridden list of directories to search
// to find plugins, instead of the default locations. See ForcePluginDirs
// for more information.
//
// Pass a zero-length list to deactivate forced plugin directories altogether,
// thus allowing the working directory to return to using the default
// search directories.
func (d *Dir) SetForcedPluginDirs(dirs []string) error {
filePath := filepath.Join(d.dataDir, PluginPathFilename)
switch {
case len(dirs) == 0:
err := os.Remove(filePath)
if !os.IsNotExist(err) {
return err
}
return nil
default:
// We'll ignore errors from this one, because if we fail to create
// the directory then we'll fail to create the file below too,
// and that subsequent error will more directly reflect what we
// are trying to do here.
d.ensureDataDir()
raw, err := json.MarshalIndent(dirs, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filePath, raw, 0644)
}
}