-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update Go version in go.mod to 1.23 * Adds comments to ExecutableCommand interface and BaseCommand * Refactor CommandArgs initialization with options pattern * Refactor command structure and consolidate types. * Refactor: Centralize command types and update dependencies * Refactor and reorganize internal packages
- Loading branch information
1 parent
d748f61
commit 55e96a4
Showing
35 changed files
with
423 additions
and
268 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/matzefriedrich/cobra-extensions/pkg" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/abstractions" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/commands" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type cryptoCommand struct { | ||
abstractions.BaseCommand | ||
use abstractions.CommandName `flag:"crypt"` | ||
types.BaseCommand | ||
use types.CommandName `flag:"crypt"` | ||
} | ||
|
||
func CreateCryptCommand() *cobra.Command { | ||
instance := &cryptoCommand{ | ||
BaseCommand: abstractions.BaseCommand{}, | ||
BaseCommand: types.BaseCommand{}, | ||
} | ||
return pkg.CreateTypedCommand(instance) | ||
return commands.CreateTypedCommand(instance) | ||
} |
This file contains 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 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 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 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 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,32 @@ | ||
package reflection | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// ArgumentDescriptor represents metadata for an argument, including its index, value, and type kind. | ||
type ArgumentDescriptor struct { | ||
argumentIndex int | ||
value reflect.Value | ||
typeKind reflect.Kind | ||
} | ||
|
||
// ArgumentIndex returns the index of the argument represented by the ArgumentDescriptor. | ||
func (a *ArgumentDescriptor) ArgumentIndex() int { | ||
return a.argumentIndex | ||
} | ||
|
||
// SetString Applies a string argument to the underlying structure. | ||
func (a *ArgumentDescriptor) SetString(value string) { | ||
a.value.SetString(value) | ||
} | ||
|
||
// SetInt64 Applies an int64 argument to the underlying structure. | ||
func (a *ArgumentDescriptor) SetInt64(n int64) { | ||
a.value.SetInt(n) | ||
} | ||
|
||
// SetBool Applies a boolean argument to the underlying structure. | ||
func (a *ArgumentDescriptor) SetBool(b bool) { | ||
a.value.SetBool(b) | ||
} |
This file contains 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,58 @@ | ||
package reflection | ||
|
||
import ( | ||
"github.com/matzefriedrich/cobra-extensions/pkg/types" | ||
"github.com/spf13/cobra" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// ArgumentsDescriptor Stores arguments metadata. | ||
type argumentsDescriptor struct { | ||
minimumArgs int | ||
args []ArgumentDescriptor | ||
} | ||
|
||
var _ types.ArgumentsDescriptor = (*argumentsDescriptor)(nil) | ||
|
||
// BindArguments sets the minimum number of positional arguments required for the given Cobra command. | ||
func (d *argumentsDescriptor) BindArguments(target *cobra.Command) { | ||
target.Args = cobra.MinimumNArgs(d.minimumArgs) | ||
} | ||
|
||
// BindArgumentValues Sets the given set of values to positional argument fields. | ||
func (d *argumentsDescriptor) BindArgumentValues(args ...string) { | ||
for _, a := range d.args { | ||
index := a.argumentIndex | ||
if index < len(args) { | ||
value := args[index] | ||
switch a.typeKind { | ||
case reflect.String: | ||
a.SetString(value) | ||
case reflect.Int64: | ||
n, err := strconv.ParseInt(value, 10, 64) | ||
if err == nil { | ||
a.SetInt64(n) | ||
} | ||
case reflect.Bool: | ||
b, err := strconv.ParseBool(value) | ||
if err == nil { | ||
a.SetBool(b) | ||
} | ||
default: | ||
panic("unsupported type") | ||
} | ||
} | ||
} | ||
} | ||
|
||
// NewArgumentsDescriptorWith Creates a new ArgumentsDescriptor. | ||
func NewArgumentsDescriptorWith(options ...types.ArgumentsDescriptorOption) types.ArgumentsDescriptor { | ||
argumentsDescriptor := &argumentsDescriptor{ | ||
args: make([]ArgumentDescriptor, 0), | ||
} | ||
for _, option := range options { | ||
option(argumentsDescriptor) | ||
} | ||
return argumentsDescriptor | ||
} |
Oops, something went wrong.