-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontributor.go
64 lines (56 loc) · 2.23 KB
/
contributor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd
import (
"github.com/cacoco/codemetagenerator/internal/model"
"github.com/cacoco/codemetagenerator/internal/utils"
"github.com/spf13/cobra"
)
func contributor(reader utils.Reader, writer utils.Writer, basedir string) (*map[string]any, error) {
inProgressFilePath := utils.GetInProgressFilePath(basedir)
codemeta, err := utils.Unmarshal(inProgressFilePath)
if err != nil {
handleErr(writer, err)
return nil, writer.Errorf("unable to load the in-progress codemeta.json file for editing. Have you run \"codemetagenerator new\" yet?")
}
mutateMap := *codemeta
currrentValue := mutateMap[model.Contributor]
contributor, err := utils.NewPersonOrOrganizationPrompt(&reader, &writer, "Contributor")
if err != nil {
return nil, err
}
if currrentValue == nil {
// set the new contributor as the value
mutateMap[model.Contributor] = []any{contributor}
} else {
// append new contributor to existing contributors
mutateMap[model.Contributor] = append(currrentValue.([]any), contributor)
}
err = utils.Marshal(inProgressFilePath, mutateMap)
if err != nil {
handleErr(writer, err)
return nil, writer.Errorf("unable to save in-progress codemeta.json file after editing")
} else {
writer.Println("⭐ Successfully updated in-progress codemeta.json file.")
}
return contributor, nil
}
// contributorCmd represents the contributor command
var contributorCmd = &cobra.Command{
Use: "contributor",
Args: cobra.NoArgs,
Short: "Adds a contributor to the in-progress codemeta.json file",
Long: `
Add a single contributor to the in-progres codemeta.json file.
A contributor can be a person or an organization. Prompts for the information
needed to add a contributor and then add it to the in-progress codemeta.json
file. You can add multiple contributors by running this command multiple times.
If you need to remove a contributor, run the "delete" command to remove contributors.
Run the "set" command to edit properties of a contributor.
When complete, run "generate" to generate the resultant 'codemeta.json' file.`,
RunE: func(cmd *cobra.Command, args []string) error {
_, err := contributor(&utils.StdinReader{}, &utils.StdoutWriter{}, utils.UserHomeDir)
return err
},
}
func init() {
addCmd.AddCommand(contributorCmd)
}