Skip to content

Commit

Permalink
prompt hostname and log type if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvervaeke committed Oct 29, 2024
1 parent e6fadcb commit 854f977
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
61 changes: 51 additions & 10 deletions internal/cli/deployments/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/deployments/options"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
Expand All @@ -42,8 +43,8 @@ type DownloadOpts struct {
cli.DownloaderOpts
options.DeploymentOpts
downloadStore store.LogsDownloader
host string
name string
Host string
Name string
start int64
end int64
}
Expand All @@ -70,6 +71,9 @@ func (opts *DownloadOpts) Run(ctx context.Context) error {
}

if opts.IsAtlasDeploymentType() {
if err := opts.promptMissingAtlasFlags(); err != nil {
return err
}
if err := opts.validateAtlasFlags(); err != nil {
return err
}
Expand Down Expand Up @@ -144,10 +148,10 @@ func (opts *DownloadOpts) downloadLogFile() error {
}

func (opts *DownloadOpts) newHostLogsParams() *admin.GetHostLogsApiParams {
fileBaseName := strings.TrimSuffix(opts.name, filepath.Ext(opts.name))
fileBaseName := strings.TrimSuffix(opts.Name, filepath.Ext(opts.Name))
params := &admin.GetHostLogsApiParams{
GroupId: opts.ConfigProjectID(),
HostName: opts.host,
HostName: opts.Host,
LogName: fileBaseName,
}
if opts.start > 0 {
Expand All @@ -171,17 +175,54 @@ func (opts *DownloadOpts) RunLocal(ctx context.Context) error {
return opts.Print(strings.Join(logs, "\n"))
}

func (opts *DownloadOpts) promptMissingAtlasFlags() error {
questions := make([]*survey.Question, 0)

if opts.Host == "" {
questions = append(questions, &survey.Question{
Name: "host",
Prompt: &survey.Input{
Message: "Hostname:",
},
Validate: survey.Required,
})
}

if opts.Name == "" {
questions = append(questions, &survey.Question{
Name: "name",
Prompt: &survey.Select{
Message: "Choose a log:",
Options: []string{
"mongodb.gz",
"mongos.gz",
"mongosqld.gz",
"mongodb-audit-log.gz",
"mongos-audit-log.gz",
},
},
Validate: survey.Required,
})
}

if len(questions) > 0 {
return survey.Ask(questions, opts)
}

return nil
}

func (opts *DownloadOpts) validateAtlasFlags() error {
if opts.host == "" {
if opts.Host == "" {
return errors.New("missing --hostname flag")
}
if opts.name == "" {
if opts.Name == "" {
return errors.New("missing --name flag")
}

validNameFlags := []string{"mongodb.gz", "mongos.gz", "mongosqld.gz", "mongodb-audit-log.gz", "mongos-audit-log.gz"}
if !search.StringInSliceFold(validNameFlags, opts.name) {
return fmt.Errorf("invalid --name flag: %s", opts.name)
if !search.StringInSliceFold(validNameFlags, opts.Name) {
return fmt.Errorf("invalid --name flag: %s", opts.Name)
}
return nil
}
Expand Down Expand Up @@ -227,8 +268,8 @@ func LogsBuilder() *cobra.Command {
cmd.Flags().Int64Var(&opts.end, flag.End, 0, usage.LogEnd)
cmd.Flags().BoolVar(&opts.Force, flag.Force, false, usage.ForceFile)
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
cmd.Flags().StringVar(&opts.host, flag.Hostname, "", usage.LogHostName)
cmd.Flags().StringVar(&opts.name, flag.Name, "", usage.LogName)
cmd.Flags().StringVar(&opts.Host, flag.Hostname, "", usage.LogHostName)
cmd.Flags().StringVar(&opts.Name, flag.Name, "", usage.LogName)

return cmd
}
4 changes: 2 additions & 2 deletions internal/cli/deployments/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func TestLogs_RunAtlas(t *testing.T) {
},
DeploymentOpts: *deploymentTest.Opts,
downloadStore: mockStore,
host: "test",
name: "mongodb.gz",
Host: "test",
Name: "mongodb.gz",
}

downloadOpts.Fs = afero.NewMemMapFs()
Expand Down

0 comments on commit 854f977

Please sign in to comment.