github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/controller/render.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 "errors" 20 "fmt" 21 "os" 22 "strings" 23 24 "github.com/awslabs/clencli/cobra/aid" 25 "github.com/awslabs/clencli/cobra/dao" 26 "github.com/awslabs/clencli/cobra/model" 27 "github.com/awslabs/clencli/helper" 28 "github.com/sirupsen/logrus" 29 "github.com/spf13/cobra" 30 ) 31 32 var renderValidArgs = []string{"template"} 33 34 // RenderCmd command to render templates 35 func RenderCmd() *cobra.Command { 36 man, err := helper.GetManual("render") 37 if err != nil { 38 fmt.Println(err) 39 os.Exit(1) 40 } 41 42 cmd := &cobra.Command{ 43 Use: man.Use, 44 Short: man.Short, 45 Long: man.Long, 46 ValidArgs: renderValidArgs, 47 Args: cobra.OnlyValidArgs, 48 PreRunE: renderPreRun, 49 RunE: renderRun, 50 } 51 52 cmd.Flags().StringP("name", "n", "readme", "Database file name of the template to be rendered (it must be under clencli/ directory.") 53 54 return cmd 55 } 56 57 func renderPreRun(cmd *cobra.Command, args []string) error { 58 logrus.Traceln("start: command render pre-run") 59 60 if err := helper.ValidateCmdArgs(cmd, args, "render"); err != nil { 61 return err 62 } 63 64 if err := helper.ValidateCmdArgAndFlag(cmd, args, "render", "template", "name"); err != nil { 65 return err 66 } 67 68 name, err := cmd.Flags().GetString("name") 69 if err != nil { 70 logrus.Errorf("error: unable to access flag name\n%v", err) 71 return fmt.Errorf("unable to access flag name\n%v", err) 72 } 73 74 path := "clencli/" + name + ".yaml" 75 if !helper.FileExists(path) { 76 logrus.Errorf("missing database " + path) 77 return errors.New("missing database " + path) 78 } 79 80 path = "clencli/" + name + ".tmpl" 81 if !helper.FileExists(path) { 82 logrus.Errorf("missing template " + path) 83 return errors.New("missing template " + path) 84 } 85 86 logrus.Traceln("end: command render pre-run") 87 return nil 88 } 89 90 func renderRun(cmd *cobra.Command, args []string) error { 91 logrus.Traceln("start: command render run") 92 93 name, err := cmd.Flags().GetString("name") 94 if err != nil { 95 logrus.Errorf("error: unable to render template "+name+"\n%v", err) 96 return fmt.Errorf("unable to render template "+name+"\n%v", err) 97 } 98 99 // TODO: fix this, causing issues on Windows 100 101 // remove any trailing whitespaces 102 // path := "./clencli/" + name + ".yaml" 103 // if err := helper.TrimRightFile(path, true); err != nil { 104 // logrus.Errorf("unexpected err: %v", err) 105 // return fmt.Errorf("unable to remove white spaces from %s.yaml\n%v", name, err) 106 // } 107 108 if err := updateLogo(profile); err != nil { 109 logrus.Errorf("Unexpected error: %v", err) 110 return fmt.Errorf("unable to update logo url\n%v", err) 111 } 112 113 if err := aid.BuildTemplate(name); err != nil { 114 logrus.Errorf("Unexpected error: %v", err) 115 return fmt.Errorf("unable to render template "+name+"\n%v", err) 116 } 117 118 cmd.Println("Template " + name + ".tmpl rendered as " + strings.ToUpper(name) + ".md.") 119 120 logrus.Traceln("end: command render run") 121 return nil 122 } 123 124 func updateLogo(profile string) error { 125 126 if !updateLogoFromUnsplashFile() { 127 return updateLogoFromConfigurations(profile) 128 } 129 130 return nil 131 } 132 133 func updateLogoFromUnsplashFile() bool { 134 if helper.FileExists("unsplash.yaml") { 135 configPath, _ := os.Getwd() 136 configName := "unsplash" 137 configType := "yaml" 138 139 var response model.UnsplashRandomPhotoResponse 140 141 v, err := aid.ReadConfigAsViper(configPath, configName, configType) 142 if err != nil { 143 logrus.Errorf("unable to read unsplash.yaml as viper object\n%v", err) 144 return false 145 } 146 147 err = v.Unmarshal(&response) 148 if err != nil { 149 logrus.Errorf("unable to unmarshall unsplash.yaml as unsplash response\n%v", err) 150 return false 151 } 152 153 err = helper.DownloadFile(response.Urls.Regular, "clencli", "logo.jpeg") 154 if err != nil { 155 logrus.Errorf("unable to download photo\n%v", err) 156 return false 157 } 158 159 response.Urls.Regular = "clencli/logo.jpeg" 160 161 readMe, err := dao.GetReadMe() 162 if err != nil { 163 logrus.Errorf("Unable to get local readme config\n%v", err) 164 return false 165 } 166 167 err = aid.UpdateReadMeLogoURL(readMe, response) 168 if err != nil { 169 logrus.Errorf("unable to update logo URL\n%s", err) 170 return false 171 } 172 173 return true 174 } 175 176 return false 177 } 178 179 func updateLogoFromConfigurations(profile string) error { 180 if aid.ConfigurationsDirectoryExist() { 181 if aid.CredentialsFileExist() && aid.ConfigurationsFileExist() { 182 183 // ignore error, as credentials doesn't exist 184 cred, err := dao.GetCredentialByProvider(profile, "unsplash") 185 if err != nil { 186 logrus.Warnf("no unsplash credential found\n%v", err) 187 return nil 188 } 189 190 if cred.AccessKey != "" && cred.SecretKey != "" { 191 readMe, err := dao.GetReadMe() 192 if err != nil { 193 return fmt.Errorf("Unable to get local readme config\n%v", err) 194 } 195 196 params := dao.GetUnsplashRandomPhotoParameters(profile) 197 if (model.UnsplashRandomPhotoParameters{}) == params { 198 logrus.Warnf("no unsplash random photo parameters configuration found or enabled\n%v", err) 199 return nil 200 } 201 202 response, err := aid.RequestRandomPhoto(params, cred) 203 if err != nil { 204 logrus.Warnf("unable to fetch response from unsplash during render command\n%v", err) 205 return err 206 } 207 208 err = aid.UpdateReadMeLogoURL(readMe, response) 209 if err != nil { 210 return fmt.Errorf("unable to update logo URL\n%s", err) 211 } 212 } 213 } 214 } 215 216 return nil 217 }