|
| 1 | +/* |
| 2 | +Copyright 2020 Google Inc. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/fatih/color" |
| 27 | + |
| 28 | + "github.com/google/go-jsonnet" |
| 29 | + "github.com/google/go-jsonnet/cmd/internal/cmd" |
| 30 | +) |
| 31 | + |
| 32 | +func version(o io.Writer) { |
| 33 | + fmt.Fprintf(o, "Jsonnet static dependency parser %s\n", jsonnet.Version()) |
| 34 | +} |
| 35 | + |
| 36 | +func usage(o io.Writer) { |
| 37 | + version(o) |
| 38 | + fmt.Fprintln(o) |
| 39 | + fmt.Fprintln(o, "jsonnet-deps {<option>} <filename>...") |
| 40 | + fmt.Fprintln(o) |
| 41 | + fmt.Fprintln(o, "Available options:") |
| 42 | + fmt.Fprintln(o, " -h / --help This message") |
| 43 | + fmt.Fprintln(o, " -J / --jpath <dir> Specify an additional library search dir") |
| 44 | + fmt.Fprintln(o, " (right-most wins)") |
| 45 | + fmt.Fprintln(o, " -o / --output-file <file> Write to the output file rather than stdout") |
| 46 | + fmt.Fprintln(o, " --version Print version") |
| 47 | + fmt.Fprintln(o) |
| 48 | + fmt.Fprintln(o, "Environment variables:") |
| 49 | + fmt.Fprintln(o, " JSONNET_PATH is a colon (semicolon on Windows) separated list of directories") |
| 50 | + fmt.Fprintln(o, " added in reverse order before the paths specified by --jpath (i.e. left-most") |
| 51 | + fmt.Fprintln(o, " wins). E.g. these are equivalent:") |
| 52 | + fmt.Fprintln(o, " JSONNET_PATH=a:b jsonnet -J c -J d") |
| 53 | + fmt.Fprintln(o, " JSONNET_PATH=d:c:a:b jsonnet") |
| 54 | + fmt.Fprintln(o, " jsonnet -J b -J a -J c -J d") |
| 55 | + fmt.Fprintln(o) |
| 56 | + fmt.Fprintln(o, "In all cases:") |
| 57 | + fmt.Fprintln(o, " Multichar options are expanded e.g. -abc becomes -a -b -c.") |
| 58 | + fmt.Fprintln(o, " The -- option suppresses option processing for subsequent arguments.") |
| 59 | + fmt.Fprintln(o, " Note that since filenames and jsonnet programs can begin with -, it is") |
| 60 | + fmt.Fprintln(o, " advised to use -- if the argument is unknown, e.g. jsonnet-deps -- \"$FILENAME\".") |
| 61 | +} |
| 62 | + |
| 63 | +type config struct { |
| 64 | + inputFiles []string |
| 65 | + outputFile string |
| 66 | + jPaths []string |
| 67 | +} |
| 68 | + |
| 69 | +type processArgsStatus int |
| 70 | + |
| 71 | +const ( |
| 72 | + processArgsStatusContinue = iota |
| 73 | + processArgsStatusSuccessUsage = iota |
| 74 | + processArgsStatusFailureUsage = iota |
| 75 | + processArgsStatusSuccess = iota |
| 76 | + processArgsStatusFailure = iota |
| 77 | +) |
| 78 | + |
| 79 | +func processArgs(givenArgs []string, conf *config, vm *jsonnet.VM) (processArgsStatus, error) { |
| 80 | + args := cmd.SimplifyArgs(givenArgs) |
| 81 | + remainingArgs := make([]string, 0, len(args)) |
| 82 | + |
| 83 | + for i := 0; i < len(args); i++ { |
| 84 | + arg := args[i] |
| 85 | + if arg == "-h" || arg == "--help" { |
| 86 | + return processArgsStatusSuccessUsage, nil |
| 87 | + } else if arg == "-v" || arg == "--version" { |
| 88 | + version(os.Stdout) |
| 89 | + return processArgsStatusSuccess, nil |
| 90 | + } else if arg == "-o" || arg == "--output-file" { |
| 91 | + outputFile := cmd.NextArg(&i, args) |
| 92 | + if len(outputFile) == 0 { |
| 93 | + return processArgsStatusFailure, fmt.Errorf("-o argument was empty string") |
| 94 | + } |
| 95 | + conf.outputFile = outputFile |
| 96 | + } else if arg == "-J" || arg == "--jpath" { |
| 97 | + dir := cmd.NextArg(&i, args) |
| 98 | + if len(dir) == 0 { |
| 99 | + return processArgsStatusFailure, fmt.Errorf("-J argument was empty string") |
| 100 | + } |
| 101 | + conf.jPaths = append(conf.jPaths, dir) |
| 102 | + } else if arg == "--" { |
| 103 | + // All subsequent args are not options. |
| 104 | + i++ |
| 105 | + for ; i < len(args); i++ { |
| 106 | + remainingArgs = append(remainingArgs, args[i]) |
| 107 | + } |
| 108 | + break |
| 109 | + } else if len(arg) > 1 && arg[0] == '-' { |
| 110 | + return processArgsStatusFailure, fmt.Errorf("unrecognized argument: %s", arg) |
| 111 | + } else { |
| 112 | + remainingArgs = append(remainingArgs, arg) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if len(remainingArgs) == 0 { |
| 117 | + return processArgsStatusFailureUsage, fmt.Errorf("must give filename") |
| 118 | + } |
| 119 | + conf.inputFiles = remainingArgs |
| 120 | + |
| 121 | + return processArgsStatusContinue, nil |
| 122 | +} |
| 123 | + |
| 124 | +func writeDependencies(dependencies []string, outputFile string) (err error) { |
| 125 | + var f *os.File |
| 126 | + |
| 127 | + if outputFile == "" { |
| 128 | + f = os.Stdout |
| 129 | + } else { |
| 130 | + f, err = os.Create(outputFile) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + defer func() { |
| 135 | + if ferr := f.Close(); ferr != nil { |
| 136 | + err = ferr |
| 137 | + } |
| 138 | + }() |
| 139 | + } |
| 140 | + |
| 141 | + if len(dependencies) != 0 { |
| 142 | + output := strings.Join(dependencies, "\n") + "\n" |
| 143 | + _, err = f.WriteString(output) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return |
| 150 | +} |
| 151 | + |
| 152 | +func main() { |
| 153 | + cmd.StartCPUProfile() |
| 154 | + defer cmd.StopCPUProfile() |
| 155 | + |
| 156 | + vm := jsonnet.MakeVM() |
| 157 | + vm.ErrorFormatter.SetColorFormatter(color.New(color.FgRed).Fprintf) |
| 158 | + |
| 159 | + conf := config{} |
| 160 | + jsonnetPath := filepath.SplitList(os.Getenv("JSONNET_PATH")) |
| 161 | + for i := len(jsonnetPath) - 1; i >= 0; i-- { |
| 162 | + conf.jPaths = append(conf.jPaths, jsonnetPath[i]) |
| 163 | + } |
| 164 | + |
| 165 | + status, err := processArgs(os.Args[1:], &conf, vm) |
| 166 | + if err != nil { |
| 167 | + fmt.Fprintln(os.Stderr, "ERROR: "+err.Error()) |
| 168 | + } |
| 169 | + switch status { |
| 170 | + case processArgsStatusContinue: |
| 171 | + break |
| 172 | + case processArgsStatusSuccessUsage: |
| 173 | + usage(os.Stdout) |
| 174 | + os.Exit(0) |
| 175 | + case processArgsStatusFailureUsage: |
| 176 | + if err != nil { |
| 177 | + fmt.Fprintln(os.Stderr, "") |
| 178 | + } |
| 179 | + usage(os.Stderr) |
| 180 | + os.Exit(1) |
| 181 | + case processArgsStatusSuccess: |
| 182 | + os.Exit(0) |
| 183 | + case processArgsStatusFailure: |
| 184 | + os.Exit(1) |
| 185 | + } |
| 186 | + |
| 187 | + vm.Importer(&jsonnet.FileImporter{JPaths: conf.jPaths}) |
| 188 | + |
| 189 | + for _, file := range conf.inputFiles { |
| 190 | + if _, err := os.Stat(file); err != nil { |
| 191 | + fmt.Fprintln(os.Stderr, err.Error()) |
| 192 | + os.Exit(1) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + dependencies, err := vm.FindDependencies("", conf.inputFiles) |
| 197 | + if err != nil { |
| 198 | + fmt.Fprintln(os.Stderr, err.Error()) |
| 199 | + os.Exit(1) |
| 200 | + } |
| 201 | + cmd.MemProfile() |
| 202 | + |
| 203 | + err = writeDependencies(dependencies, conf.outputFile) |
| 204 | + if err != nil { |
| 205 | + fmt.Fprintln(os.Stderr, err.Error()) |
| 206 | + os.Exit(1) |
| 207 | + } |
| 208 | +} |
0 commit comments