-
Notifications
You must be signed in to change notification settings - Fork 7
prevented grimServerID from being over 15 chars #62
Conversation
grimServerID := config.grimServerID | ||
if len(grimServerID) > 15 { | ||
grimServerID = fmt.Sprintf("%s", grimServerID[:15]) | ||
logger.Printf("grimServerID shouldn't be over 15 characters and has been truncated\nPlease update your config.json file to have a shorter grimServerID\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grimServerID is the name of the in memory field. In the config it is actually GrimServerID. Also, note it is possible to get the GrimServerID set by the "defaults" which is hte grim queue name, so they might not even have a GrimServerID. Don't know what to do about that, maybe add to the error message?
Also note, you haven't truncated the grimServerID globally, only for the first message. So the next time they do a notify (like 3 lines later) it will trigger again.
So, you should move this code to the config file validation/load code. That will cause it to trigger on startup, not on the first notify message.
return err | ||
} | ||
|
||
err = sendMessageToRoom(config.hipChatToken, config.hipChatRoom, config.grimServerID, message, color) | ||
grimServerID := config.grimServerID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grimServerID is the name of the in memory field. In the config it is actually GrimServerID. Also, note it is possible to get the GrimServerID set by the "defaults" which is hte grim queue name, so they might not even have a GrimServerID. Don't know what to do about that, maybe add to the error message?
Also note, you haven't truncated the grimServerID globally, only for the first message. So the next time they do a notify (like 3 lines later) it will trigger again.
So, you should move this code to the config file validation/load code. That will cause it to trigger on startup, not on the first notify message.
7a99f98
to
f3aa807
Compare
@@ -19,6 +19,7 @@ var ( | |||
defaultResultRoot = "/var/log/grim" | |||
defaultWorkspaceRoot = "/var/tmp/grim" | |||
configFileName = "config.json" | |||
truncatedGrimServerID = "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You never use this (and you shouldn't as it is a global variable).
What you've done is:
But it isn't actually being used anywhere. So the hipchat notification will still see and use the longer than 15 character name. What you should do:
One option might be to add a field to the effective config struct that is "hipchatGrimServerId" and do the truncation into that struct. Then for logging on startup right after loading you can check to see if grimServerID != hipchatGrimServerId and log in that case. Then in the notify function you should use the truncated value. |
I made a slight change to the implementation from what @kklipsch said I should do |
var buf bytes.Buffer | ||
logger := log.New(&buf, "", log.Lshortfile) | ||
|
||
tempDir, _ := ioutil.TempDir("", "TestTruncatedGrimServerID") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no err ?
return effectiveConfig{ | ||
grimQueueName: firstNonEmptyStringPtr(global.GrimQueueName, &defaultGrimQueueName), | ||
resultRoot: firstNonEmptyStringPtr(global.ResultRoot, &defaultResultRoot), | ||
workspaceRoot: firstNonEmptyStringPtr(global.WorkspaceRoot, &defaultWorkspaceRoot), | ||
grimServerID: firstNonEmptyStringPtr(global.GrimServerID, global.GrimQueueName, &defaultGrimQueueName), | ||
grimServerID: truncatedServerID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My own personal preference is to leave the config as it was created by the user. And to store the truncated in a new value. That way there is no confusion about what the user entered.
That said it is a nitpick and you don't have to change it.
Logger now knows which parameter in the config.json file was too large (GrimServerID and GrimQueueName) and uses it in the logged message |
@@ -218,3 +222,9 @@ func (i *Instance) checkGrimQueue() error { | |||
|
|||
return nil | |||
} | |||
|
|||
func BuildTruncatedMessage(truncateID string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function doesnt need to be exported. If it is exported it needs to be commented. golint would tell you this so I suspect you are not running golint? (make check will do this for you)
5ef9767
to
cb90f40
Compare
truncateID = "GrimQueueName" | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I think I'd like to see this pulled out into a function called performCorrections
(or something to that effect) that takes an effectiveConfig
and returns (effectiveConfig, []error)
. That way the caller has the before/after and the messages that it can log or throw away.
Don't worry about it unless @kklipsch agrees though. This implementation is solid as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever we get an effectiveConfig, we are reading it from the file. If we go with this approach, we would have to call performCorrections every time we use an effectiveConfig.
prevented grimServerID from being over 15 chars
Truncates the grimServerID and logs that it was truncated and how to fix it