|
| 1 | +// codemetaedit.go is a command line edit for working with a codemeta.json file. |
| 2 | +// |
| 3 | +// Author: R. S. Doiel <[email protected]> |
| 4 | +// |
| 5 | +// Copyright (c) 2021, Caltech |
| 6 | +// All rights not granted herein are expressly reserved by Caltech. |
| 7 | +// |
| 8 | +// Redistribution and use in source and binary forms, with or without |
| 9 | +// modification, are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// 1. Redistributions of source code must retain the above copyright notice, |
| 12 | +// this list of conditions and the following disclaimer. |
| 13 | +// |
| 14 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +// this list of conditions and the following disclaimer in the documentation |
| 16 | +// and/or other materials provided with the distribution. |
| 17 | +// |
| 18 | +// 3. Neither the name of the copyright holder nor the names of its contributors |
| 19 | +// may be used to endorse or promote products derived from this software without |
| 20 | +// specific prior written permission. |
| 21 | +// |
| 22 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 26 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | +// POSSIBILITY OF SUCH DAMAGE. |
| 33 | +// |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "flag" |
| 38 | + "fmt" |
| 39 | + "os" |
| 40 | + "path" |
| 41 | + |
| 42 | + // Caltech Library Package |
| 43 | + "github.com/caltechlibrary/datatools" |
| 44 | +) |
| 45 | + |
| 46 | +func usage(appName string, exitCode int) { |
| 47 | + out := os.Stderr |
| 48 | + if exitCode == 0 { |
| 49 | + out = os.Stdout |
| 50 | + } |
| 51 | + fmt.Fprintf(out, ` |
| 52 | +USAGE: %s |
| 53 | +
|
| 54 | + %s ACTION [OPTION] [CODEMETA_JSON] |
| 55 | +
|
| 56 | +The tool is for updating a codemeta.json file from the command |
| 57 | +line. It assumings the codemeta.json file is in the current |
| 58 | +directory unless a name and path are provided. There are |
| 59 | +four "actions" that can be performed. |
| 60 | +
|
| 61 | +
|
| 62 | +ACTIONS |
| 63 | +
|
| 64 | + help display help |
| 65 | + show show the value of a field given a json path |
| 66 | + set set the value of a field given a json path |
| 67 | + delete removes a a value given a json path |
| 68 | +
|
| 69 | +EXAMPLE |
| 70 | +
|
| 71 | +Retreive the version number in the codemeta.json file. |
| 72 | +
|
| 73 | + %s show ".version" |
| 74 | +
|
| 75 | +Set the "@id" of the first author in the codemeta.json file. |
| 76 | +
|
| 77 | + %s set '.author[0]["@id"]' 'https://orcid.org/0000-0003-0900-6903' |
| 78 | +
|
| 79 | +Remove the third author from the codemeta.json file. |
| 80 | +
|
| 81 | + %s delete '.author[2]' |
| 82 | +
|
| 83 | +datatools v%s |
| 84 | +`, appName, appName, appName, appName, appName, datatools.Version) |
| 85 | + os.Exit(exitCode) |
| 86 | +} |
| 87 | + |
| 88 | +func main() { |
| 89 | + // command line name and options support |
| 90 | + appName := path.Base(os.Args[0]) |
| 91 | + help, version := false, false |
| 92 | + args := []string{} |
| 93 | + // Setup to parse command line |
| 94 | + flag.BoolVar(&help, "h", false, "display help") |
| 95 | + flag.BoolVar(&help, "help", false, "display help") |
| 96 | + flag.BoolVar(&version, "version", false, "display version") |
| 97 | + flag.Parse() |
| 98 | + |
| 99 | + args = flag.Args() |
| 100 | + |
| 101 | + // Process options and run report |
| 102 | + if help { |
| 103 | + usage(appName, 0) |
| 104 | + } |
| 105 | + if version { |
| 106 | + fmt.Printf("datatools, %s v%s\n", appName, datatools.Version) |
| 107 | + os.Exit(0) |
| 108 | + } |
| 109 | + if len(args) < 1 { |
| 110 | + usage(appName, 1) |
| 111 | + } |
| 112 | + var err error |
| 113 | + action := args[0] |
| 114 | + switch action { |
| 115 | + case "help": |
| 116 | + usage(appName, 0) |
| 117 | + case "show": |
| 118 | + err = datatools.CodemetaShow(args[1:]...) |
| 119 | + case "set": |
| 120 | + err = datatools.CodemetaSet(args[1:]...) |
| 121 | + case "delete": |
| 122 | + err = datatools.CodemetaDelete(args[1:]...) |
| 123 | + default: |
| 124 | + fmt.Fprintf(os.Stderr, "Unsupported action.\n") |
| 125 | + usage(appName, 1) |
| 126 | + } |
| 127 | + if err != nil { |
| 128 | + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) |
| 129 | + os.Exit(1) |
| 130 | + } |
| 131 | +} |
0 commit comments