-
Notifications
You must be signed in to change notification settings - Fork 5
feat(completion): add autocompletion for IDs, output and runtime #86
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
Open
feloy
wants to merge
5
commits into
kortex-hub:main
Choose a base branch
from
feloy:completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b593598
feat(cmd): add shell completion for workspace IDs
feloy d22f9dd
feat(cmd): add shell completion for --output flag
feloy 2966216
feat(cmd): add shell completion for --runtime flag
feloy 099da30
fix(cmd): prevent autocomplete from creating storage directories
feloy 27a84b4
fix: test
feloy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /********************************************************************** | ||
| * Copyright (C) 2026 Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| **********************************************************************/ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/kortex-hub/kortex-cli/pkg/instances" | ||
| "github.com/kortex-hub/kortex-cli/pkg/runtimesetup" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // stateFilter is a function that determines if an instance with the given state should be included | ||
| type stateFilter func(state string) bool | ||
|
|
||
| // getFilteredWorkspaceIDs retrieves workspace IDs, optionally filtered by state | ||
| func getFilteredWorkspaceIDs(cmd *cobra.Command, filter stateFilter) ([]string, cobra.ShellCompDirective) { | ||
| // Get storage directory from global flag | ||
| storageDir, err := cmd.Flags().GetString("storage") | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
|
|
||
| // Normalize storage path to absolute path | ||
| absStorageDir, err := filepath.Abs(storageDir) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
|
|
||
| // Check if storage directory exists to avoid creating it during tab-completion | ||
| if _, err := os.Stat(absStorageDir); os.IsNotExist(err) { | ||
| // Storage doesn't exist yet, return no suggestions | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
|
|
||
| // Create manager | ||
| manager, err := instances.NewManager(absStorageDir) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
|
|
||
| // List all instances | ||
| instancesList, err := manager.List() | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
|
|
||
| // Extract IDs with optional filtering | ||
| var ids []string | ||
| for _, instance := range instancesList { | ||
| state := instance.GetRuntimeData().State | ||
| // Apply filter if provided, otherwise include all | ||
| if filter == nil || filter(state) { | ||
| ids = append(ids, instance.GetID()) | ||
| } | ||
| } | ||
|
|
||
| return ids, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
|
|
||
| // completeNonRunningWorkspaceID provides completion for non-running workspaces (for start and remove) | ||
| // The args and toComplete parameters are part of Cobra's ValidArgsFunction signature but are unused | ||
| // because Cobra's shell completion framework automatically filters results based on user input. | ||
| func completeNonRunningWorkspaceID(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return getFilteredWorkspaceIDs(cmd, func(state string) bool { | ||
| return state != "running" | ||
| }) | ||
| } | ||
|
|
||
| // completeRunningWorkspaceID provides completion for running workspaces (for stop) | ||
| // The args and toComplete parameters are part of Cobra's ValidArgsFunction signature but are unused | ||
| // because Cobra's shell completion framework automatically filters results based on user input. | ||
| func completeRunningWorkspaceID(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return getFilteredWorkspaceIDs(cmd, func(state string) bool { | ||
| return state == "running" | ||
| }) | ||
| } | ||
|
|
||
| // newOutputFlagCompletion creates a completion function for the --output flag | ||
| // with the given list of valid output formats | ||
| func newOutputFlagCompletion(validFormats []string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return validFormats, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
|
|
||
| // completeRuntimeFlag provides completion for the --runtime flag | ||
| // It lists all available runtimes, excluding the "fake" runtime (used only for testing) | ||
| func completeRuntimeFlag(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| // Get all available runtimes without requiring a manager instance | ||
| // This avoids creating storage directories during tab-completion | ||
| runtimes := runtimesetup.ListAvailable() | ||
|
|
||
| // Filter out "fake" runtime (used only for testing) | ||
| var filteredRuntimes []string | ||
| for _, rt := range runtimes { | ||
| if rt != "fake" { | ||
| filteredRuntimes = append(filteredRuntimes, rt) | ||
| } | ||
| } | ||
|
|
||
| return filteredRuntimes, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.