Skip to content

Commit 8e915d5

Browse files
committed
explicitly defined params should take precedence
1 parent f71fdeb commit 8e915d5

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

internal/topic/topic-operation.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ type CreateTopicFlags struct {
6060
Configs []string
6161
}
6262

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-
7963
type AlterTopicFlags struct {
8064
Partitions int32
8165
ReplicationFactor int16
@@ -137,25 +121,40 @@ func (operation *Operation) CreateTopics(topics []string, flags CreateTopicFlags
137121
return errors.Wrap(err, "could not read topic description file")
138122
}
139123

140-
createTopicConfig := CreateTopicConfig{}
124+
fileTopicConfig := Topic{}
141125
ext := path.Ext(flags.File)
142126
var unmarshalErr error
143127
switch ext {
144128
case ".yml", ".yaml":
145-
unmarshalErr = yaml.Unmarshal(fileContent, &createTopicConfig)
129+
unmarshalErr = yaml.Unmarshal(fileContent, &fileTopicConfig)
146130
case ".json":
147-
unmarshalErr = json.Unmarshal(fileContent, &createTopicConfig)
131+
unmarshalErr = json.Unmarshal(fileContent, &fileTopicConfig)
148132
default:
149133
return errors.Wrapf(err, "unsupported file format '%s'", ext)
150134
}
151135
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
153142
}
154143

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+
}
159158
}
160159
}
161160

0 commit comments

Comments
 (0)