-
Notifications
You must be signed in to change notification settings - Fork 2
feat: init command #23
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,130 @@ | ||
| /********************************************************************** | ||
| * 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 ( | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/kortex-hub/kortex-cli/pkg/instances" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // initCmd contains the configuration for the init command | ||
| type initCmd struct { | ||
| sourcesDir string | ||
| workspaceConfigDir string | ||
| absSourcesDir string | ||
| absConfigDir string | ||
| manager instances.Manager | ||
| verbose bool | ||
| } | ||
|
|
||
| // preRun validates the parameters and flags | ||
| func (i *initCmd) preRun(cmd *cobra.Command, args []string) error { | ||
| // Get storage directory from global flag | ||
| storageDir, err := cmd.Flags().GetString("storage") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read --storage flag: %w", err) | ||
| } | ||
|
|
||
| // Create manager | ||
| manager, err := instances.NewManager(storageDir) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create manager: %w", err) | ||
| } | ||
| i.manager = manager | ||
|
|
||
| // Get sources directory (default to current directory) | ||
| i.sourcesDir = "." | ||
| if len(args) > 0 { | ||
| i.sourcesDir = args[0] | ||
| } | ||
|
|
||
| // Convert to absolute path for clarity | ||
| absSourcesDir, err := filepath.Abs(i.sourcesDir) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to resolve sources directory path: %w", err) | ||
| } | ||
| i.absSourcesDir = absSourcesDir | ||
|
|
||
| // If workspace-configuration flag was not explicitly set, default to .kortex/ inside sources directory | ||
| if !cmd.Flags().Changed("workspace-configuration") { | ||
| i.workspaceConfigDir = filepath.Join(i.sourcesDir, ".kortex") | ||
| } | ||
|
|
||
| // Convert workspace config to absolute path | ||
| absConfigDir, err := filepath.Abs(i.workspaceConfigDir) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to resolve workspace configuration directory path: %w", err) | ||
| } | ||
| i.absConfigDir = absConfigDir | ||
|
|
||
| return nil | ||
feloy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // run executes the init command logic | ||
| func (i *initCmd) run(cmd *cobra.Command, args []string) error { | ||
| // Create a new instance | ||
| instance, err := instances.NewInstance(i.absSourcesDir, i.absConfigDir) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create instance: %w", err) | ||
| } | ||
|
|
||
| // Add the instance to the manager | ||
| addedInstance, err := i.manager.Add(instance) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to add instance: %w", err) | ||
| } | ||
|
|
||
| if i.verbose { | ||
| cmd.Printf("Registered workspace:\n") | ||
| cmd.Printf(" ID: %s\n", addedInstance.GetID()) | ||
| cmd.Printf(" Sources directory: %s\n", addedInstance.GetSourceDir()) | ||
| cmd.Printf(" Configuration directory: %s\n", addedInstance.GetConfigDir()) | ||
| } else { | ||
| cmd.Println(addedInstance.GetID()) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func NewInitCmd() *cobra.Command { | ||
| c := &initCmd{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "init [sources-directory]", | ||
| Short: "Register a new workspace", | ||
| Long: `Register a new workspace with the specified sources directory and configuration location. | ||
|
|
||
| The sources directory defaults to the current directory (.) if not specified. | ||
| The workspace configuration directory defaults to .kortex/ inside the sources directory if not specified.`, | ||
| Args: cobra.MaximumNArgs(1), | ||
| PreRunE: c.preRun, | ||
| RunE: c.run, | ||
| } | ||
|
|
||
| // Add workspace-configuration flag | ||
| cmd.Flags().StringVar(&c.workspaceConfigDir, "workspace-configuration", "", "Directory for workspace configuration (default: <sources-directory>/.kortex)") | ||
|
|
||
| // Add verbose flag | ||
| cmd.Flags().BoolVarP(&c.verbose, "verbose", "v", false, "Show detailed output") | ||
|
|
||
| return cmd | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.