2020-12-09 15:31:34 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-03 09:43:15 +00:00
|
|
|
"io/fs"
|
2020-12-09 15:31:34 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2021-06-03 09:43:15 +00:00
|
|
|
"path/filepath"
|
2020-12-09 15:31:34 +00:00
|
|
|
"runtime"
|
2021-06-03 09:43:15 +00:00
|
|
|
"strings"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-06-04 11:49:31 +00:00
|
|
|
error2 "github.com/cloudskiff/driftctl/pkg/terraform/error"
|
2021-02-09 18:43:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-03-03 16:20:25 +00:00
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/output"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HomeDirInterface interface {
|
|
|
|
Dir() (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProviderInstaller struct {
|
|
|
|
downloader ProviderDownloaderInterface
|
2021-02-08 15:42:35 +00:00
|
|
|
config ProviderConfig
|
2020-12-09 15:31:34 +00:00
|
|
|
homeDir string
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
func NewProviderInstaller(config ProviderConfig) (*ProviderInstaller, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
return &ProviderInstaller{
|
|
|
|
NewProviderDownloader(),
|
2021-02-08 15:42:35 +00:00
|
|
|
config,
|
2021-06-12 14:17:23 +00:00
|
|
|
config.ConfigDir,
|
2020-12-09 15:31:34 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
func (p *ProviderInstaller) Install() (string, error) {
|
2021-06-03 09:43:15 +00:00
|
|
|
providerDir := p.getProviderDirectory()
|
|
|
|
providerPath := p.getBinaryPath()
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
info, err := os.Stat(providerPath)
|
2021-06-03 09:43:15 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"path": providerPath,
|
2021-02-08 15:42:35 +00:00
|
|
|
}).Debug("provider not found, downloading ...")
|
2021-03-03 16:20:25 +00:00
|
|
|
output.Printf("Downloading terraform provider: %s\n", p.config.Key)
|
2020-12-09 15:31:34 +00:00
|
|
|
err := p.downloader.Download(
|
2021-02-08 15:42:35 +00:00
|
|
|
p.config.GetDownloadUrl(),
|
2020-12-09 15:31:34 +00:00
|
|
|
providerDir,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-06-04 11:49:31 +00:00
|
|
|
if notFoundErr, ok := err.(error2.ProviderNotFoundError); ok {
|
|
|
|
notFoundErr.Version = p.config.Version
|
|
|
|
return "", notFoundErr
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
logrus.Debug("Download successful")
|
|
|
|
}
|
|
|
|
|
|
|
|
if info != nil && info.IsDir() {
|
2021-02-10 13:37:59 +00:00
|
|
|
return "", errors.Errorf(
|
|
|
|
"found directory instead of provider binary in %s",
|
|
|
|
providerPath,
|
2021-02-09 18:43:39 +00:00
|
|
|
)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if info != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"path": providerPath,
|
2021-02-08 15:42:35 +00:00
|
|
|
}).Debug("Found existing provider")
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 09:43:15 +00:00
|
|
|
return p.getBinaryPath(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p ProviderInstaller) getProviderDirectory() string {
|
2021-06-23 08:33:57 +00:00
|
|
|
return path.Join(p.homeDir, fmt.Sprintf(".driftctl/plugins/%s_%s/", runtime.GOOS, runtime.GOARCH))
|
2021-06-03 09:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle postfixes in binary names
|
|
|
|
func (p *ProviderInstaller) getBinaryPath() string {
|
|
|
|
providerDir := p.getProviderDirectory()
|
|
|
|
binaryName := p.config.GetBinaryName()
|
|
|
|
_, err := os.Stat(path.Join(providerDir, binaryName))
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
_ = filepath.WalkDir(providerDir, func(filePath string, d fs.DirEntry, err error) error {
|
|
|
|
if d != nil && strings.HasPrefix(d.Name(), p.config.GetBinaryName()) {
|
|
|
|
binaryName = d.Name()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(providerDir, binaryName)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|