github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/plugin/plugin_shim.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  )
     8  
     9  /**
    10  	* This function is called by the plugin to setup their server. This allows us to call Run on the plugin
    11  	* os.Args[1] port CF_CLI rpc server is running on
    12  	* os.Args[2] **OPTIONAL**
    13  		* SendMetadata - used to fetch the plugin metadata
    14  **/
    15  func Start(cmd Plugin) {
    16  	if len(os.Args) < 2 {
    17  		fmt.Printf("This cf CLI plugin is not intended to be run on its own\n\n")
    18  		os.Exit(1)
    19  	}
    20  
    21  	cliConnection := NewCliConnection(os.Args[1])
    22  	cliConnection.pingCLI()
    23  	if isMetadataRequest(os.Args) {
    24  		cliConnection.sendPluginMetadataToCliServer(cmd.GetMetadata())
    25  	} else {
    26  		if version := MinCliVersionStr(cmd.GetMetadata().MinCliVersion); version != "" {
    27  			ok := cliConnection.isMinCliVersion(version)
    28  			if !ok {
    29  				fmt.Printf("Minimum CLI version %s is required to run this plugin command\n\n", version)
    30  				os.Exit(0)
    31  			}
    32  		}
    33  
    34  		cmd.Run(cliConnection, os.Args[2:])
    35  	}
    36  }
    37  
    38  func isMetadataRequest(args []string) bool {
    39  	return len(args) == 3 && args[2] == "SendMetadata"
    40  }
    41  
    42  func MinCliVersionStr(version VersionType) string {
    43  	if version.Major == 0 && version.Minor == 0 && version.Build == 0 {
    44  		return ""
    45  	}
    46  
    47  	return strconv.Itoa(version.Major) + "." + strconv.Itoa(version.Minor) + "." + strconv.Itoa(version.Build)
    48  }