|
1 | 1 | package cmd
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/nginxinc/crossplane-go/pkg/builder" |
| 11 | + "github.com/nginxinc/crossplane-go/pkg/lexer" |
| 12 | + "github.com/nginxinc/crossplane-go/pkg/parser" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var rootCmd = &cobra.Command{ |
| 17 | + Use: "crossplane", |
| 18 | + Short: "Crossplane is a quick and reliable way to convert NGINX configurations into JSON and back.", |
| 19 | + Long: `A quick and reliable way to convert NGINX configurations into JSON and back. |
| 20 | +
|
| 21 | +built with ❤ by nginxinc and gophers who live in Cork and are from Cork |
| 22 | +Complete documentation is available at: https://github.com/nginxinc/crossplane-go |
| 23 | + `, |
| 24 | +} |
| 25 | + |
| 26 | +var parseCmd = &cobra.Command{ |
| 27 | + Use: "parse [/path/to/nginx.conf]", |
| 28 | + Short: "Parses an NGINX config for a JSON format", |
| 29 | + Args: cobra.ExactArgs(1), |
| 30 | +} |
| 31 | + |
| 32 | +var buildCmd = &cobra.Command{ |
| 33 | + Use: "build [/path/to/payload.json]", |
| 34 | + Short: "Build an NGINX config using a JSON format", |
| 35 | + Args: cobra.ExactArgs(1), |
| 36 | +} |
| 37 | + |
| 38 | +var lexCmd = &cobra.Command{ |
| 39 | + Use: "lex [/path/to/tokens-file.txt]", |
| 40 | + Short: "Lexes tokens from an NGINX config file", |
| 41 | + Args: cobra.ExactArgs(1), |
| 42 | +} |
| 43 | + |
3 | 44 | // Execute - cmd entrypoint
|
4 |
| -func Execute() { |
| 45 | +func Execute() (err error) { |
| 46 | + |
| 47 | + // TODO: strict mode (BoolVarP) |
| 48 | + // TODO: ignore directives (StringArrayVarP) |
| 49 | + |
| 50 | + var ( |
| 51 | + indent uint |
| 52 | + outFile string |
| 53 | + catchErrors, combine, comment, single, strict, checkctx, checkargs bool |
| 54 | + ignore []string |
| 55 | + ) |
| 56 | + parseCmd.Flags().UintVarP(&indent, "indent", "i", 4, "Set spaces for indentation") |
| 57 | + parseCmd.Flags().StringVarP(&outFile, "out", "o", "", "Output to a file. If not specified, it will output to STDOUT") |
| 58 | + parseCmd.Flags().BoolVar(&catchErrors, "catch-errors", false, "Stop parse after first error") |
| 59 | + parseCmd.Flags().BoolVar(&combine, "combine", false, "Inline includes to create single config object") |
| 60 | + parseCmd.Flags().BoolVar(&single, "single", false, "Skip includes") |
| 61 | + parseCmd.Flags().BoolVar(&strict, "strict", false, "Strict mode: error on unrecognized directives") |
| 62 | + parseCmd.Flags().BoolVar(&checkctx, "check-ctx", false, "Run context analysis on directives") |
| 63 | + parseCmd.Flags().BoolVar(&checkargs, "check-args", false, "Run arg count analysis on directives") |
| 64 | + parseCmd.Flags().BoolVar(&comment, "include-comments", false, "Include comments in json") |
| 65 | + parseCmd.Flags().StringArrayVar(&ignore, "ignore", []string{}, "List of ignored directives") |
| 66 | + parseCmd.Run = func(cmd *cobra.Command, args []string) { |
| 67 | + filename := args[0] |
| 68 | + _, err := os.Stat(filename) |
| 69 | + if err != nil { |
| 70 | + log.Fatalf("Error: cannot access file %s", filename) |
| 71 | + } |
| 72 | + payload, err := parser.Parse(filename, catchErrors, ignore, single, comment, strict, combine, true, checkctx, checkargs) |
| 73 | + if err != nil { |
| 74 | + log.Fatalf("Error parsing file %s: %v", filename, err) |
| 75 | + } |
| 76 | + s := make([]string, indent) |
| 77 | + b, err := json.MarshalIndent(payload, "", strings.Join(s, " ")) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("Error marshalling data: %v", err) |
| 80 | + } |
| 81 | + if outFile != "" { |
| 82 | + if err = ioutil.WriteFile(outFile, b, 0644); err != nil { |
| 83 | + log.Fatalf("Error writing data file %s: %v", outFile, err) |
| 84 | + } |
| 85 | + } else { |
| 86 | + os.Stdout.Write(b) |
| 87 | + os.Stdout.Write([]byte("\n")) // compatibility: always return newline at end |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + var ( |
| 92 | + buildPath string |
| 93 | + force, tabs bool |
| 94 | + ) |
| 95 | + buildCmd.Flags().UintVarP(&indent, "indent", "i", 4, "Set spaces for indentation") |
| 96 | + buildCmd.Flags().StringVarP(&buildPath, "path", "d", "", "Output to a directory. If not specified, it will output to STDOUT") |
| 97 | + buildCmd.Flags().BoolVarP(&tabs, "tabs", "t", false, "Use tabs instead of spaces on built files") |
| 98 | + buildCmd.Flags().BoolVarP(&force, "force", "f", false, "Force overwrite existing files") |
| 99 | + buildCmd.Run = func(cmd *cobra.Command, args []string) { |
| 100 | + filename := args[0] |
| 101 | + _, err := os.Stat(filename) |
| 102 | + if err != nil { |
| 103 | + log.Fatalf("Error: cannot access file %s", filename) |
| 104 | + } |
| 105 | + |
| 106 | + f, err := ioutil.ReadFile(filename) |
| 107 | + if err != nil { |
| 108 | + log.Fatalf("Error: cannot read file %s: %v", filename, err) |
| 109 | + } |
| 110 | + input, err := builder.NewPayload(f) |
| 111 | + if err != nil { |
| 112 | + log.Fatalf("Error translating payload file %s: %v", filename, err) |
| 113 | + } |
| 114 | + output, err := builder.BuildFiles(input, buildPath, int(indent), tabs, false) |
| 115 | + if err != nil { |
| 116 | + log.Fatalf("Error: cannot build file %s: %v", filename, err) |
| 117 | + } |
| 118 | + os.Stdout.WriteString(output) |
| 119 | + os.Stdout.Write([]byte("\n")) // compatibility: always return newline at end |
| 120 | + } |
| 121 | + |
| 122 | + var ( |
| 123 | + lineNumbers bool |
| 124 | + ) |
| 125 | + lexCmd.Flags().UintVarP(&indent, "indent", "i", 4, "Set spaces for indentation") |
| 126 | + lexCmd.Flags().StringVarP(&outFile, "out", "o", "", "Output to a file. If not specified, it will output to STDOUT") |
| 127 | + lexCmd.Flags().BoolVarP(&lineNumbers, "line-numbers", "n", false, "Include line numbers in output") |
| 128 | + lexCmd.Run = func(cmd *cobra.Command, args []string) { |
| 129 | + filename := args[0] |
| 130 | + _, err := os.Stat(filename) |
| 131 | + if err != nil { |
| 132 | + log.Fatalf("Error: cannot access file %s", filename) |
| 133 | + } |
| 134 | + f, err := ioutil.ReadFile(filename) |
| 135 | + if err != nil { |
| 136 | + log.Fatalf("Error: cannot read file %s: %v", filename, err) |
| 137 | + } |
| 138 | + input := string(f) |
| 139 | + tokenStream := lexer.LexScanner(input) |
| 140 | + li := []interface{}{} |
| 141 | + for token := range tokenStream { |
| 142 | + li = append(li, token.Repr(lineNumbers)) |
| 143 | + } |
| 144 | + b, err := json.Marshal(li) |
| 145 | + if err != nil { |
| 146 | + log.Fatalf("Error marshalling token stream data from lexer: %v", err) |
| 147 | + } |
| 148 | + os.Stdout.Write(b) |
| 149 | + os.Stdout.Write([]byte("\n")) // compatibility: always return newline at end |
| 150 | + } |
5 | 151 |
|
| 152 | + rootCmd.AddCommand(parseCmd, buildCmd, lexCmd) |
| 153 | + err = rootCmd.Execute() |
| 154 | + return err |
6 | 155 | }
|
0 commit comments