github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/aid/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 aid 17 18 import ( 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 23 "github.com/awslabs/clencli/helper" 24 "github.com/sirupsen/logrus" 25 "github.com/spf13/cobra" 26 ) 27 28 // DownloadGitIgnore .. 29 func DownloadGitIgnore(cmd *cobra.Command, input string) (bool, error) { 30 bytes, err := requestGitIgnore(cmd, input) 31 if err != nil { 32 logrus.Errorf("unable to download gitignore file\n%v", err) 33 return false, err 34 } 35 36 if !saveGitIgnoreAsFile(bytes) { 37 logrus.Errorln("unable to save gitignore API response as file") 38 return false, fmt.Errorf("unable to create .gitignore file") 39 } 40 41 return true, nil 42 } 43 44 // GetGitIgnoreList ... 45 func GetGitIgnoreList() string { 46 bytes, err := requestGitIgnoreList() 47 if err != nil { 48 logrus.Errorf("unable to fetch the gitignore list") 49 } 50 return string(bytes) 51 } 52 53 func requestGitIgnore(cmd *cobra.Command, input string) ([]byte, error) { 54 url := fmt.Sprintf("https://www.toptal.com/developers/gitignore/api/%s", input) 55 var response []byte 56 57 var client http.Client 58 resp, err := client.Get(url) 59 if err != nil { 60 logrus.Errorf("unexpected error while performing GET on Toptal API\n%v", err) 61 return response, fmt.Errorf("unable to fetch gitignore API\n%v", err) 62 } 63 defer resp.Body.Close() 64 65 if resp.StatusCode == http.StatusOK { 66 response, err = ioutil.ReadAll(resp.Body) 67 if err != nil { 68 return response, fmt.Errorf("unexpected error while reading Unsplash response \n%v", err) 69 } 70 71 return response, nil 72 } 73 74 return response, err 75 } 76 77 func requestGitIgnoreList() ([]byte, error) { 78 url := fmt.Sprintf("https://www.toptal.com/developers/gitignore/api/list") 79 var response []byte 80 81 var client http.Client 82 resp, err := client.Get(url) 83 if err != nil { 84 logrus.Errorf("unexpected error while performing GET on Toptal API\n%v", err) 85 return response, fmt.Errorf("unable to fetch gitignore API\n%v", err) 86 } 87 defer resp.Body.Close() 88 89 if resp.StatusCode == http.StatusOK { 90 response, err = ioutil.ReadAll(resp.Body) 91 if err != nil { 92 return response, fmt.Errorf("unexpected error while reading Unsplash response \n%v", err) 93 } 94 95 return response, nil 96 } 97 98 return response, err 99 } 100 101 func saveGitIgnoreAsFile(bytes []byte) bool { 102 return helper.WriteFile(".gitignore", bytes) 103 }