|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "strings" |
6 | | - |
7 | 4 | "github.com/cacoco/codemetagenerator/internal/utils" |
8 | | - "github.com/ohler55/ojg/oj" |
9 | 5 | "github.com/spf13/cobra" |
10 | 6 | ) |
11 | 7 |
|
12 | | -func validateLicenseId(basedir string) func(string) error { |
13 | | - return func(id string) error { |
14 | | - supportedLicenses := SupportedLicenses.getSupportedLicenses() |
15 | | - if supportedLicenses == nil { |
16 | | - return fmt.Errorf("SPDX licenses have not be downloaded, please run `codemeta licenses refresh` to download the SPDX licenses") |
17 | | - } |
18 | | - for _, license := range supportedLicenses { |
19 | | - if license == id { |
20 | | - return nil |
21 | | - } |
22 | | - } |
23 | | - return fmt.Errorf("invalid SPDX license ID: " + id + ", see: https://spdx.org/licenses/ for a list of valid values") |
24 | | - } |
25 | | -} |
26 | | - |
27 | | -func getLicenseReference(basedir string, id string) (*string, error) { |
28 | | - bytes, err := utils.LoadFile(utils.GetLicensesFilePath(basedir)) |
29 | | - if err != nil { |
30 | | - return nil, err |
31 | | - } |
32 | | - var licenses map[string]string |
33 | | - err = oj.Unmarshal(bytes, &licenses) |
34 | | - |
35 | | - if err != nil { |
36 | | - return nil, err |
37 | | - } |
38 | | - |
39 | | - reference, ok := licenses[id] |
40 | | - if !ok { |
41 | | - return nil, fmt.Errorf("Invalid license ID: " + id) |
| 8 | +func licenses(writer utils.Writer) ([]string, error) { |
| 9 | + supportedLicenses := SupportedLicenses.getSupportedLicenses() |
| 10 | + // list licenses |
| 11 | + var list []string = make([]string, 0) |
| 12 | + for _, license := range supportedLicenses { |
| 13 | + list = append(list, license) |
| 14 | + writer.Println(license) |
42 | 15 | } |
43 | | - return &reference, nil |
| 16 | + return list, nil |
44 | 17 | } |
45 | 18 |
|
46 | 19 | // licensesCmd represents the licenses command |
47 | 20 | var licensesCmd = &cobra.Command{ |
48 | 21 | Use: "licenses [refresh]", |
49 | | - Args: cobra.RangeArgs(0, 1), |
50 | | - Short: "List or refresh cached SPDX (https://spdx.org/licenses/) license IDs", |
| 22 | + Args: cobra.NoArgs, |
| 23 | + Short: "List current SPDX IDs (https://spdx.org/licenses/)", |
51 | 24 | Long: ` |
52 | | -Use this command to list license SPDX ids from the https://spdx.org/licenses/. |
| 25 | +Use this command to list currently supported SPDX IDs from https://spdx.org/licenses/. |
53 | 26 |
|
54 | 27 | This is a long list and as such you may want to pipe the output into "more" or |
55 | | -"less" to view it. |
56 | | -
|
57 | | -Pass the "refresh" argument to update the cached list of licenses.`, |
| 28 | +"less" to view it. See: https://spdx.dev/learn/handling-license-info/#why`, |
58 | 29 | RunE: func(cmd *cobra.Command, args []string) error { |
59 | | - writer := &utils.StdoutWriter{} |
60 | | - |
61 | | - if len(args) == 0 { |
62 | | - supportedLicenses := SupportedLicenses.getSupportedLicenses() |
63 | | - // list licenses |
64 | | - for _, license := range supportedLicenses { |
65 | | - fmt.Println(license) |
66 | | - } |
67 | | - return nil |
68 | | - } else if len(args) == 1 && args[0] == "refresh" { |
69 | | - // update licenses file |
70 | | - err := downloadSPDXLicenses(utils.UserHomeDir, utils.MkHttpClient(), true) |
71 | | - if err != nil { |
72 | | - handleErr(writer, err) |
73 | | - return fmt.Errorf("unable to update SPDX licenses file: %s", err.Error()) |
74 | | - } |
75 | | - fmt.Println("✅ Successfully updated SPDX licenses file.") |
76 | | - return nil |
77 | | - } else { |
78 | | - return fmt.Errorf("unrecognized argument(s): '%s'", strings.Join(args, " ")) |
79 | | - } |
| 30 | + _, err := licenses(&utils.StdoutWriter{}) |
| 31 | + return err |
80 | 32 | }, |
81 | 33 | } |
82 | 34 |
|
|
0 commit comments