-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.go
210 lines (176 loc) · 7.14 KB
/
new.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cmd
import (
"fmt"
"path/filepath"
"github.com/cacoco/codemetagenerator/internal/model"
"github.com/cacoco/codemetagenerator/internal/utils"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
func new(basedir string, reader utils.Reader, writer utils.Writer, inFile string) error {
stdin := reader.Stdin()
stdout := writer.Stdout()
// clean up any previous file
inProgressFilePath := utils.GetInProgressFilePath(basedir)
utils.DeleteFile(inProgressFilePath)
var successMsg string = "⭐ Successfully created new in-progress codemeta.json file."
if inFile != "" {
bytes, err := utils.LoadFile(inFile)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to read input file %s", inFile)
}
err = utils.WriteFile(inProgressFilePath, bytes)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to write in-progress codemeta.json file")
}
fileBase := filepath.Base(inFile)
successMsg = fmt.Sprintf("⭐ Successfully loaded '%s' as new in-progress codemeta.json file.", fileBase)
} else {
var result = make(map[string]any)
identifier, err := utils.MkPrompt(&stdin, &stdout, "Enter a unique identifier for your software source code", utils.Nop)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to create new identifier")
}
name, err := utils.MkPrompt(&stdin, &stdout, "Enter a name for your software source code", utils.Nop)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to create new name")
}
description, err := utils.MkPrompt(&stdin, &stdout, "Enter a description for your software source code", utils.Nop)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to create new description")
}
developmentStatusOptions := []model.MenuOption{
{Name: "Abandoned", Type: "abandoned"},
{Name: "Active", Type: "active"},
{Name: "Concept", Type: "concept"},
{Name: "Inactive", Type: "inactive"},
{Name: "Moved", Type: "moved"},
{Name: "Suspended", Type: "suspended"},
{Name: "Unsupported", Type: "unsupported"},
{Name: "WIP", Type: "wip"},
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "➞ {{ .Name | cyan }}",
Inactive: " {{ .Name | cyan }}",
Selected: `{{ "Select a development status (see: https://www.repostatus.org/):" | faint}} {{ .Name | faint }}`,
Details: `--------- Status ----------
{{ "Name:" | faint }} {{ .Name }}`,
}
prompt := promptui.Select{
Label: "Select a development status from the list below (see: https://www.repostatus.org/)",
Items: developmentStatusOptions,
Templates: templates,
Size: 8,
Searcher: nil,
Stdin: reader.Stdin(),
Stdout: writer.Stdout(),
}
i, _, err := prompt.Run()
if err != nil {
return err
}
developmentStatus := developmentStatusOptions[i].Name
codeRepository, err := utils.MkPrompt(&stdin, &stdout, "Enter the URL of the code repository for the project", utils.ValidUrl)
if err != nil {
return err
}
programmingLanguageName, err := utils.MkPrompt(&stdin, &stdout, "Enter the name of the programming language of the project", utils.Nop)
if err != nil {
return err
}
programmingLanguageURL, err := utils.MkPrompt(&stdin, &stdout, "Enter the URL of the programming language of the project", utils.ValidUrl)
if err != nil {
return err
}
programmingLanguage := model.NewProgrammingLanguage(programmingLanguageName, programmingLanguageURL)
runtimePlatform, err := utils.MkPrompt(&stdin, &stdout, "Enter the name of the runtime platform of the project", utils.Nop)
if err != nil {
return err
}
version, err := utils.MkPrompt(&stdin, &stdout, "Enter the version of the project", utils.Nop)
if err != nil {
return err
}
validateFn := validateLicenseId(writer, basedir)
license, err := utils.MkPrompt(&stdin, &stdout, "Enter the SPDX license ID for the project (see: https://spdx.org/licenses/)", validateFn)
if err != nil {
return err
}
var licenseDetailsUrl string
if (*license) != "" {
referenceUrl, err := getLicenseReference(writer, basedir, *license)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to create new license details URL")
}
licenseDetailsUrl = *referenceUrl
}
readme, err := utils.MkPrompt(&stdin, &stdout, "Enter the URL of the README file for the project", utils.ValidUrl)
if err != nil {
return err
}
maintainer, err := utils.NewPersonOrOrganizationPrompt(&reader, &writer, "Maintainer")
if err != nil {
return err
}
result[model.Identifier] = identifier
result[model.Name] = name
result[model.Description] = description
result[model.Version] = version
result[model.Maintainer] = maintainer
result[model.ProgrammingLanguage] = programmingLanguage
result[model.DevelopmentStatus] = developmentStatus
result[model.License] = licenseDetailsUrl
result[model.RuntimePlatform] = runtimePlatform
result[model.CodeRepository] = codeRepository
result[model.Readme] = readme
codemeta := *model.NewCodemeta(&result)
err = utils.Marshal(inProgressFilePath, codemeta)
if err != nil {
handleErr(writer, err)
return writer.Errorf("unable to save in-progress codemeta.json file after editing")
}
}
writer.Println(successMsg)
writer.Println("👇 You can now add authors, contributors, keywords, and other fields to the in-progress codemeta.json file.")
writer.Println("➡️ To add/remove authors, contributors or keywords, run the following commands:")
writer.Println("\tcodemetagenerator add author")
writer.Println("\tcodemetagenerator remove authors")
writer.Println("\tcodemetagenerator add contributor")
writer.Println("\tcodemetagenerator remove contributors")
writer.Println("\tcodemetagenerator add keyword")
writer.Println("\tcodemetagenerator remove keywords")
writer.Println("↔️ To edit any key in the in-progress codemeta.json file, run the following command:")
writer.Println("\tcodemetagenerator edit key.subkey newValue")
writer.Println("✅ To generate the resultant 'codemeta.json' file, run the following command:")
writer.Println("\tcodemetagenerator generate [-o|--output] <output file path>")
return nil
}
var inputFile string
// newCmd represents the new command
var newCmd = &cobra.Command{
Use: "new",
Args: cobra.NoArgs,
Short: "Start a new codemeta.json file for editing. When complete, run \"codemetagenerator generate\" to generate the resultant 'codemeta.json' file",
Long: `
This command starts a new 'codemeta.json' file with basic information about your
software source code project.
It is expected that you will also add authors, contributors, keywords, and other
fields to the in-progress codemeta.json, then run:
codemetagenerator generate
to generate the resultant 'codemeta.json' file, optionally selecting the file destination.`,
RunE: func(cmd *cobra.Command, args []string) error {
return new(utils.UserHomeDir, &utils.StdinReader{}, &utils.StdoutWriter{}, inputFile)
},
}
func init() {
rootCmd.AddCommand(newCmd)
utils.MkHomeDir(utils.UserHomeDir)
newCmd.Flags().StringVarP(&inputFile, "input", "i", "", "path to an input 'codemeta.json' file. If not specified, a new file will be started.")
}