diff options
| author | zhangjie <iamkadisi@163.com> | 2019-08-28 14:04:56 +0800 |
|---|---|---|
| committer | zhangjie <iamkadisi@163.com> | 2019-09-02 16:42:20 +0800 |
| commit | ba0404b89fa3fb2b043d3cb26cdbf4d524344dfd (patch) | |
| tree | 4f6ecbeb05a8075a3dbad762f9a81758a97b7da8 /edgesite | |
| parent | add gitignore file (diff) | |
| download | kubeedge-ba0404b89fa3fb2b043d3cb26cdbf4d524344dfd.tar.gz | |
make flag --version install of command version
Signed-off-by: zhangjie <iamkadisi@163.com>
Diffstat (limited to 'edgesite')
| -rw-r--r-- | edgesite/cmd/app/options/options.go | 35 | ||||
| -rw-r--r-- | edgesite/cmd/app/server.go | 37 | ||||
| -rw-r--r-- | edgesite/cmd/app/version.go | 93 | ||||
| -rw-r--r-- | edgesite/cmd/edgesite.go | 10 |
4 files changed, 75 insertions, 100 deletions
diff --git a/edgesite/cmd/app/options/options.go b/edgesite/cmd/app/options/options.go new file mode 100644 index 000000000..59d16fb43 --- /dev/null +++ b/edgesite/cmd/app/options/options.go @@ -0,0 +1,35 @@ +/* +Copyright 2019 The KubeEdge 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 options + +import ( + cliflag "k8s.io/component-base/cli/flag" +) + +// TODO set edgesite config +type EdgeSiteOptions struct { +} + +func NewEdgeSiteOptions() *EdgeSiteOptions { + return &EdgeSiteOptions{} +} + +func (o *EdgeSiteOptions) Flags() (fss cliflag.NamedFlagSets) { + // TODO set EdgeSiteOptions field + //fs := fss.FlagSet("general") + return +} diff --git a/edgesite/cmd/app/server.go b/edgesite/cmd/app/server.go index 011ff329f..cba70d91e 100644 --- a/edgesite/cmd/app/server.go +++ b/edgesite/cmd/app/server.go @@ -1,16 +1,27 @@ package app import ( + "fmt" + "github.com/spf13/cobra" + "k8s.io/apiserver/pkg/util/term" + cliflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/cli/globalflag" + "k8s.io/klog" "github.com/kubeedge/beehive/pkg/core" "github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller" "github.com/kubeedge/kubeedge/edge/pkg/common/dbm" "github.com/kubeedge/kubeedge/edge/pkg/edged" "github.com/kubeedge/kubeedge/edge/pkg/metamanager" + "github.com/kubeedge/kubeedge/edgesite/cmd/app/options" + "github.com/kubeedge/kubeedge/pkg/util/flag" + "github.com/kubeedge/kubeedge/pkg/version" + "github.com/kubeedge/kubeedge/pkg/version/verflag" ) func NewEdgeSiteCommand() *cobra.Command { + opts := options.NewEdgeSiteOptions() cmd := &cobra.Command{ Use: "edgesite", Long: `EdgeSite helps running lightweight clusters at edge, which contains three modules: edgecontroller, @@ -19,12 +30,36 @@ so that the data can be targeted to a specific edge node. MetaManager is the mes It is also responsible for storing/retrieving metadata to/from a lightweight database (SQLite).Edged is an agent that runs on edge nodes and manages containerized applications.`, Run: func(cmd *cobra.Command, args []string) { + verflag.PrintAndExitIfRequested() + flag.PrintFlags(cmd.Flags()) + + // To help debugging, immediately log version + klog.Infof("Version: %+v", version.Get()) + registerModules() // start all modules core.Run() }, } - cmd.AddCommand(NewCmdVersion()) + fs := cmd.Flags() + namedFs := opts.Flags() + verflag.AddFlags(namedFs.FlagSet("global")) + globalflag.AddGlobalFlags(namedFs.FlagSet("global"), cmd.Name()) + for _, f := range namedFs.FlagSets { + fs.AddFlagSet(f) + } + + usageFmt := "Usage:\n %s\n" + cols, _, _ := term.TerminalSize(cmd.OutOrStdout()) + cmd.SetUsageFunc(func(cmd *cobra.Command) error { + fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine()) + cliflag.PrintSections(cmd.OutOrStderr(), namedFs, cols) + return nil + }) + cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { + fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine()) + cliflag.PrintSections(cmd.OutOrStdout(), namedFs, cols) + }) return cmd } diff --git a/edgesite/cmd/app/version.go b/edgesite/cmd/app/version.go deleted file mode 100644 index 0a3c602e9..000000000 --- a/edgesite/cmd/app/version.go +++ /dev/null @@ -1,93 +0,0 @@ -package app - -import ( - "encoding/json" - "fmt" - "io" - "os" - "strings" - - "github.com/pkg/errors" - "github.com/spf13/cobra" - yaml "gopkg.in/yaml.v2" - "k8s.io/klog" - - "github.com/kubeedge/kubeedge/pkg/version" -) - -const ( - // DefaultErrorExitCode defines exit the code for failed action generally - DefaultErrorExitCode = 1 -) - -func NewCmdVersion() *cobra.Command { - cmd := &cobra.Command{ - Use: "version", - Short: "Print the version of kubeadm", - Run: func(cmd *cobra.Command, args []string) { - err := RunVersion(os.Stdout, cmd) - CheckErr(err, fatal) - }, - } - cmd.Flags().StringP("output", "o", "", "Output format; available options are 'yaml', 'json' and 'short'") - return cmd -} - -// RunVersion provides the version information of kubeadm in format depending on arguments -// specified in cobra.Command. -func RunVersion(out io.Writer, cmd *cobra.Command) error { - v := version.Get() - - const flag = "output" - of, err := cmd.Flags().GetString(flag) - if err != nil { - klog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err) - } - - switch of { - case "": - fmt.Fprintf(out, "version: %#v\n", v) - case "short": - fmt.Fprintf(out, "%s\n", v) - case "yaml": - y, err := yaml.Marshal(&v) - if err != nil { - return err - } - fmt.Fprintln(out, string(y)) - case "json": - y, err := json.MarshalIndent(&v, "", " ") - if err != nil { - return err - } - fmt.Fprintln(out, string(y)) - default: - return errors.Errorf("invalid output format: %s", of) - } - - return nil -} - -// fatal prints the message if set and then exits. -func fatal(msg string, code int) { - if len(msg) > 0 { - // add newline if needed - if !strings.HasSuffix(msg, "\n") { - msg += "\n" - } - - fmt.Fprint(os.Stderr, msg) - } - os.Exit(code) -} - -// CheckErr formats a given error as a string and calls the passed handleErr -// func with that string and an exit code. -func CheckErr(err error, handleErr func(string, int)) { - switch err.(type) { - case nil: - return - default: - handleErr(err.Error(), DefaultErrorExitCode) - } -} diff --git a/edgesite/cmd/edgesite.go b/edgesite/cmd/edgesite.go index faf841082..d5425796a 100644 --- a/edgesite/cmd/edgesite.go +++ b/edgesite/cmd/edgesite.go @@ -1,20 +1,18 @@ package main import ( - "flag" "os" - "github.com/spf13/pflag" - "k8s.io/klog" + "k8s.io/component-base/logs" "github.com/kubeedge/kubeedge/edgesite/cmd/app" ) func main() { - klog.InitFlags(nil) - pflag.CommandLine.AddGoFlagSet(flag.CommandLine) - command := app.NewEdgeSiteCommand() + logs.InitLogs() + defer logs.FlushLogs() + if err := command.Execute(); err != nil { os.Exit(1) } |
