소스 검색

compressed configuration using json tags for cli flag assignment

optimize error capturing logic
Casey DeLorme 8 년 전
부모
커밋
7c1d3bdb6b
3개의 변경된 파일21개의 추가작업 그리고 34개의 파일을 삭제
  1. 1 5
      cmd/staticmd/main.go
  2. 1 1
      cmd/staticmd/main_test.go
  3. 19 28
      generator.go

+ 1 - 5
cmd/staticmd/main.go

@@ -37,11 +37,7 @@ func configure() generator {
 	appOptions.Example("-t template.tmpl -i src/ -o out/ -r")
 	flags := appOptions.Parse()
 
-	smd.TemplateFile, _ = maps.String(flags, smd.TemplateFile, "template")
-	smd.Input, _ = maps.String(flags, smd.Input, "input")
-	smd.Output, _ = maps.String(flags, smd.Output, "output")
-	smd.Book, _ = maps.Bool(flags, smd.Book, "book")
-	smd.Relative, _ = maps.Bool(flags, smd.Relative, "relative")
+	maps.To(smd, flags)
 
 	return smd
 }

+ 1 - 1
cmd/staticmd/main_test.go

@@ -43,7 +43,7 @@ func TestConfigure(t *testing.T) {
 	}
 
 	// check values on generator match cli parameters
-	if g.Input != "/in/" || g.Output != "/out/" || !g.Book || g.TemplateFile != "afile" {
+	if g.Input != "/in/" || g.Output != "/out/" || !g.Relative || !g.Book || g.TemplateFile != "afile" {
 		t.FailNow()
 	}
 }

+ 19 - 28
generator.go

@@ -23,12 +23,13 @@ type ht interface {
 }
 
 type Generator struct {
-	Input        string
-	Output       string
-	TemplateFile string
-	Book         bool
-	Relative     bool
-	Logger       logger
+	Book         bool   `json:"book,omitempty"`
+	Input        string `json:"input,omitempty"`
+	Output       string `json:"output,omitempty"`
+	Relative     bool   `json:"relative,omitempty"`
+	TemplateFile string `json:"template,omitempty"`
+
+	Logger logger
 
 	version  string
 	pages    []string
@@ -55,9 +56,9 @@ func (self *Generator) walk(path string, file os.FileInfo, err error) error {
 	return err
 }
 
-func (self *Generator) multi() (err error) {
+func (self *Generator) multi() error {
 	navi := make(map[string][]navigation)
-	var terr error
+	var err error
 
 	for i, _ := range self.pages {
 		out := self.ior(self.pages[i])
@@ -84,9 +85,8 @@ func (self *Generator) multi() (err error) {
 		if _, ok := navi[dir]; !ok {
 			navi[dir] = make([]navigation, 0)
 			if ok, _ := exists(dir); !ok {
-				if e := mkdirall(dir, 0770); e != nil {
-					self.Logger.Error("failed to create path: %s, %s", dir, e)
-					terr = e
+				if err = mkdirall(dir, 0770); err != nil {
+					self.Logger.Error("failed to create path: %s, %s", dir, err)
 				}
 			}
 		}
@@ -98,7 +98,7 @@ func (self *Generator) multi() (err error) {
 		var markdown []byte
 		if markdown, err = readfile(p); err != nil {
 			self.Logger.Error("failed to read file: %s, %s", p, err)
-			return
+			return err
 		}
 
 		out := self.ior(p)
@@ -136,18 +136,14 @@ func (self *Generator) multi() (err error) {
 		}
 	}
 
-	if err == nil {
-		err = terr
-	}
-
-	return
+	return err
 }
 
-func (self *Generator) single() (err error) {
+func (self *Generator) single() error {
 	content := make([]byte, 0)
 	toc := "\n"
 	previous_depth := 0
-	var terr error
+	var err error
 
 	for _, p := range self.pages {
 		shorthand := strings.TrimPrefix(p, self.Input+string(os.PathSeparator))
@@ -158,10 +154,9 @@ func (self *Generator) single() (err error) {
 		anchor := strings.Replace(shorthand, string(os.PathSeparator), "-", -1)
 		toc = toc + strings.Repeat("\t", depth) + "- [" + basename(p) + "](#" + anchor + ")\n"
 
-		markdown, e := readfile(p)
-		if e != nil {
-			self.Logger.Error("failed to read file: %s (%s)", p, e)
-			terr = e
+		var markdown []byte
+		if markdown, err = readfile(p); err != nil {
+			self.Logger.Error("failed to read file: %s (%s)", p, err)
 			continue
 		}
 
@@ -200,11 +195,7 @@ func (self *Generator) single() (err error) {
 		self.Logger.Error("%s\n", err)
 	}
 
-	if err == nil {
-		err = terr
-	}
-
-	return
+	return err
 }
 
 func (self *Generator) Generate() error {