goimports on installer

Signed-off-by: zetHannes <zetHannes@outlook.com>
pull/10979/head
zetHannes 3 years ago
parent 523d0a08be
commit b9c3be1ef3

@ -16,16 +16,16 @@ limitations under the License.
package installer package installer
import ( import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"helm.sh/helm/v3/pkg/plugin" "helm.sh/helm/v3/pkg/plugin"
) )
// ErrMissingMetadata indicates that plugin.yaml is missing. // ErrMissingMetadata indicates that plugin.yaml is missing.
@ -36,57 +36,57 @@ var Debug bool
// Installer provides an interface for installing helm client plugins. // Installer provides an interface for installing helm client plugins.
type Installer interface { type Installer interface {
// Install adds a plugin. // Install adds a plugin.
Install() error Install() error
// Path is the directory of the installed plugin. // Path is the directory of the installed plugin.
Path() string Path() string
// Update updates a plugin. // Update updates a plugin.
Update() error Update() error
} }
// Install installs a plugin. // Install installs a plugin.
func Install(i Installer) error { func Install(i Installer) error {
if err := os.MkdirAll(filepath.Dir(i.Path()), 0755); err != nil { if err := os.MkdirAll(filepath.Dir(i.Path()), 0755); err != nil {
return err return err
} }
if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) { if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) {
return errors.New("plugin already exists") return errors.New("plugin already exists")
} }
return i.Install() return i.Install()
} }
// Update updates a plugin. // Update updates a plugin.
func Update(i Installer) error { func Update(i Installer) error {
if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) { if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) {
return errors.New("plugin does not exist") return errors.New("plugin does not exist")
} }
return i.Update() return i.Update()
} }
// NewForSource determines the correct Installer for the given source. // NewForSource determines the correct Installer for the given source.
func NewForSource(source, version string) (Installer, error) { func NewForSource(source, version string) (Installer, error) {
// Check if source is a local directory // Check if source is a local directory
if isLocalReference(source) { if isLocalReference(source) {
return NewLocalInstaller(source) return NewLocalInstaller(source)
} else if isRemoteHTTPArchive(source) { } else if isRemoteHTTPArchive(source) {
return NewHTTPInstaller(source) return NewHTTPInstaller(source)
} }
return NewVCSInstaller(source, version) return NewVCSInstaller(source, version)
} }
// FindSource determines the correct Installer for the given source. // FindSource determines the correct Installer for the given source.
func FindSource(location string) (Installer, error) { func FindSource(location string) (Installer, error) {
installer, err := existingVCSRepo(location) installer, err := existingVCSRepo(location)
if err != nil && err.Error() == "Cannot detect VCS" { if err != nil && err.Error() == "Cannot detect VCS" {
return installer, errors.New("cannot get information about plugin source") return installer, errors.New("cannot get information about plugin source")
} }
return installer, err return installer, err
} }
// isLocalReference checks if the source exists on the filesystem. // isLocalReference checks if the source exists on the filesystem.
func isLocalReference(source string) bool { func isLocalReference(source string) bool {
_, err := os.Stat(source) _, err := os.Stat(source)
return err == nil return err == nil
} }
// isRemoteHTTPArchive checks if the source is a http/https url and is an archive // isRemoteHTTPArchive checks if the source is a http/https url and is an archive
@ -94,42 +94,42 @@ func isLocalReference(source string) bool {
// It works by checking whether the source looks like a URL and, if it does, running a // It works by checking whether the source looks like a URL and, if it does, running a
// HEAD operation to see if the remote resource is a file that we understand. // HEAD operation to see if the remote resource is a file that we understand.
func isRemoteHTTPArchive(source string) bool { func isRemoteHTTPArchive(source string) bool {
if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") { if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") {
res, err := http.Head(source) res, err := http.Head(source)
if err != nil { if err != nil {
// If we get an error at the network layer, we can't install it. So // If we get an error at the network layer, we can't install it. So
// we return false. // we return false.
return false return false
} }
// Next, we look for the content type or content disposition headers to see // Next, we look for the content type or content disposition headers to see
// if they have matching extractors. // if they have matching extractors.
contentType := res.Header.Get("content-type") contentType := res.Header.Get("content-type")
foundSuffix, ok := mediaTypeToExtension(contentType) foundSuffix, ok := mediaTypeToExtension(contentType)
if !ok { if !ok {
// Media type not recognized // Media type not recognized
return false return false
} }
for suffix := range Extractors { for suffix := range Extractors {
if strings.HasSuffix(foundSuffix, suffix) { if strings.HasSuffix(foundSuffix, suffix) {
return true return true
} }
} }
} }
return false return false
} }
// isPlugin checks if the directory contains a plugin.yaml file. // isPlugin checks if the directory contains a plugin.yaml file.
func isPlugin(dirname string) bool { func isPlugin(dirname string) bool {
_, err := os.Stat(filepath.Join(dirname, plugin.PluginFileName)) _, err := os.Stat(filepath.Join(dirname, plugin.PluginFileName))
return err == nil return err == nil
} }
var logger = log.New(os.Stderr, "[debug] ", log.Lshortfile) var logger = log.New(os.Stderr, "[debug] ", log.Lshortfile)
func debug(format string, args ...interface{}) { func debug(format string, args ...interface{}) {
if Debug { if Debug {
logger.Output(2, fmt.Sprintf(format, args...)) logger.Output(2, fmt.Sprintf(format, args...))
} }
} }
Loading…
Cancel
Save