github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/to/ruleset.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package to 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/zntrio/harp/v2/pkg/bundle" 25 "github.com/zntrio/harp/v2/pkg/bundle/ruleset" 26 "github.com/zntrio/harp/v2/pkg/sdk/convert" 27 "github.com/zntrio/harp/v2/pkg/tasks" 28 ) 29 30 // RuleSetTask implements RuleSet generation from a bundle. 31 type RuleSetTask struct { 32 ContainerReader tasks.ReaderProvider 33 OutputWriter tasks.WriterProvider 34 } 35 36 // Run the task. 37 func (t *RuleSetTask) Run(ctx context.Context) error { 38 // Create input reader 39 reader, err := t.ContainerReader(ctx) 40 if err != nil { 41 return fmt.Errorf("unable to initialize bundle reader: %w", err) 42 } 43 44 // Load bundle 45 b, err := bundle.FromContainerReader(reader) 46 if err != nil { 47 return fmt.Errorf("unable to load bundle content: %w", err) 48 } 49 50 // Generate ruleset 51 rs, err := ruleset.FromBundle(b) 52 if err != nil { 53 return fmt.Errorf("unable to generate RuleSet from given bundle: %w", err) 54 } 55 56 // Marshal as YAML 57 out, err := convert.PBtoYAML(rs) 58 if err != nil { 59 return fmt.Errorf("unable to marshal patch as YAML: %w", err) 60 } 61 62 // Create output writer 63 writer, err := t.OutputWriter(ctx) 64 if err != nil { 65 return fmt.Errorf("unable to initialize output writer: %w", err) 66 } 67 68 // Write output 69 fmt.Fprintln(writer, string(out)) 70 71 // No error 72 return nil 73 }