github.com/sap/cf-mta-plugin@v2.6.3+incompatible/multiapps_plugin.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	defaultlog "log"
     7  	"os"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/commands"
    12  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
    13  	"github.com/cloudfoundry/cli/plugin"
    14  )
    15  
    16  // Version is the version of the CLI plugin. It is injected on linking time.
    17  var Version string = "0.0.0"
    18  
    19  // MultiappsPlugin represents a cf CLI plugin for executing operations on MTAs
    20  type MultiappsPlugin struct{}
    21  
    22  // Commands contains the commands supported by this plugin
    23  var Commands = []commands.Command{
    24  	commands.NewDeployCommand(),
    25  	commands.NewBlueGreenDeployCommand(),
    26  	commands.NewMtasCommand(),
    27  	commands.NewDmolCommand(),
    28  	commands.NewUndeployCommand(),
    29  	commands.NewMtaCommand(),
    30  	commands.NewMtaOperationsCommand(),
    31  	commands.NewPurgeConfigCommand(),
    32  }
    33  
    34  // Run runs this plugin
    35  func (p *MultiappsPlugin) Run(cliConnection plugin.CliConnection, args []string) {
    36  	disableStdOut()
    37  	if args[0] == "CLI-MESSAGE-UNINSTALL" {
    38  		return
    39  	}
    40  	command, err := findCommand(args[0])
    41  	if err != nil {
    42  		log.Fatalln(err)
    43  	}
    44  	command.Initialize(command.GetPluginCommand().Name, cliConnection)
    45  	status := command.Execute(args[1:])
    46  	if status == commands.Failure {
    47  		os.Exit(1)
    48  	}
    49  }
    50  
    51  // GetMetadata returns the metadata of this plugin
    52  func (p *MultiappsPlugin) GetMetadata() plugin.PluginMetadata {
    53  	metadata := plugin.PluginMetadata{
    54  		Name:          "multiapps",
    55  		Version:       parseSemver(Version),
    56  		MinCliVersion: plugin.VersionType{Major: 6, Minor: 7, Build: 0},
    57  	}
    58  	for _, command := range Commands {
    59  		metadata.Commands = append(metadata.Commands, command.GetPluginCommand())
    60  	}
    61  	return metadata
    62  }
    63  
    64  func main() {
    65  	plugin.Start(new(MultiappsPlugin))
    66  }
    67  
    68  func disableStdOut() {
    69  	defaultlog.SetFlags(0)
    70  	defaultlog.SetOutput(ioutil.Discard)
    71  }
    72  
    73  func findCommand(name string) (commands.Command, error) {
    74  	for _, command := range Commands {
    75  		pluginCommand := command.GetPluginCommand()
    76  		if pluginCommand.Name == name || pluginCommand.Alias == name {
    77  			return command, nil
    78  		}
    79  	}
    80  	return nil, fmt.Errorf("Could not find command with name '%s'", name)
    81  }
    82  
    83  func parseSemver(version string) plugin.VersionType {
    84  	mmb := strings.Split(version, ".")
    85  	if len(mmb) != 3 {
    86  		panic("invalid version: " + version)
    87  	}
    88  	major, _ := strconv.Atoi(mmb[0])
    89  	minor, _ := strconv.Atoi(mmb[1])
    90  	build, _ := strconv.Atoi(mmb[2])
    91  
    92  	return plugin.VersionType{
    93  		Major: major,
    94  		Minor: minor,
    95  		Build: build,
    96  	}
    97  }