Refactor plugin & version subcommands (#1359)

This commit is contained in:
Ta-Ching Chen
2019-10-22 17:57:56 +08:00
committed by GitHub
parent 22de11190c
commit fed30e1b24
10 changed files with 118 additions and 87 deletions
@@ -23,7 +23,6 @@ import (
"strings"
"time"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/urfave/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,12 +31,13 @@ import (
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/cmd/environment"
_package "github.com/fission/fission/pkg/fission-cli/cmd/package"
plugincmd "github.com/fission/fission/pkg/fission-cli/cmd/plugin"
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
"github.com/fission/fission/pkg/fission-cli/cmd/support"
"github.com/fission/fission/pkg/fission-cli/cmd/version"
"github.com/fission/fission/pkg/fission-cli/log"
"github.com/fission/fission/pkg/fission-cli/plugin"
"github.com/fission/fission/pkg/fission-cli/util"
"github.com/fission/fission/pkg/info"
"github.com/fission/fission/pkg/plugin"
"github.com/fission/fission/pkg/types"
)
@@ -59,16 +59,15 @@ func NewCliApp() *cli.App {
app := cli.NewApp()
app.Name = "fission"
app.Usage = "Serverless functions for Kubernetes"
app.Version = info.Version
cli.VersionPrinter = versionPrinter
app.HideVersion = true
app.CustomAppHelpTemplate = helpTemplate
app.ExtraInfo = func() map[string]string {
info := map[string]string{}
pluginInfo := map[string]string{}
for _, pmd := range plugin.FindAll() {
names := strings.Join(append([]string{pmd.Name}, pmd.Aliases...), ", ")
info[names] = pmd.Usage
pluginInfo[names] = pmd.Usage
}
return info
return pluginInfo
}
app.Flags = []cli.Flag{
@@ -312,6 +311,10 @@ func NewCliApp() *cli.App {
{Name: "list", Usage: "List all canary configs in a namespace", Flags: []cli.Flag{canaryNamespaceFlag}, Action: canaryConfigList},
}
pluginSubCommands := []cli.Command{
{Name: "list", Usage: "List installed client plugins", Action: urfavecli.Wrapper(plugincmd.List)},
}
app.Commands = []cli.Command{
{Name: "function", Aliases: []string{"fn"}, Usage: "Create, update and manage functions", Subcommands: fnSubcommands},
{Name: "httptrigger", Aliases: []string{"ht", "route"}, Usage: "Manage HTTP triggers (routes) for functions", Subcommands: htSubcommands},
@@ -325,8 +328,9 @@ func NewCliApp() *cli.App {
{Name: "package", Aliases: []string{"pkg"}, Usage: "Manage packages", Subcommands: pkgSubCommands},
{Name: "spec", Aliases: []string{"specs"}, Usage: "Manage a declarative app specification", Subcommands: specSubCommands},
{Name: "support", Usage: "Collect an archive of diagnostic information for support", Subcommands: supportSubCommands},
cmdPlugin,
{Name: "canary-config", Aliases: []string{}, Usage: "Create, Update and manage Canary Configs", Subcommands: canarySubCommands},
{Name: "plugin", Aliases: []string{"plugins"}, Usage: "Manage Fission CLI plugins", Subcommands: pluginSubCommands},
{Name: "version", Usage: "Version information", Action: urfavecli.Wrapper(version.Version)},
}
app.Before = cliHook
@@ -335,10 +339,6 @@ func NewCliApp() *cli.App {
}
func handleNoCommand(ctx *cli.Context) error {
if ctx.GlobalBool("version") {
versionPrinter(ctx)
return nil
}
if ctx.GlobalBool("plugin") {
bs, err := json.Marshal(plugin.Metadata{
Version: info.Version,
@@ -399,16 +399,6 @@ To install it for your local Fission CLI:
}
}
func versionPrinter(_ *cli.Context) {
client := util.GetApiClient(util.GetServerUrl())
ver := util.GetVersion(client)
bs, err := yaml.Marshal(ver)
if err != nil {
log.Fatal("Error formatting versions: " + err.Error())
}
fmt.Print(string(bs))
}
func flagValueParser(args []string) error {
// all input value for flags are properly set
if len(args) == 0 {
@@ -1,5 +1,5 @@
/*
Copyright 2016 The Fission Authors.
Copyright 2019 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -14,32 +14,31 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package fission_cli
package plugin
import (
"fmt"
"os"
"text/tabwriter"
"github.com/urfave/cli"
"github.com/fission/fission/pkg/fission-cli/plugin"
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/plugin"
)
var cmdPlugin = cli.Command{
Name: "plugin",
Aliases: []string{"plugins"},
Usage: "Manage Fission CLI plugins",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List installed client plugins",
Action: pluginList,
},
},
type ListSubCommand struct {
client *client.Client
}
func pluginList(_ *cli.Context) error {
func List(flags cli.Input) error {
opts := &ListSubCommand{
client: cmd.GetServer(flags),
}
return opts.do(flags)
}
func (opts *ListSubCommand) do(flags cli.Input) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintln(w, "NAME\tVERSION\tPATH")
for _, p := range plugin.FindAll() {
+50
View File
@@ -0,0 +1,50 @@
/*
Copyright 2019 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package version
import (
"fmt"
"github.com/ghodss/yaml"
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/log"
"github.com/fission/fission/pkg/fission-cli/util"
)
type VersionSubCommand struct {
client *client.Client
}
func Version(flags cli.Input) error {
opts := &VersionSubCommand{
client: cmd.GetServer(flags),
}
return opts.do(flags)
}
func (opts *VersionSubCommand) do(flags cli.Input) error {
ver := util.GetVersion(opts.client)
bs, err := yaml.Marshal(ver)
if err != nil {
log.Fatal("Error formatting versions: " + err.Error())
}
fmt.Print(string(bs))
return nil
}
+32
View File
@@ -31,6 +31,8 @@ import (
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/log"
"github.com/fission/fission/pkg/info"
"github.com/fission/fission/pkg/plugin"
)
func GetApiClient(serverUrl string) *client.Client {
@@ -180,3 +182,33 @@ func CheckFunctionExistence(fissionClient *client.Client, functions []string, fn
return nil
}
func GetVersion(client *client.Client) info.Versions {
// Fetch client versions
versions := info.Versions{
Client: map[string]info.BuildMeta{
"fission/core": info.BuildInfo(),
},
}
for _, pmd := range plugin.FindAll() {
versions.Client[pmd.Name] = info.BuildMeta{
Version: pmd.Version,
}
}
serverInfo, err := client.ServerInfo()
if err != nil {
log.Warn(fmt.Sprintf("Error getting Fission API version: %v", err))
serverInfo = &info.ServerInfo{}
}
// Fetch server versions
versions.Server = map[string]info.BuildMeta{
"fission/core": serverInfo.Build,
}
// FUTURE: fetch versions of plugins server-side
return versions
}
-46
View File
@@ -1,46 +0,0 @@
package util
import (
"fmt"
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/log"
"github.com/fission/fission/pkg/fission-cli/plugin"
"github.com/fission/fission/pkg/info"
)
// Versions is a container of versions of the client (and its plugins) and server (and its plugins).
type Versions struct {
Client map[string]info.BuildMeta `json:"client"`
Server map[string]info.BuildMeta `json:"server"`
}
func GetVersion(client *client.Client) Versions {
// Fetch client versions
versions := Versions{
Client: map[string]info.BuildMeta{
"fission/core": info.BuildInfo(),
},
}
for _, pmd := range plugin.FindAll() {
versions.Client[pmd.Name] = info.BuildMeta{
Version: pmd.Version,
}
}
serverInfo, err := client.ServerInfo()
if err != nil {
log.Warn(fmt.Sprintf("Error getting Fission API version: %v", err))
serverInfo = &info.ServerInfo{}
}
// Fetch server versions
versions.Server = map[string]info.BuildMeta{
"fission/core": serverInfo.Build,
}
// FUTURE: fetch versions of plugins server-side
return versions
}
+6
View File
@@ -43,6 +43,12 @@ type (
Build BuildMeta `json:"Build,omitempty"`
ServerTime Time `json:"ServerTime,omitempty"`
}
// Versions is a container of versions of the client (and its plugins) and server (and its plugins).
Versions struct {
Client map[string]BuildMeta `json:"client"`
Server map[string]BuildMeta `json:"server"`
}
)
func BuildInfo() BuildMeta {
+1 -1
View File
@@ -323,7 +323,7 @@ check_gitcommit_version() {
while true
do
# ensure we run tests against with the same git commit version of CLI & server
ip=$(fission --version|grep "GitCommit"|tr -d ' '|uniq -c|grep "2 GitCommit")
ip=$(fission version|grep "GitCommit"|tr -d ' '|uniq -c|grep "2 GitCommit")
if [ $? -eq 0 ]; then
break
fi