Skip to content
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

fix(command_parser): help text to use executable command #2878

Merged
merged 1 commit into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions server/events/comment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
}

// Helpfully warn the user if they're using "terraform" instead of "atlantis"
if args[0] == "terraform" {
return CommentParseResult{CommentResponse: DidYouMeanAtlantisComment}
if args[0] == "terraform" && e.ExecutableName != "terraform" {
return CommentParseResult{CommentResponse: fmt.Sprintf(DidYouMeanAtlantisComment, e.ExecutableName)}
}

// Atlantis can be invoked using the name of the VCS host user we're
Expand Down Expand Up @@ -171,7 +171,7 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com

// Need to have a plan, apply, approve_policy or unlock at this point.
if !e.stringInSlice(cmd, []string{command.Plan.String(), command.Apply.String(), command.Unlock.String(), command.ApprovePolicies.String(), command.Version.String(), command.Import.String()}) {
return CommentParseResult{CommentResponse: fmt.Sprintf("```\nError: unknown command %q.\nRun 'atlantis --help' for usage.\n```", cmd)}
return CommentParseResult{CommentResponse: fmt.Sprintf("```\nError: unknown command %q.\nRun '%s --help' for usage.\n```", cmd, e.ExecutableName)}
}

var workspace string
Expand Down Expand Up @@ -238,7 +238,7 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
}
if err != nil {
if cmd == command.Unlock.String() {
return CommentParseResult{CommentResponse: UnlockUsage}
return CommentParseResult{CommentResponse: fmt.Sprintf(UnlockUsage, e.ExecutableName)}
}
return CommentParseResult{CommentResponse: e.errMarkdown(err.Error(), cmd, flagSet)}
}
Expand Down Expand Up @@ -382,9 +382,11 @@ func (e *CommentParser) HelpComment(applyDisabled bool) string {
buf := &bytes.Buffer{}
var tmpl = template.Must(template.New("").Parse(helpCommentTemplate))
if err := tmpl.Execute(buf, struct {
ApplyDisabled bool
ApplyDisabled bool
ExecutableName string
}{
ApplyDisabled: applyDisabled,
ApplyDisabled: applyDisabled,
ExecutableName: e.ExecutableName,
}); err != nil {
return fmt.Sprintf("Failed to render template, this is a bug: %v", err)
}
Expand All @@ -397,18 +399,18 @@ var helpCommentTemplate = "```cmake\n" +
Terraform Pull Request Automation

Usage:
atlantis <command> [options] -- [terraform options]
{{ .ExecutableName }} <command> [options] -- [terraform options]

Examples:
# run plan in the root directory passing the -target flag to terraform
atlantis plan -d . -- -target=resource
{{ .ExecutableName }} plan -d . -- -target=resource
{{- if not .ApplyDisabled }}

# apply all unapplied plans from this pull request
atlantis apply
{{ .ExecutableName }} apply

# apply the plan for the root directory and staging workspace
atlantis apply -d . -w staging
{{ .ExecutableName }} apply -d . -w staging
{{- end }}

Commands:
Expand All @@ -430,18 +432,18 @@ Commands:
Flags:
-h, --help help for atlantis

Use "atlantis [command] --help" for more information about a command.` +
Use "{{ .ExecutableName }} [command] --help" for more information about a command.` +
"\n```"

// DidYouMeanAtlantisComment is the comment we add to the pull request when
// someone runs a command with terraform instead of atlantis.
var DidYouMeanAtlantisComment = "Did you mean to use `atlantis` instead of `terraform`?"
var DidYouMeanAtlantisComment = "Did you mean to use `%s` instead of `terraform`?"

// UnlockUsage is the comment we add to the pull request when someone runs
// `atlantis unlock` with flags.

var UnlockUsage = "`Usage of unlock:`\n\n ```cmake\n" +
`atlantis unlock
`%s unlock

Unlocks the entire PR and discards all plans in this PR.
Arguments or flags are not supported at the moment.
Expand Down
2 changes: 1 addition & 1 deletion server/events/comment_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestParse_DidYouMeanAtlantis(t *testing.T) {
}
for _, c := range comments {
r := commentParser.Parse(c, models.Github)
Assert(t, r.CommentResponse == events.DidYouMeanAtlantisComment,
Assert(t, r.CommentResponse == fmt.Sprintf(events.DidYouMeanAtlantisComment, "atlantis"),
"For comment %q expected CommentResponse==%q but got %q", c, events.DidYouMeanAtlantisComment, r.CommentResponse)
}
}
Expand Down