Skip to content

Commit 9363d49

Browse files
committed
sketching codemetaedit
1 parent 4699c0c commit 9363d49

File tree

4 files changed

+181
-6
lines changed

4 files changed

+181
-6
lines changed

cmd/codemeta2cff/codemeta2cff.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"fmt"
3939
"os"
4040
"path"
41+
"strings"
4142

4243
// Caltech Library Package
4344
"github.com/caltechlibrary/datatools"
@@ -51,20 +52,29 @@ func usage(appName string, exitCode int) {
5152
fmt.Fprintf(out, `
5253
USAGE: %s
5354
54-
%s
55+
%s [CODEMETA_JSON CITATION_CFF]
5556
56-
Reads codemeta.json file and writes CITATION.cff.
57+
Reads codemeta.json file and writes CITATION.cff. By default
58+
it assume both are in the current directory. You can also
59+
provide the name and path to both files.
5760
5861
OPTIONS
5962
6063
-h, -help display help
6164
6265
EXAMPLE
6366
67+
Generating the CITATION.cff from the codemeta.json file the current
68+
working directory.
69+
6470
%s
6571
72+
Specifying the full paths.
73+
74+
%s /opt/local/myproject/codemeta.json /opt/local/myproject/CITATION.cff
75+
6676
datatools v%s
67-
`, appName, appName, appName, datatools.Version)
77+
`, appName, appName, appName, appName, datatools.Version)
6878
os.Exit(exitCode)
6979
}
7080

@@ -89,11 +99,16 @@ func main() {
8999
fmt.Printf("datatools, %s v%s\n", appName, datatools.Version)
90100
os.Exit(0)
91101
}
92-
if len(args) != 0 {
93-
fmt.Printf("This command always reads codemeta.json creating/replacing CITATION.cff\n\n")
102+
codemeta, citation := "codemeta.json", "CITATION.cff"
103+
if len(args) == 2 {
104+
codemeta, citation = args[0], args[1]
105+
} else if len(args) == 1 {
106+
codemeta = args[0]
107+
} else if len(args) != 0 {
108+
fmt.Fprintf(os.Stderr, "Unexpected parameters: %q\n", strings.Join(os.Args, " "))
94109
usage(appName, 1)
95110
}
96-
err := datatools.CodemetaToCitationCff("codemeta.json", "CITATION.cff")
111+
err := datatools.CodemetaToCitationCff(codemeta, citation)
97112
if err != nil {
98113
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
99114
os.Exit(1)

cmd/codemetaedit/codemetaedit.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

codemeta/codemeta.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,20 @@ date-released: %s`, dt))...)
105105
src = append(src, []byte("\n")...)
106106
return src, nil
107107
}
108+
109+
// Show returns a the value as a string for the json path described by
110+
// dataPaths. If dataPath is "." return the whole object as a string.
111+
// if the dataPath is not valid return an empty string.
112+
func (cm *Codemeta) Show(dataPath string) (string, error) {
113+
return "", fmt.Errorf("Show() not implemented.")
114+
}
115+
116+
// Set sets the value in the codemeta.json file for a provided json path.
117+
func (cm *Codemeta) Set(dataPath string, value string) error {
118+
return fmt.Errorf("Set() not implemented.")
119+
}
120+
121+
// Delete removes the value in the codemeta.json file for a provided json path.
122+
func (cm *Codemeta) Delete(dataPath string) error {
123+
return fmt.Errorf("Delete() not implemented.")
124+
}

codemeta/codemeta_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ import (
77
func TestCodemeta(t *testing.T) {
88
t.Errorf("TestCodemeta() not implemented.")
99
}
10+
11+
func TestCodemetaShow(t *testing.T) {
12+
t.Errorf("TestCodemetaShow() not implemented.")
13+
}
14+
15+
func TestCodemetaSet(t *testing.T) {
16+
t.Errorf("TestCodemetaSet() not implemented.")
17+
}
18+
19+
func TestCodemetaDelete(t *testing.T) {
20+
t.Errorf("TestCodemetaDelete() not implemented.")
21+
}

0 commit comments

Comments
 (0)