Skip to content

Commit

Permalink
Add featureflag to force shell completions for windows (#5093)
Browse files Browse the repository at this point in the history
It can be run with

```
MONDOO_FEATURES=ForceShellCompletions cnquery shell local
```
  • Loading branch information
jaym authored Jan 14, 2025
1 parent c0dd0e9 commit 6f73a8a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
19 changes: 13 additions & 6 deletions cli/shell/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package shell

import (
"runtime"

"github.com/c-bata/go-prompt"
"go.mondoo.com/cnquery/v11"
"go.mondoo.com/cnquery/v11/mqlc"
Expand All @@ -14,22 +16,27 @@ var completerSeparator = string([]byte{'.', ' '})

// Completer is an auto-complete helper for the shell
type Completer struct {
schema resources.ResourcesSchema
features cnquery.Features
queryPrefix func() string
schema resources.ResourcesSchema
features cnquery.Features
queryPrefix func() string
forceCompletions bool
}

// NewCompleter creates a new Mondoo completer object
func NewCompleter(schema resources.ResourcesSchema, features cnquery.Features, queryPrefix func() string) *Completer {
return &Completer{
schema: schema,
features: features,
queryPrefix: queryPrefix,
schema: schema,
features: features,
queryPrefix: queryPrefix,
forceCompletions: features.IsActive(cnquery.ForceShellCompletion),
}
}

// CompletePrompt provides suggestions
func (c *Completer) CompletePrompt(doc prompt.Document) []prompt.Suggest {
if runtime.GOOS == "windows" && !c.forceCompletions {
return nil
}
if doc.TextBeforeCursor() == "" {
return []prompt.Suggest{}
}
Expand Down
8 changes: 0 additions & 8 deletions cli/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os/signal"
"path"
"regexp"
"runtime"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -154,13 +153,6 @@ func (s *Shell) RunInteractive(cmd string) {
}

completer := s.completer.CompletePrompt
// NOTE: this is an issue with windows cmd and powershell prompt, since this is not reliable we deactivate the
// autocompletion, see https://github.com/c-bata/go-prompt/issues/209
if runtime.GOOS == "windows" {
completer = func(doc prompt.Document) []prompt.Suggest {
return nil
}
}

p := prompt.New(
s.ExecCmd,
Expand Down
5 changes: 3 additions & 2 deletions feature_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,24 @@ const (
// start: v11.x
// end: tbd (candidate: v12.0)
SerialNumberAsID

// ForceShellCompletion feature flag
// desc: Forces shell completion to be enabled (for windows)
// start: v11.x
ForceShellCompletion
)

// FeaturesValue is a map from feature name to feature flag
var FeaturesValue = map[string]Feature{
MassQueries.String(): MassQueries,
PiperCode.String(): PiperCode,
BoolAssertions.String(): BoolAssertions,
MQLAssetContext.String(): MQLAssetContext,
ErrorsAsFailures.String(): ErrorsAsFailures,
StoreResourcesData.String(): StoreResourcesData,
FineGrainedAssets.String(): FineGrainedAssets,
SerialNumberAsID.String(): SerialNumberAsID,
MassQueries.String(): MassQueries,
PiperCode.String(): PiperCode,
BoolAssertions.String(): BoolAssertions,
MQLAssetContext.String(): MQLAssetContext,
ErrorsAsFailures.String(): ErrorsAsFailures,
StoreResourcesData.String(): StoreResourcesData,
FineGrainedAssets.String(): FineGrainedAssets,
SerialNumberAsID.String(): SerialNumberAsID,
ForceShellCompletion.String(): ForceShellCompletion,
}

// DefaultFeatures are a set of default flags that are active
Expand Down

0 comments on commit 6f73a8a

Please sign in to comment.