github.com/rohankumardubey/draft-classic@v0.16.0/pkg/plugin/plugin.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package plugin
    17  
    18  import (
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/ghodss/yaml"
    25  )
    26  
    27  const pluginFileName = "plugin.yaml"
    28  
    29  // Downloaders represents the plugins capability if it can retrieve
    30  // charts from special sources
    31  type Downloaders struct {
    32  	// Protocols are the list of schemes from the charts URL.
    33  	Protocols []string `json:"protocols"`
    34  	// Command is the executable path with which the plugin performs
    35  	// the actual download for the corresponding Protocols
    36  	Command string `json:"command"`
    37  }
    38  
    39  // Metadata describes a plugin.
    40  //
    41  // This is the plugin equivalent of a chart.Metadata.
    42  type Metadata struct {
    43  	// Name is the name of the plugin
    44  	Name string `json:"name"`
    45  
    46  	// Version is a SemVer 2 version of the plugin.
    47  	Version string `json:"version"`
    48  
    49  	// Usage is the single-line usage text shown in help
    50  	Usage string `json:"usage"`
    51  
    52  	// Description is a long description shown in places like `helm help`
    53  	Description string `json:"description"`
    54  
    55  	// Command is the command, as a single string.
    56  	//
    57  	// The command will be passed through environment expansion, so env vars can
    58  	// be present in this command. Unless IgnoreFlags is set, this will
    59  	// also merge the flags passed from Helm.
    60  	//
    61  	// Note that command is not executed in a shell. To do so, we suggest
    62  	// pointing the command to a shell script.
    63  	Command string `json:"command"`
    64  
    65  	// IgnoreFlags ignores any flags passed in from Helm
    66  	//
    67  	// For example, if the plugin is invoked as `helm --debug myplugin`, if this
    68  	// is false, `--debug` will be appended to `--command`. If this is true,
    69  	// the `--debug` flag will be discarded.
    70  	IgnoreFlags bool `json:"ignoreFlags"`
    71  
    72  	// UseTunnel indicates that this command needs a tunnel.
    73  	// Setting this will cause a number of side effects, such as the
    74  	// automatic setting of HELM_HOST.
    75  	UseTunnel bool `json:"useTunnel"`
    76  
    77  	// PlatformHooks are commands that will run on events based on the platform specified.
    78  	PlatformHooks map[string]Hooks `json:"hooks"`
    79  
    80  	// Downloaders field is used if the plugin supply downloader mechanism
    81  	// for special protocols.
    82  	Downloaders []Downloaders `json:"downloaders"`
    83  }
    84  
    85  // Plugin represents a plugin.
    86  type Plugin struct {
    87  	// Metadata is a parsed representation of a plugin.yaml
    88  	Metadata *Metadata
    89  	// Dir is the string path to the directory that holds the plugin.
    90  	Dir string
    91  }
    92  
    93  // PrepareCommand takes a Plugin.Command and prepares it for execution.
    94  //
    95  // It merges extraArgs into any arguments supplied in the plugin. It
    96  // returns the name of the command and an args array.
    97  //
    98  // The result is suitable to pass to exec.Command.
    99  func (p *Plugin) PrepareCommand(extraArgs []string) (string, []string) {
   100  	parts := strings.Split(p.Metadata.Command, " ")
   101  	main := parts[0]
   102  	baseArgs := []string{}
   103  	if len(parts) > 1 {
   104  		baseArgs = parts[1:]
   105  	}
   106  	if !p.Metadata.IgnoreFlags {
   107  		baseArgs = append(baseArgs, extraArgs...)
   108  	}
   109  
   110  	expandedArgs := make([]string, 0, len(baseArgs))
   111  	for _, baseArg := range baseArgs {
   112  		expandedArgs = append(expandedArgs, os.ExpandEnv(baseArg))
   113  	}
   114  
   115  	return os.ExpandEnv(main), expandedArgs
   116  }
   117  
   118  // LoadDir loads a plugin from the given directory.
   119  func LoadDir(dirname string) (*Plugin, error) {
   120  	data, err := ioutil.ReadFile(filepath.Join(dirname, pluginFileName))
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	plug := &Plugin{Dir: dirname}
   126  	if err := yaml.Unmarshal(data, &plug.Metadata); err != nil {
   127  		return nil, err
   128  	}
   129  	return plug, nil
   130  }
   131  
   132  // LoadAll loads all plugins found beneath the base directory.
   133  //
   134  // This scans only one directory level.
   135  func LoadAll(basedir string) ([]*Plugin, error) {
   136  	plugins := []*Plugin{}
   137  	// We want basedir/*/plugin.yaml
   138  	scanpath := filepath.Join(basedir, "*", pluginFileName)
   139  	matches, err := filepath.Glob(scanpath)
   140  	if err != nil {
   141  		return plugins, err
   142  	}
   143  
   144  	if matches == nil {
   145  		return plugins, nil
   146  	}
   147  
   148  	for _, yaml := range matches {
   149  		dir := filepath.Dir(yaml)
   150  		p, err := LoadDir(dir)
   151  		if err != nil {
   152  			return plugins, err
   153  		}
   154  		plugins = append(plugins, p)
   155  	}
   156  	return plugins, nil
   157  }
   158  
   159  // FindPlugins returns a list of YAML files that describe plugins.
   160  func FindPlugins(plugdirs string) ([]*Plugin, error) {
   161  	found := []*Plugin{}
   162  	// Let's get all UNIXy and allow path separators
   163  	for _, p := range filepath.SplitList(plugdirs) {
   164  		matches, err := LoadAll(p)
   165  		if err != nil {
   166  			return matches, err
   167  		}
   168  		found = append(found, matches...)
   169  	}
   170  	return found, nil
   171  }