Artifact
77a1f662b4c946a3777275d22671ea510a22582f33f89d1abd29a7d26399af20:
- File
cmd/gofigure/main.go
— part of check-in
[59ad0ff5ab]
at
2023-10-13 04:13:45
on branch trunk
— minor comments and some cleanup
(user:
dnc
size: 1830)
0000: 2f 2f 20 43 6f 70 79 72 69 67 68 74 20 28 43 29 // Copyright (C)
0010: 20 44 61 76 69 64 20 4e 2e 20 43 6f 68 65 6e 0a David N. Cohen.
0020: 0a 2f 2f 20 54 68 69 73 20 70 72 6f 67 72 61 6d .// This program
0030: 20 69 73 20 66 72 65 65 20 73 6f 66 74 77 61 72 is free softwar
0040: 65 3a 20 79 6f 75 20 63 61 6e 20 72 65 64 69 73 e: you can redis
0050: 74 72 69 62 75 74 65 20 69 74 20 61 6e 64 2f 6f tribute it and/o
0060: 72 20 6d 6f 64 69 66 79 0a 2f 2f 20 69 74 20 75 r modify.// it u
0070: 6e 64 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f nder the terms o
0080: 66 20 74 68 65 20 47 4e 55 20 41 66 66 65 72 6f f the GNU Affero
0090: 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 20 General Public
00a0: 4c 69 63 65 6e 73 65 20 61 73 20 70 75 62 6c 69 License as publi
00b0: 73 68 65 64 20 62 79 0a 2f 2f 20 74 68 65 20 46 shed by.// the F
00c0: 72 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 ree Software Fou
00d0: 6e 64 61 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 ndation, either
00e0: 76 65 72 73 69 6f 6e 20 33 20 6f 66 20 74 68 65 version 3 of the
00f0: 20 4c 69 63 65 6e 73 65 2c 20 6f 72 0a 2f 2f 20 License, or.//
0100: 28 61 74 20 79 6f 75 72 20 6f 70 74 69 6f 6e 29 (at your option)
0110: 20 61 6e 79 20 6c 61 74 65 72 20 76 65 72 73 69 any later versi
0120: 6f 6e 2e 0a 0a 2f 2f 20 54 68 69 73 20 70 72 6f on...// This pro
0130: 67 72 61 6d 20 69 73 20 64 69 73 74 72 69 62 75 gram is distribu
0140: 74 65 64 20 69 6e 20 74 68 65 20 68 6f 70 65 20 ted in the hope
0150: 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 that it will be
0160: 75 73 65 66 75 6c 2c 0a 2f 2f 20 62 75 74 20 57 useful,.// but W
0170: 49 54 48 4f 55 54 20 41 4e 59 20 57 41 52 52 41 ITHOUT ANY WARRA
0180: 4e 54 59 3b 20 77 69 74 68 6f 75 74 20 65 76 65 NTY; without eve
0190: 6e 20 74 68 65 20 69 6d 70 6c 69 65 64 20 77 61 n the implied wa
01a0: 72 72 61 6e 74 79 20 6f 66 0a 2f 2f 20 4d 45 52 rranty of.// MER
01b0: 43 48 41 4e 54 41 42 49 4c 49 54 59 20 6f 72 20 CHANTABILITY or
01c0: 46 49 54 4e 45 53 53 20 46 4f 52 20 41 20 50 41 FITNESS FOR A PA
01d0: 52 54 49 43 55 4c 41 52 20 50 55 52 50 4f 53 45 RTICULAR PURPOSE
01e0: 2e 20 20 53 65 65 20 74 68 65 0a 2f 2f 20 47 4e . See the.// GN
01f0: 55 20 41 66 66 65 72 6f 20 47 65 6e 65 72 61 6c U Affero General
0200: 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 Public License
0210: 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 for more details
0220: 2e 0a 0a 2f 2f 20 59 6f 75 20 73 68 6f 75 6c 64 ...// You should
0230: 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 61 have received a
0240: 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e 55 copy of the GNU
0250: 20 41 66 66 65 72 6f 20 47 65 6e 65 72 61 6c 20 Affero General
0260: 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 0a 2f Public License./
0270: 2f 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 69 / along with thi
0280: 73 20 70 72 6f 67 72 61 6d 2e 20 20 49 66 20 6e s program. If n
0290: 6f 74 2c 20 73 65 65 20 3c 68 74 74 70 73 3a 2f ot, see <https:/
02a0: 2f 77 77 77 2e 67 6e 75 2e 6f 72 67 2f 6c 69 63 /www.gnu.org/lic
02b0: 65 6e 73 65 73 2f 3e 2e 0a 0a 2f 2f 20 67 6f 66 enses/>...// gof
02c0: 69 67 75 72 65 20 63 6f 6d 6d 61 6e 64 20 69 73 igure command is
02d0: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 68 65 6c intended to hel
02e0: 70 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 20 63 p troubleshoot c
02f0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2f 2f 20 onfiguration.//
0300: 66 69 6c 65 73 20 75 73 69 6e 67 20 74 68 65 20 files using the
0310: 67 6f 66 69 67 75 72 65 20 73 79 6e 74 61 78 2e gofigure syntax.
0320: 20 20 49 74 20 70 72 69 6e 74 73 20 74 68 65 20 It prints the
0330: 70 61 74 68 73 20 61 6e 64 20 76 61 6c 75 65 73 paths and values
0340: 0a 2f 2f 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 .// encountered
0350: 77 68 65 6e 20 70 61 72 73 69 6e 67 20 63 6f 6e when parsing con
0360: 66 69 67 75 72 61 74 69 6f 6e 20 66 69 6c 65 73 figuration files
0370: 2e 0a 2f 2f 0a 2f 2f 20 55 73 61 67 65 0a 2f 2f ..//.// Usage.//
0380: 0a 2f 2f 20 20 20 20 67 6f 66 69 67 75 72 65 20 .// gofigure
0390: 66 69 6c 65 6e 61 6d 65 20 5b 2e 2e 2e 5d 0a 2f filename [...]./
03a0: 2f 0a 70 61 63 6b 61 67 65 20 6d 61 69 6e 0a 0a /.package main..
03b0: 69 6d 70 6f 72 74 20 28 0a 09 22 65 6e 63 6f 64 import (.."encod
03c0: 69 6e 67 2f 6a 73 6f 6e 22 0a 09 22 65 72 72 6f ing/json".."erro
03d0: 72 73 22 0a 09 22 66 6c 61 67 22 0a 09 22 66 6d rs".."flag".."fm
03e0: 74 22 0a 09 22 69 6f 22 0a 09 22 6f 73 22 0a 0a t".."io".."os"..
03f0: 09 22 73 72 63 2e 64 31 30 2e 64 65 76 2f 67 6f ."src.d10.dev/go
0400: 66 69 67 75 72 65 22 0a 29 0a 0a 66 75 6e 63 20 figure".)..func
0410: 6d 61 69 6e 28 29 20 7b 0a 09 66 6c 61 67 2e 50 main() {..flag.P
0420: 61 72 73 65 28 29 0a 0a 09 6d 75 78 20 3a 3d 20 arse()...mux :=
0430: 67 6f 66 69 67 75 72 65 2e 4d 75 78 7b 0a 09 09 gofigure.Mux{...
0440: 2f 2f 20 43 61 74 63 68 61 6c 6c 20 70 61 74 68 // Catchall path
0450: 20 22 22 2e 0a 09 09 2f 2f 0a 09 09 2f 2f 20 57 ""....//...// W
0460: 65 20 64 6f 6e 27 74 20 6b 6e 6f 77 20 77 68 65 e don't know whe
0470: 74 68 65 72 20 74 6f 20 65 78 70 65 63 74 20 6f ther to expect o
0480: 62 6a 65 63 74 2c 20 61 72 72 61 79 2c 20 6f 72 bject, array, or
0490: 20 73 63 61 6c 61 72 3b 20 73 6f 20 77 65 0a 09 scalar; so we..
04a0: 09 2f 2f 20 74 61 6b 65 20 61 20 64 65 63 6f 64 .// take a decod
04b0: 65 72 20 61 6e 64 20 22 73 74 72 65 61 6d 22 20 er and "stream"
04c0: 65 61 63 68 20 70 61 72 74 2e 20 54 68 65 20 6f each part. The o
04d0: 75 74 70 75 74 20 6d 61 79 20 6e 6f 74 20 62 65 utput may not be
04e0: 0a 09 09 2f 2f 20 77 68 61 74 20 61 20 75 73 65 ...// what a use
04f0: 72 20 65 78 70 65 63 74 73 2c 20 61 6e 64 20 77 r expects, and w
0500: 69 6c 6c 20 62 65 20 64 69 66 66 65 72 65 6e 74 ill be different
0510: 20 66 72 6f 6d 20 77 68 61 74 20 6f 74 68 65 72 from what other
0520: 0a 09 09 2f 2f 20 68 61 6e 64 6c 65 72 20 74 79 ...// handler ty
0530: 70 65 73 20 64 65 63 6f 64 65 2e 0a 09 09 22 22 pes decode....""
0540: 3a 20 67 6f 66 69 67 75 72 65 2e 53 74 72 65 61 : gofigure.Strea
0550: 6d 28 66 75 6e 63 28 72 20 67 6f 66 69 67 75 72 m(func(r gofigur
0560: 65 2e 52 6f 75 74 65 2c 20 64 65 63 20 2a 6a 73 e.Route, dec *js
0570: 6f 6e 2e 44 65 63 6f 64 65 72 29 20 65 72 72 6f on.Decoder) erro
0580: 72 20 7b 0a 09 09 09 66 6d 74 2e 50 72 69 6e 74 r {....fmt.Print
0590: 6c 6e 28 72 29 0a 09 09 09 76 61 72 20 72 61 77 ln(r)....var raw
05a0: 20 6a 73 6f 6e 2e 52 61 77 4d 65 73 73 61 67 65 json.RawMessage
05b0: 0a 09 09 09 66 6f 72 20 7b 0a 09 09 09 09 65 72 ....for {.....er
05c0: 72 20 3a 3d 20 64 65 63 2e 44 65 63 6f 64 65 28 r := dec.Decode(
05d0: 26 72 61 77 29 0a 09 09 09 09 69 66 20 65 72 72 &raw).....if err
05e0: 6f 72 73 2e 49 73 28 65 72 72 2c 20 69 6f 2e 45 ors.Is(err, io.E
05f0: 4f 46 29 20 7b 0a 09 09 09 09 09 62 72 65 61 6b OF) {......break
0600: 0a 09 09 09 09 7d 0a 09 09 09 09 63 68 65 63 6b .....}.....check
0610: 28 65 72 72 29 0a 09 09 09 09 66 6d 74 2e 50 72 (err).....fmt.Pr
0620: 69 6e 74 66 28 22 5c 74 25 73 5c 6e 22 2c 20 73 intf("\t%s\n", s
0630: 74 72 69 6e 67 28 72 61 77 29 29 0a 09 09 09 7d tring(raw))....}
0640: 0a 09 09 09 72 65 74 75 72 6e 20 6e 69 6c 0a 09 ....return nil..
0650: 09 7d 29 2c 0a 09 7d 0a 09 0a 09 61 72 67 20 3a .}),..}....arg :
0660: 3d 20 66 6c 61 67 2e 41 72 67 73 28 29 0a 09 2f = flag.Args()../
0670: 2f 20 54 4f 44 4f 28 64 6e 63 29 3a 20 69 66 20 / TODO(dnc): if
0680: 6e 6f 20 61 72 67 73 2c 20 73 74 64 69 6e 0a 0a no args, stdin..
0690: 09 66 6f 72 20 69 20 3a 3d 20 72 61 6e 67 65 20 .for i := range
06a0: 61 72 67 20 7b 0a 09 09 65 72 72 20 3a 3d 20 6d arg {...err := m
06b0: 75 78 2e 50 61 72 73 65 28 61 72 67 5b 69 5d 29 ux.Parse(arg[i])
06c0: 0a 09 09 63 68 65 63 6b 28 65 72 72 29 0a 09 7d ...check(err)..}
06d0: 0a 7d 0a 0a 66 75 6e 63 20 63 68 65 63 6b 28 65 .}..func check(e
06e0: 72 72 20 65 72 72 6f 72 29 20 7b 0a 09 09 69 66 rr error) {...if
06f0: 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 err != nil {...
0700: 09 66 6d 74 2e 50 72 69 6e 74 6c 6e 28 65 72 72 .fmt.Println(err
0710: 29 0a 09 09 09 6f 73 2e 45 78 69 74 28 31 29 0a )....os.Exit(1).
0720: 09 09 7d 0a 7d 0a ..}.}.