github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/controller/gitignore.go (about) 1 /* 2 Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 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 16 package controller 17 18 import ( 19 "fmt" 20 "os" 21 22 "github.com/awslabs/clencli/cobra/aid" 23 "github.com/awslabs/clencli/helper" 24 "github.com/sirupsen/logrus" 25 "github.com/spf13/cobra" 26 ) 27 28 var gitIgnoreArgs = []string{"list"} 29 30 // GitIgnoreCmd .... 31 func GitIgnoreCmd() *cobra.Command { 32 man, err := helper.GetManual("gitignore") 33 if err != nil { 34 fmt.Println(err) 35 os.Exit(1) 36 } 37 38 cmd := &cobra.Command{ 39 Use: man.Use, 40 Short: man.Short, 41 Long: man.Long, 42 Example: man.Example, 43 PreRunE: gitIgnorePreRun, 44 RunE: gitIgnoreRun, 45 } 46 47 cmd.Flags().StringP("input", "i", "", "Gitignore input. If multiple, comma-separated") 48 49 return cmd 50 } 51 52 func gitIgnorePreRun(cmd *cobra.Command, args []string) error { 53 logrus.Traceln("start: command gitignore pre-run") 54 55 if len(args) == 0 { 56 input, err := cmd.Flags().GetString("input") 57 58 if err != nil { 59 logrus.Errorf("unable to access flag input\n%v", err) 60 return err 61 } 62 63 if input == "" { 64 logrus.Errorln("no flag or argument provided") 65 return fmt.Errorf("no flag or argument provided") 66 } 67 } else if len(args) == 1 && args[0] != "list" { 68 logrus.Errorf("unknow argument passed: %v", args) 69 return fmt.Errorf("unknown argument provided: %s", args[0]) 70 } 71 72 logrus.Traceln("end: command gitignore pre-run") 73 74 return nil 75 } 76 77 func gitIgnoreRun(cmd *cobra.Command, args []string) error { 78 logrus.Traceln("start: command gitignore run") 79 80 if len(args) > 0 && args[0] == "list" { 81 list := aid.GetGitIgnoreList() 82 if list == "" { 83 return fmt.Errorf("unable to get gitignore list") 84 } 85 86 cmd.Println(list) 87 } else { 88 input, err := cmd.Flags().GetString("input") 89 if err != nil { 90 return err 91 } 92 93 downloaded, err := aid.DownloadGitIgnore(cmd, input) 94 if err != nil { 95 logrus.Errorf("unable to download gitignore\n%v", err) 96 return err 97 } 98 99 if downloaded { 100 cmd.Println(".gitignore created successfully") 101 } 102 } 103 104 logrus.Traceln("end: command gitignore run") 105 return nil 106 }