From 80943e0502c4c1c1c935e6dfb76e43ea4f9f45af Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Wed, 9 Jan 2019 17:21:08 -0800 Subject: [PATCH 1/7] Get revision and service listing skeletons in. This adds the kn program, with basic commands for service and revision listing in. kn service list kn revision list They use the genericclioptions library from kubernetes to support jsonpath, yaml, and json output. The default is to just list the names of the objects, for now, until we add in some of the kubectl-based libraries for creating tables in another commit. This is deliberately bare-bones; I am looking to get the basic skeletons of how this repository is laid out down. No tests yet. Soon. --- cmd/kn/commands/revision.go | 30 ++++++ cmd/kn/commands/revision_list.go | 77 +++++++++++++++ cmd/kn/commands/root.go | 91 ++++++++++++++++++ cmd/kn/commands/service.go | 30 ++++++ cmd/kn/commands/service_list.go | 77 +++++++++++++++ cmd/kn/main.go | 21 ++++ go.mod | 51 ++++++++++ go.sum | 158 +++++++++++++++++++++++++++++++ 8 files changed, 535 insertions(+) create mode 100644 cmd/kn/commands/revision.go create mode 100644 cmd/kn/commands/revision_list.go create mode 100644 cmd/kn/commands/root.go create mode 100644 cmd/kn/commands/service.go create mode 100644 cmd/kn/commands/service_list.go create mode 100644 cmd/kn/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/cmd/kn/commands/revision.go b/cmd/kn/commands/revision.go new file mode 100644 index 0000000000..b7343d1b57 --- /dev/null +++ b/cmd/kn/commands/revision.go @@ -0,0 +1,30 @@ +// Copyright © 2018 The Knative 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 commands + +import ( + "github.com/spf13/cobra" +) + +var revisionCmd = &cobra.Command{ + Use: "revision", + Short: "Revision command group.", +} + +func init() { + rootCmd.AddCommand(revisionCmd) + + revisionCmd.PersistentFlags().StringP("namespace", "n", "default", "Namespace to use.") +} diff --git a/cmd/kn/commands/revision_list.go b/cmd/kn/commands/revision_list.go new file mode 100644 index 0000000000..72135a57f4 --- /dev/null +++ b/cmd/kn/commands/revision_list.go @@ -0,0 +1,77 @@ +// Copyright © 2018 The Knative 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 commands + +import ( + "fmt" + "os" + + serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" + "github.com/spf13/cobra" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/cli-runtime/pkg/genericclioptions" + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + "k8s.io/client-go/tools/clientcmd" +) + +var revisionListPrintFlags *genericclioptions.PrintFlags + +// listCmd represents the list command +var revisionListCmd = &cobra.Command{ + Use: "list", + Short: "List available revisions.", + Run: func(cmd *cobra.Command, args []string) { + // use the current context in kubeconfig + config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) + if err != nil { + panic(err.Error()) + } + client, err := serving.NewForConfig(config) + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + namespace := cmd.Flag("namespace").Value.String() + revision, err := client.Revisions(namespace).List(v1.ListOptions{}) + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + + printer, err := revisionListPrintFlags.ToPrinter() + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ + Group: "knative.dev", + Version: "v1alpha1", + Kind: "Revision"}) + err = printer.PrintObj(revision, os.Stdout) + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + }, +} + +func init() { + revisionCmd.AddCommand(revisionListCmd) + + revisionListPrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput( + "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}") + revisionListPrintFlags.AddFlags(revisionListCmd) +} diff --git a/cmd/kn/commands/root.go b/cmd/kn/commands/root.go new file mode 100644 index 0000000000..04e28ea2fe --- /dev/null +++ b/cmd/kn/commands/root.go @@ -0,0 +1,91 @@ +// Copyright © 2018 The Knative 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 commands + +import ( + "fmt" + "os" + "path/filepath" + + homedir "github.com/mitchellh/go-homedir" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var cfgFile string +var kubeCfgFile string + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "kn", + Short: "Knative client.", + Long: `Manage your Knative building blokcs: + +Serving: Manage your services and release new software to them. +Build: Create builds and keep track of their results. +Eventing: Manage event subscriptions and channels. Connect up event sources.`, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func init() { + cobra.OnInitialize(initConfig) + cobra.OnInitialize(initKubeConfig) + + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kn.yaml)") + rootCmd.PersistentFlags().StringVar(&kubeCfgFile, "kubeconfig", "", "kubectl config file (default is $HOME/.kube/config)") + +} +func initKubeConfig() { + if kubeCfgFile == "" { + home, err := homedir.Dir() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + kubeCfgFile = filepath.Join(home, ".kube", "config") + } +} + +// initConfig reads in config file and ENV variables if set. +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := homedir.Dir() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Search config in home directory with name ".kn" (without extension). + viper.AddConfigPath(home) + viper.SetConfigName(".kn") + } + + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + fmt.Println("Using config file:", viper.ConfigFileUsed()) + } +} diff --git a/cmd/kn/commands/service.go b/cmd/kn/commands/service.go new file mode 100644 index 0000000000..7190553b60 --- /dev/null +++ b/cmd/kn/commands/service.go @@ -0,0 +1,30 @@ +// Copyright © 2018 The Knative 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 commands + +import ( + "github.com/spf13/cobra" +) + +var serviceCmd = &cobra.Command{ + Use: "service", + Short: "Service command group.", +} + +func init() { + rootCmd.AddCommand(serviceCmd) + + serviceCmd.PersistentFlags().StringP("namespace", "n", "default", "Namespace to use.") +} diff --git a/cmd/kn/commands/service_list.go b/cmd/kn/commands/service_list.go new file mode 100644 index 0000000000..e2dc369c95 --- /dev/null +++ b/cmd/kn/commands/service_list.go @@ -0,0 +1,77 @@ +// Copyright © 2018 The Knative 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 commands + +import ( + "fmt" + "os" + + serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" + "github.com/spf13/cobra" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/cli-runtime/pkg/genericclioptions" + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + "k8s.io/client-go/tools/clientcmd" +) + +var serviceListPrintFlags *genericclioptions.PrintFlags + +// listCmd represents the list command +var serviceListCmd = &cobra.Command{ + Use: "list", + Short: "List available services.", + Run: func(cmd *cobra.Command, args []string) { + // use the current context in kubeconfig + config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) + if err != nil { + panic(err.Error()) + } + client, err := serving.NewForConfig(config) + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + namespace := cmd.Flag("namespace").Value.String() + service, err := client.Services(namespace).List(v1.ListOptions{}) + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + + printer, err := serviceListPrintFlags.ToPrinter() + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ + Group: "knative.dev", + Version: "v1alpha1", + Kind: "Service"}) + err = printer.PrintObj(service, os.Stdout) + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } + }, +} + +func init() { + serviceCmd.AddCommand(serviceListCmd) + + serviceListPrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput( + "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}") + serviceListPrintFlags.AddFlags(serviceListCmd) +} diff --git a/cmd/kn/main.go b/cmd/kn/main.go new file mode 100644 index 0000000000..b0a60cef86 --- /dev/null +++ b/cmd/kn/main.go @@ -0,0 +1,21 @@ +// Copyright © 2018 The Knative 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 main + +import "github.com/knative/client/cmd/kn/commands" + +func main() { + commands.Execute() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000..5b8ed22bc7 --- /dev/null +++ b/go.mod @@ -0,0 +1,51 @@ +module github.com/knative/client + +require ( + cloud.google.com/go v0.34.0 // indirect + github.com/emicklei/go-restful v2.8.0+incompatible // indirect + github.com/evanphx/json-patch v4.1.0+incompatible // indirect + github.com/ghodss/yaml v1.0.0 // indirect + github.com/go-openapi/spec v0.18.0 // indirect + github.com/gogo/protobuf v1.2.0 // indirect + github.com/golang/protobuf v1.2.0 // indirect + github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect + github.com/google/go-cmp v0.2.0 // indirect + github.com/google/go-containerregistry v0.0.0-20190109170535-caf7c6e36671 // indirect + github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect + github.com/google/subcommands v0.0.0-20181012225330-46f0354f6315 + github.com/googleapis/gnostic v0.2.0 // indirect + github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f // indirect + github.com/hashicorp/golang-lru v0.5.0 // indirect + github.com/imdario/mergo v0.3.6 // indirect + github.com/json-iterator/go v1.1.5 // indirect + github.com/knative/build v0.3.0 // indirect + github.com/knative/pkg v0.0.0-20190110005142-b6044a7d1795 // indirect + github.com/knative/serving v0.3.0 + github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect + github.com/mitchellh/go-homedir v1.0.0 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/peterbourgon/diskv v2.0.1+incompatible // indirect + github.com/pkg/errors v0.8.1 // indirect + github.com/spf13/afero v1.2.0 // indirect + github.com/spf13/cobra v0.0.3 + github.com/spf13/viper v1.3.1 + github.com/stretchr/objx v0.1.1 // indirect + github.com/stretchr/testify v1.3.0 // indirect + github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 // indirect + golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect + golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect + golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 // indirect + golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect + golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + k8s.io/api v0.0.0-20181221193117-173ce66c1e39 // indirect + k8s.io/apimachinery v0.0.0-20190104073114-849b284f3b75 + k8s.io/cli-runtime v0.0.0-20190107235426-31214e12222d + k8s.io/client-go v10.0.0+incompatible + k8s.io/klog v0.1.0 // indirect + k8s.io/kube-openapi v0.0.0-20181114233023-0317810137be // indirect + sigs.k8s.io/kustomize v1.0.11 // indirect + sigs.k8s.io/yaml v1.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000..7bf8e2f6bf --- /dev/null +++ b/go.sum @@ -0,0 +1,158 @@ +cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/PuerkitoBio/purell v1.1.0 h1:rmGxhojJlM0tuKtfdvliR84CFHljx9ag64t2xmVkjK4= +github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emicklei/go-restful v2.8.0+incompatible h1:wN8GCRDPGHguIynsnBartv5GUgGUg1LAU7+xnSn1j7Q= +github.com/emicklei/go-restful v2.8.0+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= +github.com/evanphx/json-patch v4.1.0+incompatible h1:K1MDoo4AZ4wU0GIU/fPmtZg7VpzLjCxu+UwBD1FvwOc= +github.com/evanphx/json-patch v4.1.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-openapi/jsonpointer v0.17.0 h1:nH6xp8XdXHx8dqveo0ZuJBluCO2qGrPbDNZ0dwoRHP0= +github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M= +github.com/go-openapi/jsonreference v0.17.0 h1:yJW3HCkTHg7NOA+gZ83IPHzUSnUzGXhGmsdiCcMexbA= +github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I= +github.com/go-openapi/spec v0.18.0 h1:aIjeyG5mo5/FrvDkpKKEGZPmF9MPHahS72mzfVqeQXQ= +github.com/go-openapi/spec v0.18.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI= +github.com/go-openapi/swag v0.17.0 h1:iqrgMg7Q7SvtbWLlltPrkMs0UBJI6oTSs79JFRUi880= +github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg= +github.com/gogo/protobuf v1.2.0 h1:xU6/SpYbvkNYiptHJYEDRseDLvYE7wSqhYYNy0QSUzI= +github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-containerregistry v0.0.0-20190109170535-caf7c6e36671 h1:Tgwlf2vknrRIpvWtLw1m/DdLdiJRZvNWaYf4y8Kt7Cs= +github.com/google/go-containerregistry v0.0.0-20190109170535-caf7c6e36671/go.mod h1:yZAFP63pRshzrEYLXLGPmUt0Ay+2zdjmMN1loCnRLUk= +github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck= +github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= +github.com/google/subcommands v0.0.0-20181012225330-46f0354f6315 h1:WW91Hq2v0qDzoPME+TPD4En72+d2Ue3ZMKPYfwR9yBU= +github.com/google/subcommands v0.0.0-20181012225330-46f0354f6315/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/googleapis/gnostic v0.2.0 h1:l6N3VoaVzTncYYW+9yOz2LJJammFZGBO13sqgEhpy9g= +github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= +github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f h1:ShTPMJQes6tubcjzGMODIVG5hlrCeImaBnZzKF2N8SM= +github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= +github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28= +github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= +github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/knative/build v0.2.0 h1:+bmR2edXNnc8l4zTZ1QEsy8R37ariWyaNGuahHSj+Tg= +github.com/knative/build v0.2.0/go.mod h1:/sU74ZQkwlYA5FwYDJhYTy61i/Kn+5eWfln2jDbw3Qo= +github.com/knative/build v0.3.0 h1:X1luHXvWAyw6iU/XOgcOMKpDd01tRoHkmqEMNi4CPE0= +github.com/knative/build v0.3.0/go.mod h1:/sU74ZQkwlYA5FwYDJhYTy61i/Kn+5eWfln2jDbw3Qo= +github.com/knative/pkg v0.0.0-20181210152529-10aa1c060a81 h1:dYt45WaEKSzbRPtvfX4ycwZOD6Wkgu3GHykCylNg4gI= +github.com/knative/pkg v0.0.0-20181210152529-10aa1c060a81/go.mod h1:7Ijfhw7rfB+H9VtosIsDYvZQ+qYTz7auK3fHW/5z4ww= +github.com/knative/pkg v0.0.0-20190110005142-b6044a7d1795 h1:mTaIClRJEjRuTFOc4Gsicdqe/OwEla0Ano9cllUNoU0= +github.com/knative/pkg v0.0.0-20190110005142-b6044a7d1795/go.mod h1:7Ijfhw7rfB+H9VtosIsDYvZQ+qYTz7auK3fHW/5z4ww= +github.com/knative/serving v0.2.2 h1:JNNfwcKjfxIkplJJlsxoaQUjGagAemeqBtVbtvHrb0U= +github.com/knative/serving v0.2.2/go.mod h1:ljvMfwQy2qanaM/8xnBSK4Mz3Vv2NawC2fo5kFRJS1A= +github.com/knative/serving v0.3.0 h1:O8llkx4VL7C7AZm5wCxcQ9abz8yq02I62ZU++EAwBHk= +github.com/knative/serving v0.3.0/go.mod h1:ljvMfwQy2qanaM/8xnBSK4Mz3Vv2NawC2fo5kFRJS1A= +github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a h1:+J2gw7Bw77w/fbK7wnNJJDKmw1IbWft2Ul5BzrG1Qm8= +github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0= +github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= +github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/afero v1.2.0 h1:O9FblXGxoTc51M+cqr74Bm2Tmt4PvkA5iu/j8HrkNuY= +github.com/spf13/afero v1.2.0/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= +github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38= +github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M= +golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181207154023-610586996380 h1:zPQexyRtNYBc7bcHmehl1dH6TB3qn8zytv8cBGLDNY0= +golang.org/x/net v0.0.0-20181207154023-610586996380/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 h1:uESlIz09WIHT2I+pasSXcpLYqYK8wHcdCetU3VuMBJE= +golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181211161752-7da8ea5c8182 h1:3jwI9dC+BuoXWS+QtR/HhfGTGTuB6ZzL6II6S1IuVvo= +golang.org/x/sys v0.0.0-20181211161752-7da8ea5c8182/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb h1:1w588/yEchbPNpa9sEvOcMZYbWHedwJjg4VOAdDHWHk= +golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +k8s.io/api v0.0.0-20181130031204-d04500c8c3dd h1:5aHsneN62ehs/tdtS9tWZlhVk68V7yms/Qw7nsGmvCA= +k8s.io/api v0.0.0-20181130031204-d04500c8c3dd/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= +k8s.io/api v0.0.0-20181221193117-173ce66c1e39 h1:iGq7zEPXFb0IeXAQK5RiYT1SVKX/af9F9Wv0M+yudPY= +k8s.io/api v0.0.0-20181221193117-173ce66c1e39/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= +k8s.io/apimachinery v0.0.0-20181211025822-57dc7e687b54 h1:2O23KCqjI/ISedkxmAkVTb/+Of8oKfy67lmRo3rLFbQ= +k8s.io/apimachinery v0.0.0-20181211025822-57dc7e687b54/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0= +k8s.io/apimachinery v0.0.0-20190104073114-849b284f3b75 h1:dLhsGWh58R0WYgTCX6ZdaqSz2FltMZsk+ByHsUgMWRU= +k8s.io/apimachinery v0.0.0-20190104073114-849b284f3b75/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0= +k8s.io/cli-runtime v0.0.0-20181121073402-2f0d1d0a58f2 h1:0tWjdH70/BhNHxQ1cc0DEO6iogWpNoY4dYRzUtwg+/g= +k8s.io/cli-runtime v0.0.0-20181121073402-2f0d1d0a58f2/go.mod h1:qWnH3/b8sp/l7EvlDh7ulDU3UWA4P4N1NFbEEP791tM= +k8s.io/cli-runtime v0.0.0-20190107235426-31214e12222d h1:hdarETxu5sE+zfQ8CPfvWbQzMe1yaJA1XvURyx1u514= +k8s.io/cli-runtime v0.0.0-20190107235426-31214e12222d/go.mod h1:qWnH3/b8sp/l7EvlDh7ulDU3UWA4P4N1NFbEEP791tM= +k8s.io/client-go v10.0.0+incompatible h1:F1IqCqw7oMBzDkqlcBymRq1450wD0eNqLE9jzUrIi34= +k8s.io/client-go v10.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s= +k8s.io/klog v0.1.0 h1:I5HMfc/DtuVaGR1KPwUrTc476K8NCqNBldC7H4dYEzk= +k8s.io/klog v0.1.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= +k8s.io/kube-openapi v0.0.0-20181114233023-0317810137be h1:aWEq4nbj7HRJ0mtKYjNSk/7X28Tl6TI6FeG8gKF+r7Q= +k8s.io/kube-openapi v0.0.0-20181114233023-0317810137be/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc= +sigs.k8s.io/kustomize v1.0.11 h1:Yb+6DDt9+aR2AvQApvUaKS/ugteeG4MPyoFeUHiPOjk= +sigs.k8s.io/kustomize v1.0.11/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU= +sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= +sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= From 8e78290e6e8e07c60f72b6dbae42624312b83465 Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Mon, 14 Jan 2019 17:11:02 -0800 Subject: [PATCH 2/7] Move commands to pkg, so cmd only contains minimalist main.go --- cmd/kn/main.go | 2 +- {cmd => pkg}/kn/commands/revision.go | 0 {cmd => pkg}/kn/commands/revision_list.go | 0 {cmd => pkg}/kn/commands/root.go | 0 {cmd => pkg}/kn/commands/service.go | 0 {cmd => pkg}/kn/commands/service_list.go | 0 6 files changed, 1 insertion(+), 1 deletion(-) rename {cmd => pkg}/kn/commands/revision.go (100%) rename {cmd => pkg}/kn/commands/revision_list.go (100%) rename {cmd => pkg}/kn/commands/root.go (100%) rename {cmd => pkg}/kn/commands/service.go (100%) rename {cmd => pkg}/kn/commands/service_list.go (100%) diff --git a/cmd/kn/main.go b/cmd/kn/main.go index b0a60cef86..285a5dab10 100644 --- a/cmd/kn/main.go +++ b/cmd/kn/main.go @@ -14,7 +14,7 @@ package main -import "github.com/knative/client/cmd/kn/commands" +import "github.com/knative/client/pkg/kn/commands" func main() { commands.Execute() diff --git a/cmd/kn/commands/revision.go b/pkg/kn/commands/revision.go similarity index 100% rename from cmd/kn/commands/revision.go rename to pkg/kn/commands/revision.go diff --git a/cmd/kn/commands/revision_list.go b/pkg/kn/commands/revision_list.go similarity index 100% rename from cmd/kn/commands/revision_list.go rename to pkg/kn/commands/revision_list.go diff --git a/cmd/kn/commands/root.go b/pkg/kn/commands/root.go similarity index 100% rename from cmd/kn/commands/root.go rename to pkg/kn/commands/root.go diff --git a/cmd/kn/commands/service.go b/pkg/kn/commands/service.go similarity index 100% rename from cmd/kn/commands/service.go rename to pkg/kn/commands/service.go diff --git a/cmd/kn/commands/service_list.go b/pkg/kn/commands/service_list.go similarity index 100% rename from cmd/kn/commands/service_list.go rename to pkg/kn/commands/service_list.go From e658488771f3a50de05c09f3642761323ed52dec Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Mon, 14 Jan 2019 17:34:31 -0800 Subject: [PATCH 3/7] RunE instead of Run. The defaults on Cobra command generation are weird. --- pkg/kn/commands/revision_list.go | 15 +++++---------- pkg/kn/commands/root.go | 2 +- pkg/kn/commands/service_list.go | 15 +++++---------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/pkg/kn/commands/revision_list.go b/pkg/kn/commands/revision_list.go index 72135a57f4..e81106a430 100644 --- a/pkg/kn/commands/revision_list.go +++ b/pkg/kn/commands/revision_list.go @@ -15,7 +15,6 @@ package commands import ( - "fmt" "os" serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" @@ -33,7 +32,7 @@ var revisionListPrintFlags *genericclioptions.PrintFlags var revisionListCmd = &cobra.Command{ Use: "list", Short: "List available revisions.", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) { // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) if err != nil { @@ -41,20 +40,17 @@ var revisionListCmd = &cobra.Command{ } client, err := serving.NewForConfig(config) if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } namespace := cmd.Flag("namespace").Value.String() revision, err := client.Revisions(namespace).List(v1.ListOptions{}) if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } printer, err := revisionListPrintFlags.ToPrinter() if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ Group: "knative.dev", @@ -62,8 +58,7 @@ var revisionListCmd = &cobra.Command{ Kind: "Revision"}) err = printer.PrintObj(revision, os.Stdout) if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } }, } diff --git a/pkg/kn/commands/root.go b/pkg/kn/commands/root.go index 04e28ea2fe..abf6311fab 100644 --- a/pkg/kn/commands/root.go +++ b/pkg/kn/commands/root.go @@ -40,7 +40,7 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`, func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) + fmt.Println(os.Stderr, err) os.Exit(1) } } diff --git a/pkg/kn/commands/service_list.go b/pkg/kn/commands/service_list.go index e2dc369c95..fb19ac3a76 100644 --- a/pkg/kn/commands/service_list.go +++ b/pkg/kn/commands/service_list.go @@ -15,7 +15,6 @@ package commands import ( - "fmt" "os" serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" @@ -33,7 +32,7 @@ var serviceListPrintFlags *genericclioptions.PrintFlags var serviceListCmd = &cobra.Command{ Use: "list", Short: "List available services.", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) { // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) if err != nil { @@ -41,20 +40,17 @@ var serviceListCmd = &cobra.Command{ } client, err := serving.NewForConfig(config) if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } namespace := cmd.Flag("namespace").Value.String() service, err := client.Services(namespace).List(v1.ListOptions{}) if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } printer, err := serviceListPrintFlags.ToPrinter() if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ Group: "knative.dev", @@ -62,8 +58,7 @@ var serviceListCmd = &cobra.Command{ Kind: "Service"}) err = printer.PrintObj(service, os.Stdout) if err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) + return err } }, } From c85ec13e58850d121fbad86ed5a83e3d863e9cd3 Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Mon, 14 Jan 2019 17:36:10 -0800 Subject: [PATCH 4/7] Oops forgot to change types of stuff --- pkg/kn/commands/revision_list.go | 3 ++- pkg/kn/commands/service_list.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/kn/commands/revision_list.go b/pkg/kn/commands/revision_list.go index e81106a430..ec3ce08876 100644 --- a/pkg/kn/commands/revision_list.go +++ b/pkg/kn/commands/revision_list.go @@ -32,7 +32,7 @@ var revisionListPrintFlags *genericclioptions.PrintFlags var revisionListCmd = &cobra.Command{ Use: "list", Short: "List available revisions.", - RunE: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) if err != nil { @@ -60,6 +60,7 @@ var revisionListCmd = &cobra.Command{ if err != nil { return err } + return nil }, } diff --git a/pkg/kn/commands/service_list.go b/pkg/kn/commands/service_list.go index fb19ac3a76..bbaa15c1dd 100644 --- a/pkg/kn/commands/service_list.go +++ b/pkg/kn/commands/service_list.go @@ -32,7 +32,7 @@ var serviceListPrintFlags *genericclioptions.PrintFlags var serviceListCmd = &cobra.Command{ Use: "list", Short: "List available services.", - RunE: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) if err != nil { @@ -60,6 +60,7 @@ var serviceListCmd = &cobra.Command{ if err != nil { return err } + return nil }, } From c200076dfb8e67eedf3078424ca12ba6b47566e2 Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Tue, 15 Jan 2019 15:00:16 -0800 Subject: [PATCH 5/7] Do not panic on bad config file --- pkg/kn/commands/revision_list.go | 2 +- pkg/kn/commands/service_list.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/kn/commands/revision_list.go b/pkg/kn/commands/revision_list.go index ec3ce08876..939e3a889f 100644 --- a/pkg/kn/commands/revision_list.go +++ b/pkg/kn/commands/revision_list.go @@ -36,7 +36,7 @@ var revisionListCmd = &cobra.Command{ // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) if err != nil { - panic(err.Error()) + return err } client, err := serving.NewForConfig(config) if err != nil { diff --git a/pkg/kn/commands/service_list.go b/pkg/kn/commands/service_list.go index bbaa15c1dd..fb53fe1fa8 100644 --- a/pkg/kn/commands/service_list.go +++ b/pkg/kn/commands/service_list.go @@ -36,7 +36,7 @@ var serviceListCmd = &cobra.Command{ // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) if err != nil { - panic(err.Error()) + return err } client, err := serving.NewForConfig(config) if err != nil { From d84a42179d20ce1498b9df9b9c55bb600fa4e714 Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Wed, 16 Jan 2019 17:34:43 -0800 Subject: [PATCH 6/7] Make commands no longer global --- pkg/kn/commands/revision.go | 15 +++---- pkg/kn/commands/revision_list.go | 74 +++++++++++++++--------------- pkg/kn/commands/root.go | 20 ++++++--- pkg/kn/commands/service.go | 15 +++---- pkg/kn/commands/service_list.go | 77 ++++++++++++++++---------------- 5 files changed, 101 insertions(+), 100 deletions(-) diff --git a/pkg/kn/commands/revision.go b/pkg/kn/commands/revision.go index b7343d1b57..0fa803e0b1 100644 --- a/pkg/kn/commands/revision.go +++ b/pkg/kn/commands/revision.go @@ -18,13 +18,12 @@ import ( "github.com/spf13/cobra" ) -var revisionCmd = &cobra.Command{ - Use: "revision", - Short: "Revision command group.", -} - -func init() { - rootCmd.AddCommand(revisionCmd) - +func NewRevisionCommand() *cobra.Command { + revisionCmd := &cobra.Command{ + Use: "revision", + Short: "Revision command group.", + } revisionCmd.PersistentFlags().StringP("namespace", "n", "default", "Namespace to use.") + revisionCmd.AddCommand(NewRevisionListCommand()) + return revisionCmd } diff --git a/pkg/kn/commands/revision_list.go b/pkg/kn/commands/revision_list.go index 939e3a889f..025f0a98d0 100644 --- a/pkg/kn/commands/revision_list.go +++ b/pkg/kn/commands/revision_list.go @@ -29,45 +29,43 @@ import ( var revisionListPrintFlags *genericclioptions.PrintFlags // listCmd represents the list command -var revisionListCmd = &cobra.Command{ - Use: "list", - Short: "List available revisions.", - RunE: func(cmd *cobra.Command, args []string) error { - // use the current context in kubeconfig - config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) - if err != nil { - return err - } - client, err := serving.NewForConfig(config) - if err != nil { - return err - } - namespace := cmd.Flag("namespace").Value.String() - revision, err := client.Revisions(namespace).List(v1.ListOptions{}) - if err != nil { - return err - } - - printer, err := revisionListPrintFlags.ToPrinter() - if err != nil { - return err - } - revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ - Group: "knative.dev", - Version: "v1alpha1", - Kind: "Revision"}) - err = printer.PrintObj(revision, os.Stdout) - if err != nil { - return err - } - return nil - }, -} - -func init() { - revisionCmd.AddCommand(revisionListCmd) - +func NewRevisionListCommand() *cobra.Command { revisionListPrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput( "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}") + revisionListCmd := &cobra.Command{ + Use: "list", + Short: "List available revisions.", + RunE: func(cmd *cobra.Command, args []string) error { + // use the current context in kubeconfig + config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) + if err != nil { + return err + } + client, err := serving.NewForConfig(config) + if err != nil { + return err + } + namespace := cmd.Flag("namespace").Value.String() + revision, err := client.Revisions(namespace).List(v1.ListOptions{}) + if err != nil { + return err + } + + printer, err := revisionListPrintFlags.ToPrinter() + if err != nil { + return err + } + revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ + Group: "knative.dev", + Version: "v1alpha1", + Kind: "Revision"}) + err = printer.PrintObj(revision, os.Stdout) + if err != nil { + return err + } + return nil + }, + } revisionListPrintFlags.AddFlags(revisionListCmd) + return revisionListCmd } diff --git a/pkg/kn/commands/root.go b/pkg/kn/commands/root.go index abf6311fab..6a628fbbc3 100644 --- a/pkg/kn/commands/root.go +++ b/pkg/kn/commands/root.go @@ -28,17 +28,25 @@ var cfgFile string var kubeCfgFile string // rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "kn", - Short: "Knative client.", - Long: `Manage your Knative building blokcs: +func NewKnCommand() *cobra.Command { + rootCmd := &cobra.Command{ + Use: "kn", + Short: "Knative client.", + Long: `Manage your Knative building blokcs: Serving: Manage your services and release new software to them. Build: Create builds and keep track of their results. Eventing: Manage event subscriptions and channels. Connect up event sources.`, + } + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kn.yaml)") + rootCmd.PersistentFlags().StringVar(&kubeCfgFile, "kubeconfig", "", "kubectl config file (default is $HOME/.kube/config)") + rootCmd.AddCommand(NewServiceCommand()) + rootCmd.AddCommand(NewRevisionCommand()) + return rootCmd } func Execute() { + rootCmd := NewKnCommand() if err := rootCmd.Execute(); err != nil { fmt.Println(os.Stderr, err) os.Exit(1) @@ -49,10 +57,8 @@ func init() { cobra.OnInitialize(initConfig) cobra.OnInitialize(initKubeConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kn.yaml)") - rootCmd.PersistentFlags().StringVar(&kubeCfgFile, "kubeconfig", "", "kubectl config file (default is $HOME/.kube/config)") - } + func initKubeConfig() { if kubeCfgFile == "" { home, err := homedir.Dir() diff --git a/pkg/kn/commands/service.go b/pkg/kn/commands/service.go index 7190553b60..2283d843a1 100644 --- a/pkg/kn/commands/service.go +++ b/pkg/kn/commands/service.go @@ -18,13 +18,12 @@ import ( "github.com/spf13/cobra" ) -var serviceCmd = &cobra.Command{ - Use: "service", - Short: "Service command group.", -} - -func init() { - rootCmd.AddCommand(serviceCmd) - +func NewServiceCommand() *cobra.Command { + serviceCmd := &cobra.Command{ + Use: "service", + Short: "Service command group.", + } serviceCmd.PersistentFlags().StringP("namespace", "n", "default", "Namespace to use.") + serviceCmd.AddCommand(NewServiceListCommand()) + return serviceCmd } diff --git a/pkg/kn/commands/service_list.go b/pkg/kn/commands/service_list.go index fb53fe1fa8..56973a622d 100644 --- a/pkg/kn/commands/service_list.go +++ b/pkg/kn/commands/service_list.go @@ -29,45 +29,44 @@ import ( var serviceListPrintFlags *genericclioptions.PrintFlags // listCmd represents the list command -var serviceListCmd = &cobra.Command{ - Use: "list", - Short: "List available services.", - RunE: func(cmd *cobra.Command, args []string) error { - // use the current context in kubeconfig - config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) - if err != nil { - return err - } - client, err := serving.NewForConfig(config) - if err != nil { - return err - } - namespace := cmd.Flag("namespace").Value.String() - service, err := client.Services(namespace).List(v1.ListOptions{}) - if err != nil { - return err - } +func NewServiceListCommand() *cobra.Command { - printer, err := serviceListPrintFlags.ToPrinter() - if err != nil { - return err - } - service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ - Group: "knative.dev", - Version: "v1alpha1", - Kind: "Service"}) - err = printer.PrintObj(service, os.Stdout) - if err != nil { - return err - } - return nil - }, -} - -func init() { - serviceCmd.AddCommand(serviceListCmd) - - serviceListPrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput( + serviceListPrintFlags := genericclioptions.NewPrintFlags("").WithDefaultOutput( "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}") - serviceListPrintFlags.AddFlags(serviceListCmd) + serviceListCommand := &cobra.Command{ + Use: "list", + Short: "List available services.", + RunE: func(cmd *cobra.Command, args []string) error { + // use the current context in kubeconfig + config, err := clientcmd.BuildConfigFromFlags("", kubeCfgFile) + if err != nil { + return err + } + client, err := serving.NewForConfig(config) + if err != nil { + return err + } + namespace := cmd.Flag("namespace").Value.String() + service, err := client.Services(namespace).List(v1.ListOptions{}) + if err != nil { + return err + } + + printer, err := serviceListPrintFlags.ToPrinter() + if err != nil { + return err + } + service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ + Group: "knative.dev", + Version: "v1alpha1", + Kind: "Service"}) + err = printer.PrintObj(service, os.Stdout) + if err != nil { + return err + } + return nil + }, + } + serviceListPrintFlags.AddFlags(serviceListCommand) + return serviceListCommand } From 0bb3a409087c87a4344c5915e7222a68dfd8aac0 Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Wed, 16 Jan 2019 17:39:28 -0800 Subject: [PATCH 7/7] Make configuration initialization not static in the module, but rather triggered by main.go init --- cmd/kn/main.go | 17 +++++++++++++++-- pkg/kn/commands/root.go | 10 +--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cmd/kn/main.go b/cmd/kn/main.go index 285a5dab10..4bb133e8fa 100644 --- a/cmd/kn/main.go +++ b/cmd/kn/main.go @@ -14,8 +14,21 @@ package main -import "github.com/knative/client/pkg/kn/commands" +import ( + "fmt" + "os" + + "github.com/knative/client/pkg/kn/commands" +) + +func init() { + commands.InitializeConfig() +} func main() { - commands.Execute() + err := commands.NewKnCommand().Execute() + if err != nil { + fmt.Println(os.Stderr, err) + os.Exit(1) + } } diff --git a/pkg/kn/commands/root.go b/pkg/kn/commands/root.go index 6a628fbbc3..14e42d57fa 100644 --- a/pkg/kn/commands/root.go +++ b/pkg/kn/commands/root.go @@ -45,15 +45,7 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`, return rootCmd } -func Execute() { - rootCmd := NewKnCommand() - if err := rootCmd.Execute(); err != nil { - fmt.Println(os.Stderr, err) - os.Exit(1) - } -} - -func init() { +func InitializeConfig() { cobra.OnInitialize(initConfig) cobra.OnInitialize(initKubeConfig)