github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/core/topics_generator.go (about) 1 /* 2 * Copyright 2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package core 18 19 import ( 20 "bytes" 21 "github.com/projectriff/riff-cli/pkg/options" 22 "text/template" 23 ) 24 25 type Topic struct { 26 ApiVersion string 27 Name string 28 Partitions int 29 } 30 31 //TODO: Flag for number of partitions? 32 func createTopics(opts options.InitOptions) (string, error) { 33 34 var topicTemplate string = ` 35 apiVersion : {{.ApiVersion}} 36 kind: Topic 37 metadata: 38 name: {{.Name}} 39 spec: 40 partitions: {{.Partitions}} 41 ` 42 tmpl, err := template.New("topic").Parse(topicTemplate) 43 if err != nil { 44 return "", err 45 } 46 47 var buffer bytes.Buffer 48 49 input := Topic{ApiVersion: ApiVersion, Name: opts.Input, Partitions: 1} 50 err = tmpl.Execute(&buffer, input) 51 if err != nil { 52 return "", err 53 } 54 if opts.Output != "" { 55 buffer.WriteString("---") 56 output := Topic{ApiVersion: ApiVersion, Name: opts.Output, Partitions: 1} 57 err = tmpl.Execute(&buffer, output) 58 if err != nil { 59 return "", err 60 } 61 } 62 63 return buffer.String(), nil 64 }