-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix.py
More file actions
63 lines (49 loc) · 2.21 KB
/
fix.py
File metadata and controls
63 lines (49 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
import os
# Path to the directory with Go files
directory = "."
# Updated regex pattern to find incorrect Msgf statements
pattern = re.compile(r'log\.Info\(\)\.Msgf\((\"[^\"]*\")((?:,\s*(?:\"[^\"]*\"|\w+))*)\)')
# Function to embed string literals and handle existing %s or %v placeholders
def replacer(match):
message = match.group(1)
args = match.group(2).strip().split(',')[1:] # Extract arguments
formatted_message = message[:-1] # Remove the closing quote for now
new_args = []
placeholders = []
# Count the number of existing %s and %v in the message
existing_placeholders = re.findall(r'%[sv]', formatted_message)
for arg in args:
arg = arg.strip()
if re.match(r'\"[^\"]*\"', arg): # If the argument is a string literal
formatted_message += " " + arg[1:-1] # Embed the string literal without quotes
else: # If the argument is a variable
# Determine which placeholder to use
if "str" in arg.lower(): # Customize this check as needed
placeholders.append('%s')
else:
placeholders.append('%v')
new_args.append(arg)
# Insert additional placeholders, only if we haven't exceeded existing placeholders
for _ in range(len(placeholders)):
if len(existing_placeholders) > 0:
existing_placeholders.pop(0) # Consume an existing placeholder
else:
formatted_message += " " + placeholders.pop(0)
# Close the format string and reassemble the log statement
formatted_message += '",'
# Add any remaining arguments
return f'log.Info().Msgf({formatted_message} {", ".join(new_args)})'
# Iterate over Go files and apply the regex replacement
for subdir, _, files in os.walk(directory):
for file in files:
if file.endswith(".go"):
filepath = os.path.join(subdir, file)
with open(filepath, 'r') as f:
content = f.read()
# Apply the replacement
new_content = pattern.sub(replacer, content)
# Write the modified content back to the file
with open(filepath, 'w') as f:
f.write(new_content)
print("Replacement completed!")