Refactor environment CLI command (#1265)
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
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 cli
|
||||
|
||||
type (
|
||||
Input interface {
|
||||
//Parse(input interface{}) error
|
||||
|
||||
// IsSet checks whether a flag has been set by the user
|
||||
IsSet(key string) bool
|
||||
|
||||
// Bool returns true if given flag has been set;
|
||||
// otherwise, return false.
|
||||
Bool(key string) bool
|
||||
|
||||
// String returns string value of given flag.
|
||||
String(key string) string
|
||||
|
||||
// StringSlice returns string slice of given flag..
|
||||
StringSlice(key string) []string
|
||||
|
||||
// Int returns int value of given flag.nd false.
|
||||
Int(key string) int
|
||||
|
||||
// IntSlice returns int slice of given flag.lse.
|
||||
IntSlice(key string) []int
|
||||
|
||||
// Int64 returns int64 value of given flag. false.
|
||||
Int64(key string) int64
|
||||
|
||||
// Int64Slice returns int64 slice of given flag.e.
|
||||
Int64Slice(key string) []int64
|
||||
|
||||
// GlobalBool returns true if given global flag has been set;
|
||||
// otherwise, return false.
|
||||
GlobalBool(key string) bool
|
||||
|
||||
// GlobalString returns global string value of given flag.
|
||||
GlobalString(key string) string
|
||||
|
||||
// GlobalStringSlice returns global string slice of given flag.
|
||||
GlobalStringSlice(key string) []string
|
||||
|
||||
// GlobalInt returns global int value of given flag.
|
||||
GlobalInt(key string) int
|
||||
|
||||
// GlobalIntSlice returns global int slice of given flag.
|
||||
GlobalIntSlice(key string) []int
|
||||
|
||||
// GlobalInt64 returns global int64 value of given flag.
|
||||
GlobalInt64(key string) int64
|
||||
|
||||
// GlobalInt64Slice returns global int64 slice of given flag.
|
||||
GlobalInt64Slice(key string) []int64
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
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 cliwrapper is a wrapper that allowing functions to access flag value in an
|
||||
// identical way no matter what underlying CLI package used. It brings couple benefits
|
||||
// for doing this:
|
||||
// 1. Separate CLI function and CLI package.
|
||||
// 2. Easier to write test no matter what CLI package actually used.
|
||||
// 3. Migrate to new CLI package without changing the way for CLI function to access flag value.
|
||||
package cliwrapper
|
||||
@@ -0,0 +1 @@
|
||||
package driver
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
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 urfavecli
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
|
||||
fCli "github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
)
|
||||
|
||||
var _ fCli.Input = &Cli{}
|
||||
|
||||
type Cli struct {
|
||||
c *cli.Context
|
||||
}
|
||||
|
||||
// Parse is only for converting urfave *cli.Context to Input and will be removed in future.
|
||||
func Parse(c *cli.Context) fCli.Input {
|
||||
return Cli{c: c}
|
||||
}
|
||||
|
||||
func Wrapper(action cmd.CommandAction) func(*cli.Context) error {
|
||||
return func(c *cli.Context) error {
|
||||
e := action(Cli{c: c})
|
||||
// Urfave cli doesn't exit with error code even error is not nil.
|
||||
// We have to check whether error is empty and print error log here.
|
||||
if e != nil {
|
||||
log.Fatalf("%v", e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
func (u Cli) IsSet(key string) bool {
|
||||
return u.c.IsSet(key)
|
||||
}
|
||||
|
||||
func (u Cli) Bool(key string) bool {
|
||||
return u.c.Bool(key)
|
||||
}
|
||||
|
||||
func (u Cli) String(key string) string {
|
||||
return u.c.String(key)
|
||||
}
|
||||
|
||||
func (u Cli) StringSlice(key string) []string {
|
||||
return u.c.StringSlice(key)
|
||||
}
|
||||
|
||||
func (u Cli) Int(key string) int {
|
||||
return u.c.Int(key)
|
||||
}
|
||||
|
||||
func (u Cli) IntSlice(key string) []int {
|
||||
return u.c.IntSlice(key)
|
||||
}
|
||||
|
||||
func (u Cli) Int64(key string) int64 {
|
||||
return u.c.Int64(key)
|
||||
}
|
||||
|
||||
func (u Cli) Int64Slice(key string) []int64 {
|
||||
return u.c.Int64Slice(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalBool(key string) bool {
|
||||
return u.c.GlobalBool(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalString(key string) string {
|
||||
return u.c.GlobalString(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalStringSlice(key string) []string {
|
||||
return u.c.GlobalStringSlice(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalInt(key string) int {
|
||||
return u.c.GlobalInt(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalIntSlice(key string) []int {
|
||||
return u.c.GlobalIntSlice(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalInt64(key string) int64 {
|
||||
return u.c.GlobalInt64(key)
|
||||
}
|
||||
|
||||
func (u Cli) GlobalInt64Slice(key string) []int64 {
|
||||
return u.c.GlobalInt64Slice(key)
|
||||
}
|
||||
Reference in New Issue
Block a user