-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State export log ignores some log files #3382
Conversation
internal/captain/command.go
Outdated
@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string { | |||
|
|||
return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render()) | |||
} | |||
|
|||
func (c *Command) LogArgs() { | |||
children, err := c.FindChildren(os.Args[1:]) |
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.
We should use c.Args
. I think for those it already dropped the first entry of os.Args
, but worth verifying.
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.
All of the arg values in the command appear to be the known command arguments and not the os.Args
. We set the args as part of the cobra command but I'm not finding a way to get them back out.
internal/captain/command.go
Outdated
@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string { | |||
|
|||
return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render()) | |||
} | |||
|
|||
func (c *Command) LogArgs() { | |||
children, err := c.FindChildren(os.Args[1:]) |
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.
I'm pretty sure FindChild()
already achieves the desired effect you're looking for. And it passes through cobra, guaranteeing that we get the right command. From there I think you could use command.cobra.CommandPath()
, which should give you the full path.
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're right and it also turns out we have other ways to accomplish this already so I've deleted the FindChildren
function.
internal/runners/export/log.go
Outdated
logging.Error("failed to validate log file: %s", err.Error()) | ||
continue |
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.
Bubble up errors please. This shouldn't ever cause an error, so if it does something is broken and we need to fix it. Logging just obfuscates the breakage.
internal/runners/export/log.go
Outdated
} | ||
defer file.Close() | ||
|
||
regex := regexp.MustCompile(`.*\] Args: \[(.*?)\], Flags: \[.*?\]`) |
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.
regex := regexp.MustCompile(`.*\] Args: \[(.*?)\], Flags: \[.*?\]`) | |
regex := regexp.MustCompile(`Args: \[(.*?)\], Flags: \[.*?\]`) |
Doesn't seem necessary for this function to have an opinion on what the prefix looks like.
internal/runners/export/log.go
Outdated
} | ||
} | ||
|
||
if strings.Contains(args, "export") && strings.Contains(args, "log") { |
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.
I'd prefer we be a but more explicit with the matches. However unlikely, it's easy enough to guard against something like state exec -- myapp -i file.log --export
or something along those lines.
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.
I made the check sequential. Is there more that you're looking for?
internal/captain/command.go
Outdated
@@ -920,18 +885,15 @@ func childCommands(cmd *Command) string { | |||
} | |||
|
|||
func (c *Command) LogArgs() { | |||
children, err := c.FindChildren(os.Args[1:]) | |||
child, err := c.FindChild(os.Args[1:]) |
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.
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.
For some reason all of my comments were Pending
🤦
internal/captain/command.go
Outdated
func flags() []string { | ||
flags := []string{} | ||
for _, arg := range os.Args { | ||
if strings.HasPrefix(arg, "-") { | ||
flags = append(flags, arg) | ||
} | ||
} | ||
return flags | ||
} |
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 is largely duplicated from the analytics
package. I tried moving things around but ended up with import cycles so opted to put this here.
internal/runners/export/log.go
Outdated
} | ||
} | ||
|
||
if strings.Contains(args, "export") && strings.Contains(args, "log") { |
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.
I made the check sequential. Is there more that you're looking for?
internal/captain/command.go
Outdated
@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string { | |||
|
|||
return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render()) | |||
} | |||
|
|||
func (c *Command) LogArgs() { | |||
children, err := c.FindChildren(os.Args[1:]) |
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.
All of the arg values in the command appear to be the known command arguments and not the os.Args
. We set the args as part of the cobra command but I'm not finding a way to get them back out.
internal/captain/command.go
Outdated
@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string { | |||
|
|||
return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render()) | |||
} | |||
|
|||
func (c *Command) LogArgs() { | |||
children, err := c.FindChildren(os.Args[1:]) |
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're right and it also turns out we have other ways to accomplish this already so I've deleted the FindChildren
function.
Co-authored-by: Nathan Rijksen <nathanr@activestate.com>
@MDrakos can't respond to actual comment cause it's "old" and github UX is awful. Suggest instead passing That said, perhaps it would be better to just log args internally when the command is executed and the args are passed, ie. here: cli/internal/captain/command.go Line 216 in 26129e4
|
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.
Looks good! I think we just need a simple integration test covering this mechanic though, come to think of it. As this feels a little fragile considering how the logic is somewhat implicitly shared between different packages.
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.
Nice work!
One thing to note about this PR is that moving the logging of arguments to the
captain
package means that they won't be at the top of the log file anymore. I still think it makes sense to log the arguments in that package. The added requirement of only logging known commands and flags means that we have to log after command registration so that we can inspect the arguments and omit things we don't recognize.