@@ -2,18 +2,33 @@ package main
22
33import (
44 "encoding/json"
5+ "errors"
6+ "os"
7+ "time"
58
69 "github.com/adityalstkp/go-ngx-config/pkg/cli"
10+ "github.com/sirupsen/logrus"
711 "github.com/spf13/cobra"
812)
913
1014func RunParseNgx (cmd * cobra.Command , args []string ) error {
15+ startTime := time .Now ()
16+
1117 filePath , err := cmd .Flags ().GetString ("file" )
1218 if err != nil {
1319 return err
1420 }
1521
16- ast , err := cli .NewNgxConfParser (filePath )
22+ outputFilePath , err := cmd .Flags ().GetString ("output" )
23+ if err != nil {
24+ return err
25+ }
26+
27+ cliOpts := cli.NgxConfParserCliOptions {
28+ Filepath : filePath ,
29+ }
30+
31+ ast , err := cli .NewNgxConfParser (cliOpts )
1732 if err != nil {
1833 return err
1934 }
@@ -23,7 +38,32 @@ func RunParseNgx(cmd *cobra.Command, args []string) error {
2338 return err
2439 }
2540
26- println (string (ast_json ))
41+ if outputFilePath != "" {
42+ if _ , err := os .Stat (outputFilePath ); errors .Is (err , os .ErrNotExist ) {
43+ err := os .MkdirAll (outputFilePath , os .ModePerm )
44+ if err != nil {
45+ return err
46+ }
47+ }
48+
49+ dumpAstJsonFile := outputFilePath + "/dump.json"
50+ f , err := os .Create (dumpAstJsonFile )
51+ if err != nil {
52+ return err
53+ }
54+ defer f .Close ()
55+
56+ _ , err = f .Write (ast_json )
57+ if err != nil {
58+ return err
59+ }
60+ } else {
61+ println (string (ast_json ))
62+ }
63+
64+ elapsed := time .Since (startTime )
65+ logrus .Info ("Process time: " , elapsed )
66+
2767 return nil
2868
2969}
0 commit comments