|
1 | 1 | package create
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "os" |
| 4 | + "strings" |
6 | 5 |
|
7 |
| - "github.com/compspec/compspec-go/pkg/types" |
8 |
| - ep "github.com/compspec/compspec-go/plugins/extractors" |
9 |
| - |
10 |
| - p "github.com/compspec/compspec-go/plugins" |
| 6 | + "github.com/compspec/compspec-go/plugins/creators/artifact" |
11 | 7 | )
|
12 | 8 |
|
13 | 9 | // Artifact will create a compatibility artifact based on a request in YAML
|
14 | 10 | // TODO likely want to refactor this into a proper create plugin
|
15 | 11 | func Artifact(specname string, fields []string, saveto string, allowFail bool) error {
|
16 | 12 |
|
17 |
| - // Cut out early if a spec not provided |
18 |
| - if specname == "" { |
19 |
| - return fmt.Errorf("a spec input -i/--input is required") |
20 |
| - } |
21 |
| - request, err := loadRequest(specname) |
22 |
| - if err != nil { |
23 |
| - return err |
24 |
| - } |
25 |
| - |
26 |
| - // Right now we only know about extractors, when we define subfields |
27 |
| - // we can further filter here. |
28 |
| - extractors := request.GetExtractors() |
29 |
| - plugins, err := ep.GetPlugins(extractors) |
30 |
| - if err != nil { |
31 |
| - return err |
32 |
| - } |
33 |
| - |
34 |
| - // Finally, add custom fields and extract metadata |
35 |
| - result, err := plugins.Extract(allowFail) |
36 |
| - if err != nil { |
37 |
| - return err |
| 13 | + // This is janky, oh well |
| 14 | + allowFailFlag := "false" |
| 15 | + if allowFail { |
| 16 | + allowFailFlag = "true" |
38 | 17 | }
|
39 | 18 |
|
40 |
| - // Update with custom fields (either new or overwrite) |
41 |
| - result.AddCustomFields(fields) |
42 |
| - |
43 |
| - // The compspec returned is the populated Compatibility request! |
44 |
| - compspec, err := PopulateExtractors(&result, request) |
| 19 | + // assemble options for node creator |
| 20 | + creator, err := artifact.NewPlugin() |
45 | 21 | if err != nil {
|
46 | 22 | return err
|
47 | 23 | }
|
48 |
| - |
49 |
| - output, err := compspec.ToJson() |
50 |
| - if err != nil { |
51 |
| - return err |
| 24 | + options := map[string]string{ |
| 25 | + "specname": specname, |
| 26 | + "fields": strings.Join(fields, "||"), |
| 27 | + "saveto": saveto, |
| 28 | + "allowFail": allowFailFlag, |
52 | 29 | }
|
53 |
| - if saveto == "" { |
54 |
| - fmt.Println(string(output)) |
55 |
| - } else { |
56 |
| - err = os.WriteFile(saveto, output, 0644) |
57 |
| - if err != nil { |
58 |
| - return err |
59 |
| - } |
60 |
| - } |
61 |
| - return nil |
62 |
| -} |
63 |
| - |
64 |
| -// LoadExtractors loads a compatibility result into a compatibility request |
65 |
| -// After this we can save the populated thing into an artifact (json DUMP) |
66 |
| -func PopulateExtractors(result *ep.Result, request *types.CompatibilityRequest) (*types.CompatibilityRequest, error) { |
67 |
| - |
68 |
| - // Every metadata attribute must be known under a schema |
69 |
| - schemas := request.Metadata.Schemas |
70 |
| - if len(schemas) == 0 { |
71 |
| - return nil, fmt.Errorf("the request must have one or more schemas") |
72 |
| - } |
73 |
| - for i, compat := range request.Compatibilities { |
74 |
| - |
75 |
| - // The compatibility section name is a schema, and must be defined |
76 |
| - url, ok := schemas[compat.Name] |
77 |
| - if !ok { |
78 |
| - return nil, fmt.Errorf("%s is missing a schema", compat.Name) |
79 |
| - } |
80 |
| - if url == "" { |
81 |
| - return nil, fmt.Errorf("%s has an empty schema", compat.Name) |
82 |
| - } |
83 |
| - |
84 |
| - for key, extractorKey := range compat.Attributes { |
85 |
| - |
86 |
| - // Get the extractor, section, and subfield from the extractor lookup key |
87 |
| - f, err := p.ParseField(extractorKey) |
88 |
| - if err != nil { |
89 |
| - fmt.Printf("warning: cannot parse %s: %s, setting to empty\n", key, extractorKey) |
90 |
| - compat.Attributes[key] = "" |
91 |
| - continue |
92 |
| - } |
93 |
| - |
94 |
| - // If we get here, we can parse it and look it up in our result metadata |
95 |
| - extractor, ok := result.Results[f.Extractor] |
96 |
| - if !ok { |
97 |
| - fmt.Printf("warning: extractor %s is unknown, setting to empty\n", f.Extractor) |
98 |
| - compat.Attributes[key] = "" |
99 |
| - continue |
100 |
| - } |
101 |
| - |
102 |
| - // Now get the section |
103 |
| - section, ok := extractor.Sections[f.Section] |
104 |
| - if !ok { |
105 |
| - fmt.Printf("warning: section %s.%s is unknown, setting to empty\n", f.Extractor, f.Section) |
106 |
| - compat.Attributes[key] = "" |
107 |
| - continue |
108 |
| - } |
109 |
| - |
110 |
| - // Now get the value! |
111 |
| - value, ok := section[f.Field] |
112 |
| - if !ok { |
113 |
| - fmt.Printf("warning: field %s.%s.%s is unknown, setting to empty\n", f.Extractor, f.Section, f.Field) |
114 |
| - compat.Attributes[key] = "" |
115 |
| - continue |
116 |
| - } |
117 |
| - |
118 |
| - // If we get here - we found it! Hooray! |
119 |
| - compat.Attributes[key] = value |
120 |
| - } |
121 |
| - |
122 |
| - // Update the compatibiity |
123 |
| - request.Compatibilities[i] = compat |
124 |
| - } |
125 |
| - |
126 |
| - return request, nil |
| 30 | + return creator.Create(options) |
127 | 31 | }
|
0 commit comments