From 7a2991a99a3a1c3050515b7ea9e591871db43db8 Mon Sep 17 00:00:00 2001 From: Jeremy Clerc Date: Sat, 12 Sep 2015 10:53:03 +0200 Subject: [PATCH] aggregated file creation in pki structure init --- pkg/easyca/easyca.go | 68 ++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/pkg/easyca/easyca.go b/pkg/easyca/easyca.go index a0fff16..06bb587 100644 --- a/pkg/easyca/easyca.go +++ b/pkg/easyca/easyca.go @@ -376,50 +376,38 @@ func GeneratePKIStructure(pkiroot string) error { } } - serial, err := os.Create(filepath.Join(pkiroot, "serial")) - if err != nil { - return fmt.Errorf("create serial: %v", err) - } - defer serial.Close() - n, err := fmt.Fprintln(serial, "01") - if err != nil { - return fmt.Errorf("write serial: %v", err) - } - if n == 0 { - return fmt.Errorf("write serial, written 0 bytes") - } + files := []struct { + Name string + Content string + File *os.File + }{ + {Name: "serial", Content: "01"}, + {Name: "crlnumber", Content: "01"}, + {Name: "index.txt", Content: ""}, + {Name: "index.txt.attr", Content: "unique_subject = no"}, + } + for _, f := range files { + // if using := here i get needs identifier, hm ? + var err error + f.File, err = os.Create(filepath.Join(pkiroot, f.Name)) + if err != nil { + return fmt.Errorf("create %v: %v", f.Name, err) + } + defer f.File.Close() - crlnumber, err := os.Create(filepath.Join(pkiroot, "crlnumber")) - if err != nil { - return fmt.Errorf("create crlnumber: %v", err) - } - defer crlnumber.Close() - n, err = fmt.Fprintln(crlnumber, "01") - if err != nil { - return fmt.Errorf("write crlnumber: %v", err) - } - if n == 0 { - return fmt.Errorf("write crlnumber, written 0 bytes") - } + if len(f.Content) == 0 { + continue + } - index, err := os.Create(filepath.Join(pkiroot, "index.txt")) - if err != nil { - return fmt.Errorf("create index: %v", err) + n, err := fmt.Fprintln(f.File, f.Content) + if err != nil { + return fmt.Errorf("write %v: %v", f.Name, err) + } + if n == 0 { + return fmt.Errorf("write %v, written 0 bytes", f.Name) + } } - defer index.Close() - indexattr, err := os.Create(filepath.Join(pkiroot, "index.txt.attr")) - if err != nil { - return fmt.Errorf("create index.txt.attr: %v", err) - } - defer indexattr.Close() - n, err = fmt.Fprintln(indexattr, "unique_subject = no") - if err != nil { - return fmt.Errorf("write index.txt.attr: %v", err) - } - if n == 0 { - return fmt.Errorf("write index.txt.attr, written 0 bytes") - } return nil }