Skip to content

Commit

Permalink
Rename version flag to service-version
Browse files Browse the repository at this point in the history
The `--version` flag is a very common and basically reserved flag.
Renaming the UiPath specific version flag to `--service-version`
which avoids name conflicts and also makes the intent of the flag
more clear.
  • Loading branch information
thschmitt committed Oct 7, 2024
1 parent 2edd6eb commit 85910d5
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 102 deletions.
46 changes: 23 additions & 23 deletions commandline/command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (b CommandBuilder) execute(executionContext executor.ExecutionContext, outp
func (b CommandBuilder) createCategoryCommand(operation parser.Operation) *CommandDefinition {
flags := NewFlagBuilder().
AddHelpFlag().
AddVersionFlag(true).
AddServiceVersionFlag(true).
Build()

return NewCommand(operation.Category.Name, operation.Category.Summary, operation.Category.Description).
Expand Down Expand Up @@ -472,7 +472,7 @@ func (b CommandBuilder) createServiceCommand(definition parser.Definition) *Comm

flags := NewFlagBuilder().
AddHelpFlag().
AddVersionFlag(true).
AddServiceVersionFlag(true).
Build()

return NewCommand(definition.Name, definition.Summary, definition.Description).
Expand Down Expand Up @@ -507,7 +507,7 @@ func (b CommandBuilder) createAutoCompleteEnableCommand() *CommandDefinition {
})
}

func (b CommandBuilder) createAutoCompleteCompleteCommand(version string) *CommandDefinition {
func (b CommandBuilder) createAutoCompleteCompleteCommand(serviceVersion string) *CommandDefinition {
const commandFlagName = "command"

flags := NewFlagBuilder().
Expand All @@ -525,7 +525,7 @@ func (b CommandBuilder) createAutoCompleteCompleteCommand(version string) *Comma
exclude = append(exclude, "--"+flagName)
}
args := strings.Split(commandText, " ")
definitions, err := b.loadAutocompleteDefinitions(args, version)
definitions, err := b.loadAutocompleteDefinitions(args, serviceVersion)
if err != nil {
return err
}
Expand All @@ -541,14 +541,14 @@ func (b CommandBuilder) createAutoCompleteCompleteCommand(version string) *Comma
})
}

func (b CommandBuilder) createAutoCompleteCommand(version string) *CommandDefinition {
func (b CommandBuilder) createAutoCompleteCommand(serviceVersion string) *CommandDefinition {
flags := NewFlagBuilder().
AddHelpFlag().
Build()

subcommands := []*CommandDefinition{
b.createAutoCompleteEnableCommand(),
b.createAutoCompleteCompleteCommand(version),
b.createAutoCompleteCompleteCommand(serviceVersion),
}

return NewCommand("autocomplete", "Autocompletion", "Commands for autocompletion").
Expand Down Expand Up @@ -608,28 +608,28 @@ func (b CommandBuilder) createConfigSetCommand() *CommandDefinition {
})
}

func (b CommandBuilder) loadDefinitions(args []string, version string) ([]parser.Definition, error) {
func (b CommandBuilder) loadDefinitions(args []string, serviceVersion string) ([]parser.Definition, error) {
if len(args) <= 1 || strings.HasPrefix(args[1], "-") {
return b.DefinitionProvider.Index(version)
return b.DefinitionProvider.Index(serviceVersion)
}
if len(args) > 1 && args[1] == "commands" {
return b.loadAllDefinitions(version)
return b.loadAllDefinitions(serviceVersion)
}
definition, err := b.DefinitionProvider.Load(args[1], version)
definition, err := b.DefinitionProvider.Load(args[1], serviceVersion)
if definition == nil {
return nil, err
}
return []parser.Definition{*definition}, err
}

func (b CommandBuilder) loadAllDefinitions(version string) ([]parser.Definition, error) {
all, err := b.DefinitionProvider.Index(version)
func (b CommandBuilder) loadAllDefinitions(serviceVersion string) ([]parser.Definition, error) {
all, err := b.DefinitionProvider.Index(serviceVersion)
if err != nil {
return nil, err
}
definitions := []parser.Definition{}
for _, d := range all {
definition, err := b.DefinitionProvider.Load(d.Name, version)
definition, err := b.DefinitionProvider.Load(d.Name, serviceVersion)
if err != nil {
return nil, err
}
Expand All @@ -640,11 +640,11 @@ func (b CommandBuilder) loadAllDefinitions(version string) ([]parser.Definition,
return definitions, nil
}

func (b CommandBuilder) loadAutocompleteDefinitions(args []string, version string) ([]parser.Definition, error) {
func (b CommandBuilder) loadAutocompleteDefinitions(args []string, serviceVersion string) ([]parser.Definition, error) {
if len(args) <= 2 {
return b.DefinitionProvider.Index(version)
return b.DefinitionProvider.Index(serviceVersion)
}
return b.loadDefinitions(args, version)
return b.loadDefinitions(args, serviceVersion)
}

func (b CommandBuilder) createShowCommand(definitions []parser.Definition) *CommandDefinition {
Expand Down Expand Up @@ -706,26 +706,26 @@ func (b CommandBuilder) parseArgument(args []string, name string) string {
return ""
}

func (b CommandBuilder) versionFromProfile(profile string) string {
func (b CommandBuilder) serviceVersionFromProfile(profile string) string {
config := b.ConfigProvider.Config(profile)
if config == nil {
return ""
}
return config.Version
return config.ServiceVersion
}

func (b CommandBuilder) Create(args []string) ([]*CommandDefinition, error) {
version := b.parseArgument(args, FlagNameVersion)
serviceVersion := b.parseArgument(args, FlagNameServiceVersion)
profile := b.parseArgument(args, FlagNameProfile)
if version == "" && profile != "" {
version = b.versionFromProfile(profile)
if serviceVersion == "" && profile != "" {
serviceVersion = b.serviceVersionFromProfile(profile)
}
definitions, err := b.loadDefinitions(args, version)
definitions, err := b.loadDefinitions(args, serviceVersion)
if err != nil {
return nil, err
}
servicesCommands := b.createServiceCommands(definitions)
autocompleteCommand := b.createAutoCompleteCommand(version)
autocompleteCommand := b.createAutoCompleteCommand(serviceVersion)
configCommand := b.createConfigCommand()
inspectCommand := b.createInspectCommand(definitions)
commands := append(servicesCommands, autocompleteCommand, configCommand, inspectCommand)
Expand Down
4 changes: 2 additions & 2 deletions commandline/config_command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (h configCommandHandler) Set(key string, value string, profileName string)

func (h configCommandHandler) setConfigValue(config *config.Config, key string, value string) error {
keyParts := strings.Split(key, ".")
if key == "version" {
config.SetVersion(value)
if key == "serviceVersion" {
config.SetServiceVersion(value)
return nil
} else if key == "organization" {
config.ConfigureOrgTenant(value, "")
Expand Down
10 changes: 5 additions & 5 deletions commandline/definition_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package commandline

// DefinitionData contains the name of the definition file and its data.
type DefinitionData struct {
Name string
Version string
Data []byte
Name string
ServiceVersion string
Data []byte
}

func NewDefinitionData(name string, version string, data []byte) *DefinitionData {
return &DefinitionData{name, version, data}
func NewDefinitionData(name string, serviceVersion string, data []byte) *DefinitionData {
return &DefinitionData{name, serviceVersion, data}
}
36 changes: 18 additions & 18 deletions commandline/definition_file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,52 @@ type DefinitionFileStore struct {

const DefinitionsDirectory = "definitions"

func (s *DefinitionFileStore) Names(version string) ([]string, error) {
func (s *DefinitionFileStore) Names(serviceVersion string) ([]string, error) {
if s.definitions != nil {
names := []string{}
for _, definition := range s.definitions {
if version == definition.Version {
if serviceVersion == definition.ServiceVersion {
names = append(names, definition.Name)
}
}
return names, nil
}

definitionFiles, err := s.discoverDefinitions(version)
definitionFiles, err := s.discoverDefinitions(serviceVersion)
if err != nil {
return nil, err
}
return s.definitionNames(definitionFiles), nil
}

func (s *DefinitionFileStore) Read(name string, version string) (*DefinitionData, error) {
func (s *DefinitionFileStore) Read(name string, serviceVersion string) (*DefinitionData, error) {
if s.definitions != nil {
for _, definition := range s.definitions {
if name == definition.Name && version == definition.Version {
if name == definition.Name && serviceVersion == definition.ServiceVersion {
return &definition, nil
}
}
}

definitionFiles, err := s.discoverDefinitions(version)
definitionFiles, err := s.discoverDefinitions(serviceVersion)
if err != nil {
return nil, err
}

for _, fileName := range definitionFiles {
if name == s.definitionName(fileName) {
data, err := s.readDefinitionData(version, fileName)
data, err := s.readDefinitionData(serviceVersion, fileName)
if err != nil {
return nil, err
}
definition := NewDefinitionData(name, version, data)
definition := NewDefinitionData(name, serviceVersion, data)
return definition, err
}
}
return nil, nil
}

func (s *DefinitionFileStore) discoverDefinitions(version string) ([]string, error) {
func (s *DefinitionFileStore) discoverDefinitions(serviceVersion string) ([]string, error) {
if s.files != nil {
return s.files, nil
}
Expand All @@ -77,13 +77,13 @@ func (s *DefinitionFileStore) discoverDefinitions(version string) ([]string, err
for _, fileName := range embeddedFiles {
definitionFiles[fileName] = fileName
}
directoryFiles := s.discoverDefinitionsDirectory(version)
directoryFiles := s.discoverDefinitionsDirectory(serviceVersion)
for _, fileName := range directoryFiles {
definitionFiles[fileName] = fileName
}

if len(definitionFiles) == 0 {
return nil, fmt.Errorf("Could not find definition files in folder '%s'", s.definitionsPath(version))
return nil, fmt.Errorf("Could not find definition files in folder '%s'", s.definitionsPath(serviceVersion))
}

result := []string{}
Expand All @@ -106,9 +106,9 @@ func (s DefinitionFileStore) discoverDefinitionsEmbedded() []string {
return definitionFiles
}

func (s DefinitionFileStore) discoverDefinitionsDirectory(version string) []string {
func (s DefinitionFileStore) discoverDefinitionsDirectory(serviceVersion string) []string {
definitionFiles := []string{}
definitionsDirectory := s.definitionsPath(version)
definitionsDirectory := s.definitionsPath(serviceVersion)
files, err := os.ReadDir(definitionsDirectory)
if err == nil {
for _, file := range files {
Expand All @@ -121,15 +121,15 @@ func (s DefinitionFileStore) discoverDefinitionsDirectory(version string) []stri
return definitionFiles
}

func (s DefinitionFileStore) definitionsPath(version string) string {
func (s DefinitionFileStore) definitionsPath(serviceVersion string) string {
if s.directory != "" {
return s.directory
}
currentDirectory, err := os.Executable()
if err != nil {
return filepath.Join(DefinitionsDirectory, version)
return filepath.Join(DefinitionsDirectory, serviceVersion)
}
return filepath.Join(filepath.Dir(currentDirectory), DefinitionsDirectory, version)
return filepath.Join(filepath.Dir(currentDirectory), DefinitionsDirectory, serviceVersion)
}

func (s DefinitionFileStore) definitionName(path string) string {
Expand All @@ -144,8 +144,8 @@ func (s DefinitionFileStore) definitionNames(paths []string) []string {
return names
}

func (s DefinitionFileStore) readDefinitionData(version string, fileName string) ([]byte, error) {
definitionsFilePath := filepath.Join(s.definitionsPath(version), fileName)
func (s DefinitionFileStore) readDefinitionData(serviceVersion string, fileName string) ([]byte, error) {
definitionsFilePath := filepath.Join(s.definitionsPath(serviceVersion), fileName)
data, err := os.ReadFile(definitionsFilePath)
if err != nil {
embeddedFilePath := path.Join(DefinitionsDirectory, fileName)
Expand Down
16 changes: 8 additions & 8 deletions commandline/definition_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type DefinitionProvider struct {
commandPlugins []plugin.CommandPlugin
}

func (p DefinitionProvider) Index(version string) ([]parser.Definition, error) {
emptyDefinitions, err := p.loadEmptyDefinitions(version)
func (p DefinitionProvider) Index(serviceVersion string) ([]parser.Definition, error) {
emptyDefinitions, err := p.loadEmptyDefinitions(serviceVersion)
if err != nil {
return nil, err
}
Expand All @@ -44,15 +44,15 @@ func (p DefinitionProvider) Index(version string) ([]parser.Definition, error) {
return result, nil
}

func (p DefinitionProvider) Load(name string, version string) (*parser.Definition, error) {
names, err := p.store.Names(version)
func (p DefinitionProvider) Load(name string, serviceVersion string) (*parser.Definition, error) {
names, err := p.store.Names(serviceVersion)
if err != nil {
return nil, err
}
definitions := []*parser.Definition{}
for _, n := range names {
if p.getServiceName(n) == name {
data, err := p.store.Read(n, version)
data, err := p.store.Read(n, serviceVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,16 +86,16 @@ func (p DefinitionProvider) getServiceName(name string) string {
return name
}

func (p DefinitionProvider) loadEmptyDefinitions(version string) ([]DefinitionData, error) {
names, err := p.store.Names(version)
func (p DefinitionProvider) loadEmptyDefinitions(serviceVersion string) ([]DefinitionData, error) {
names, err := p.store.Names(serviceVersion)
if err != nil {
return nil, err
}
result := []DefinitionData{}
for _, name := range names {
serviceName := p.getServiceName(name)
if len(result) == 0 || result[len(result)-1].Name != serviceName {
result = append(result, *NewDefinitionData(serviceName, version, []byte{}))
result = append(result, *NewDefinitionData(serviceName, serviceVersion, []byte{}))
}
}
return result, nil
Expand Down
4 changes: 2 additions & 2 deletions commandline/definition_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package commandline

// DefinitionStore is used to provide the names and content of definition files.
type DefinitionStore interface {
Names(version string) ([]string, error)
Read(name string, version string) (*DefinitionData, error)
Names(serviceVersion string) ([]string, error)
Read(name string, serviceVersion string) (*DefinitionData, error)
}
16 changes: 8 additions & 8 deletions commandline/flag_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const FlagNameWait = "wait"
const FlagNameWaitTimeout = "wait-timeout"
const FlagNameFile = "file"
const FlagNameIdentityUri = "identity-uri"
const FlagNameVersion = "version"
const FlagNameServiceVersion = "service-version"
const FlagNameHelp = "help"

const FlagValueFromStdIn = "-"
Expand All @@ -38,7 +38,7 @@ var FlagNamesPredefined = []string{
FlagNameWaitTimeout,
FlagNameFile,
FlagNameIdentityUri,
FlagNameVersion,
FlagNameServiceVersion,
FlagNameHelp,
}

Expand Down Expand Up @@ -75,8 +75,8 @@ func (b *FlagBuilder) AddHelpFlag() *FlagBuilder {
return b
}

func (b *FlagBuilder) AddVersionFlag(hidden bool) *FlagBuilder {
b.AddFlag(b.versionFlag(hidden))
func (b *FlagBuilder) AddServiceVersionFlag(hidden bool) *FlagBuilder {
b.AddFlag(b.serviceVersionFlag(hidden))
return b
}

Expand Down Expand Up @@ -130,13 +130,13 @@ func (b FlagBuilder) defaultFlags(hidden bool) []*FlagDefinition {
NewFlag(FlagNameIdentityUri, "Identity Server URI", FlagTypeString).
WithEnvVarName("UIPATH_IDENTITY_URI").
WithHidden(hidden),
b.versionFlag(hidden),
b.serviceVersionFlag(hidden),
}
}

func (b FlagBuilder) versionFlag(hidden bool) *FlagDefinition {
return NewFlag(FlagNameVersion, "Specific service version", FlagTypeString).
WithEnvVarName("UIPATH_VERSION").
func (b FlagBuilder) serviceVersionFlag(hidden bool) *FlagDefinition {
return NewFlag(FlagNameServiceVersion, "Specific service version", FlagTypeString).
WithEnvVarName("UIPATH_SERVICE_VERSION").
WithDefaultValue("").
WithHidden(hidden)
}
Expand Down
Loading

0 comments on commit 85910d5

Please sign in to comment.