github.com/spinnaker/spin@v1.30.0/cmd/application/get.go (about) 1 // Copyright (c) 2018, Google, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package application 16 17 import ( 18 "fmt" 19 "net/http" 20 21 "github.com/antihax/optional" 22 "github.com/spf13/cobra" 23 24 gate "github.com/spinnaker/spin/gateapi" 25 "github.com/spinnaker/spin/util" 26 ) 27 28 type getOptions struct { 29 *applicationOptions 30 expand bool 31 } 32 33 var ( 34 getApplicationShort = "Get the specified application" 35 getApplicationLong = "Get the specified application" 36 getApplicationExample = "usage: spin application get [options] application-name" 37 ) 38 39 func NewGetCmd(appOptions *applicationOptions) *cobra.Command { 40 options := &getOptions{ 41 applicationOptions: appOptions, 42 expand: false, 43 } 44 45 cmd := &cobra.Command{ 46 Use: "get", 47 Aliases: []string{"get"}, 48 Short: getApplicationShort, 49 Long: getApplicationLong, 50 Example: getApplicationExample, 51 RunE: func(cmd *cobra.Command, args []string) error { 52 return getApplication(cmd, options, args) 53 }, 54 } 55 56 // Note that false here means defaults to false, and flips to true if the flag is present. 57 cmd.PersistentFlags().BoolVarP(&options.expand, "expand", "x", false, "expand app payload to include clusters") 58 59 return cmd 60 } 61 62 func getApplication(cmd *cobra.Command, options *getOptions, args []string) error { 63 applicationName, err := util.ReadArgsOrStdin(args) 64 if err != nil { 65 return err 66 } 67 68 app, resp, err := options.GateClient.ApplicationControllerApi.GetApplicationUsingGET(options.GateClient.Context, applicationName, &gate.ApplicationControllerApiGetApplicationUsingGETOpts{Expand: optional.NewBool(options.expand)}) 69 if resp != nil { 70 switch resp.StatusCode { 71 case http.StatusOK: 72 // pass 73 case http.StatusNotFound: 74 return fmt.Errorf("Application '%s' not found\n", applicationName) 75 default: 76 return fmt.Errorf("Encountered an error getting application, status code: %d\n", resp.StatusCode) 77 } 78 } 79 80 if err != nil { 81 return err 82 } 83 84 if options.expand { 85 // NOTE: expand returns the actual attributes as well as the app's cluster details, nested in 86 // their own fields. This means that the expanded output can't be submitted as input to `save`. 87 options.Ui.JsonOutput(app) 88 } else { 89 // NOTE: app GET wraps the actual app attributes in an 'attributes' field. 90 options.Ui.JsonOutput(app["attributes"]) 91 } 92 93 return nil 94 }