blog: show a helpful error for invalid template directory
Currently, if the blog binary is executed outside the root directory without any additional flags, it bails out with the following error - "open template/root.tmpl: no such file or directory" This CL prints out a more user friendly error which allows the user to learn that there is a flag with which the template directory can be set. Fixes golang/go#22622 Change-Id: I726e7c28f5e5036146769ca01516368f45a6262c Reviewed-on: https://go-review.googlesource.com/86855 Run-TryBot: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
		
							parent
							
								
									fbec762f83
								
							
						
					
					
						commit
						8ea45f9674
					
				
							
								
								
									
										15
									
								
								blog/blog.go
								
								
								
								
							
							
						
						
									
										15
									
								
								blog/blog.go
								
								
								
								
							| 
						 | 
					@ -73,10 +73,17 @@ type Server struct {
 | 
				
			||||||
func NewServer(cfg Config) (*Server, error) {
 | 
					func NewServer(cfg Config) (*Server, error) {
 | 
				
			||||||
	present.PlayEnabled = cfg.PlayEnabled
 | 
						present.PlayEnabled = cfg.PlayEnabled
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if notExist(cfg.TemplatePath) {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("template directory not found: %s", cfg.TemplatePath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	root := filepath.Join(cfg.TemplatePath, "root.tmpl")
 | 
						root := filepath.Join(cfg.TemplatePath, "root.tmpl")
 | 
				
			||||||
	parse := func(name string) (*template.Template, error) {
 | 
						parse := func(name string) (*template.Template, error) {
 | 
				
			||||||
 | 
							path := filepath.Join(cfg.TemplatePath, name)
 | 
				
			||||||
 | 
							if notExist(path) {
 | 
				
			||||||
 | 
								return nil, fmt.Errorf("template %s was not found in %s", name, cfg.TemplatePath)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		t := template.New("").Funcs(funcMap)
 | 
							t := template.New("").Funcs(funcMap)
 | 
				
			||||||
		return t.ParseFiles(root, filepath.Join(cfg.TemplatePath, name))
 | 
							return t.ParseFiles(root, path)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	s := &Server{cfg: cfg}
 | 
						s := &Server{cfg: cfg}
 | 
				
			||||||
| 
						 | 
					@ -422,3 +429,9 @@ type docsByTime []*Doc
 | 
				
			||||||
func (s docsByTime) Len() int           { return len(s) }
 | 
					func (s docsByTime) Len() int           { return len(s) }
 | 
				
			||||||
func (s docsByTime) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
 | 
					func (s docsByTime) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
 | 
				
			||||||
func (s docsByTime) Less(i, j int) bool { return s[i].Time.After(s[j].Time) }
 | 
					func (s docsByTime) Less(i, j int) bool { return s[i].Time.After(s[j].Time) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// notExist reports whether the path exists or not.
 | 
				
			||||||
 | 
					func notExist(path string) bool {
 | 
				
			||||||
 | 
						_, err := os.Stat(path)
 | 
				
			||||||
 | 
						return os.IsNotExist(err)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue