11package output
22
33import (
4+ "bytes"
45 "context"
56 "fmt"
7+ "log"
68 "os"
79 "path"
810 "time"
@@ -55,28 +57,14 @@ func (o *LocalOutput) Write(ctx context.Context, policyManifest *packaging.Polic
5557 info , err := os .Stat (fullpath )
5658
5759 if os .IsNotExist (err ) {
58- err = os .MkdirAll (path .Dir (fullpath ), 0744 )
59- if err != nil {
60- return err
61- }
62- file , err := os .Create (fullpath )
63- if err != nil {
64- return err
65- }
66-
67- _ , err = file .Write (buffer .Bytes ())
68- if err != nil {
69- return err
70- }
60+ return writeBufferToPath (fullpath , buffer )
7161 } else if err != nil {
7262 return err
7363 } else if info .IsDir () {
7464 return fmt .Errorf ("%q is an existing directory" , fullpath )
7565 } else {
7666 return fmt .Errorf ("%q is an existing file" , fullpath )
7767 }
78-
79- return nil
8068}
8169
8270// WriteIndex exports clusterSummary data in the specified format
@@ -90,26 +78,27 @@ func (o *LocalOutput) WriteIndex(ctx context.Context, cluster string, timestamp
9078 info , err := os .Stat (fullpath )
9179
9280 if os .IsNotExist (err ) {
93- err = os .MkdirAll (path .Dir (fullpath ), 0744 )
94- if err != nil {
95- return err
96- }
97- file , err := os .Create (fullpath )
98- if err != nil {
99- return err
100- }
101-
102- _ , err = file .Write (buffer .Bytes ())
103- if err != nil {
104- return err
105- }
81+ return writeBufferToPath (fullpath , buffer )
10682 } else if err != nil {
10783 return err
10884 } else if info .IsDir () {
10985 return fmt .Errorf ("%q is an existing directory" , fullpath )
11086 } else {
111- return fmt .Errorf ("%q is an existing file" , fullpath )
87+ log .Printf ("%q is an existing index, overwriting..." , fullpath )
88+ return writeBufferToPath (fullpath , buffer )
89+ }
90+ }
91+
92+ func writeBufferToPath (fullpath string , buffer * bytes.Buffer ) error {
93+ err := os .MkdirAll (path .Dir (fullpath ), 0744 )
94+ if err != nil {
95+ return err
96+ }
97+ file , err := os .Create (fullpath )
98+ if err != nil {
99+ return err
112100 }
113101
114- return nil
102+ _ , err = file .Write (buffer .Bytes ())
103+ return err
115104}
0 commit comments