@@ -60,22 +60,6 @@ type CreateTopicFlags struct {
60
60
Configs []string
61
61
}
62
62
63
- type CreateTopicConfig struct {
64
- Name string `json:"Name"`
65
- Partitions []struct {
66
- ID int `json:"ID"`
67
- OldestOffset int `json:"oldestOffset"`
68
- NewestOffset int `json:"newestOffset"`
69
- Leader string `json:"Leader"`
70
- Replicas []int `json:"Replicas"`
71
- InSyncReplicas []int `json:"inSyncReplicas"`
72
- } `json:"Partitions"`
73
- Configs []struct {
74
- Name string `json:"Name"`
75
- Value string `json:"Value"`
76
- } `json:"Configs"`
77
- }
78
-
79
63
type AlterTopicFlags struct {
80
64
Partitions int32
81
65
ReplicationFactor int16
@@ -137,25 +121,40 @@ func (operation *Operation) CreateTopics(topics []string, flags CreateTopicFlags
137
121
return errors .Wrap (err , "could not read topic description file" )
138
122
}
139
123
140
- createTopicConfig := CreateTopicConfig {}
124
+ fileTopicConfig := Topic {}
141
125
ext := path .Ext (flags .File )
142
126
var unmarshalErr error
143
127
switch ext {
144
128
case ".yml" , ".yaml" :
145
- unmarshalErr = yaml .Unmarshal (fileContent , & createTopicConfig )
129
+ unmarshalErr = yaml .Unmarshal (fileContent , & fileTopicConfig )
146
130
case ".json" :
147
- unmarshalErr = json .Unmarshal (fileContent , & createTopicConfig )
131
+ unmarshalErr = json .Unmarshal (fileContent , & fileTopicConfig )
148
132
default :
149
133
return errors .Wrapf (err , "unsupported file format '%s'" , ext )
150
134
}
151
135
if unmarshalErr != nil {
152
- return errors .Wrap (err , "could not umarshal config file" )
136
+ return errors .Wrap (err , "could not unmarshal config file" )
137
+ }
138
+
139
+ numPartitions := int32 (len (fileTopicConfig .Partitions ))
140
+ if flags .Partitions == 1 {
141
+ topicDetails .NumPartitions = numPartitions
153
142
}
154
143
155
- topicDetails .NumPartitions = int32 (len (createTopicConfig .Partitions ))
156
- topicDetails .ReplicationFactor = int16 (len (createTopicConfig .Partitions [0 ].Replicas ))
157
- for _ , v := range createTopicConfig .Configs {
158
- topicDetails .ConfigEntries [v .Name ] = & v .Value
144
+ replicationFactors := map [int16 ]struct {}{}
145
+ for _ , partition := range fileTopicConfig .Partitions {
146
+ replicationFactors [int16 (len (partition .Replicas ))] = struct {}{}
147
+ }
148
+ if flags .ReplicationFactor == - 1 && len (replicationFactors ) == 1 {
149
+ topicDetails .ReplicationFactor = int16 (len (fileTopicConfig .Partitions [0 ].Replicas ))
150
+ } else if flags .ReplicationFactor == - 1 && len (replicationFactors ) != 1 {
151
+ output .Warnf ("replication factor from file ignored. partitions have different replicaCounts." )
152
+ }
153
+
154
+ for _ , v := range fileTopicConfig .Configs {
155
+ if _ , ok := topicDetails .ConfigEntries [v .Name ]; ! ok {
156
+ topicDetails .ConfigEntries [v .Name ] = & v .Value
157
+ }
159
158
}
160
159
}
161
160
0 commit comments