-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Saidl
committed
Jun 23, 2024
1 parent
2ac4336
commit b2cfaf6
Showing
10 changed files
with
422 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/zeropsio/zcli/src/cmd/scope" | ||
"github.com/zeropsio/zcli/src/cmdBuilder" | ||
"github.com/zeropsio/zcli/src/entity" | ||
"github.com/zeropsio/zcli/src/entity/repository" | ||
"github.com/zeropsio/zcli/src/i18n" | ||
"github.com/zeropsio/zcli/src/uxHelpers" | ||
"github.com/zeropsio/zerops-go/dto/input/body" | ||
"github.com/zeropsio/zerops-go/dto/input/path" | ||
"github.com/zeropsio/zerops-go/types" | ||
"github.com/zeropsio/zerops-go/types/stringId" | ||
) | ||
|
||
const serviceAddArgName = "serviceAddName" | ||
const serviceAddArgType = "type" | ||
|
||
func projectServiceAddCmd() *cmdBuilder.Cmd { | ||
return cmdBuilder.NewCmd(). | ||
Use("service-add"). | ||
Short(i18n.T(i18n.CmdDescProjectServiceAdd)). | ||
ScopeLevel(scope.Project). | ||
Arg(serviceAddArgName). | ||
StringFlag(serviceAddArgType, "", i18n.T(i18n.ServiceTypeFlag)). | ||
HelpFlag(i18n.T(i18n.CmdHelpProjectServiceAdd)). | ||
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error { | ||
name := cmdData.Args[serviceAddArgName][0] | ||
|
||
var typeNameVersion entity.ServiceStackTypeVersion | ||
var typeNameVersionId stringId.ServiceStackTypeVersionId | ||
|
||
if cmdData.Params.GetString(serviceAddArgType) == "" { | ||
serviceStackType, err := uxHelpers.PrintServiceStackTypeSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient) | ||
if err != nil { | ||
return err | ||
} | ||
if len(serviceStackType.Versions) == 1 { | ||
typeNameVersion = serviceStackType.Versions[0] | ||
} else { | ||
typeNameVersion, err = uxHelpers.PrintServiceStackTypeVersionSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient, | ||
uxHelpers.PrintServiceStackTypeVersionSelectorWithServiceStackTypeIdFilter(serviceStackType), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
typeNameVersionId = typeNameVersion.ID | ||
} else { | ||
input := cmdData.Params.GetString(serviceAddArgType) | ||
serviceStackType, err := repository.GetServiceStackTypeById(ctx, cmdData.RestApiClient, stringId.ServiceStackTypeId(input)) | ||
if err != nil { | ||
return err | ||
} | ||
typeNameVersionId = serviceStackType.Versions[0].ID | ||
} | ||
|
||
serviceAddResponse, err := cmdData.RestApiClient.PostServiceStack(ctx, | ||
path.ServiceStackServiceStackTypeVersionId{ | ||
ServiceStackTypeVersionId: typeNameVersionId, | ||
}, | ||
body.PostStandardServiceStack{ | ||
ProjectId: cmdData.Project.ID, | ||
Name: types.NewString(name), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
serviceAddOutput, err := serviceAddResponse.Output() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = uxHelpers.ProcessCheckWithSpinner( | ||
ctx, | ||
cmdData.UxBlocks, | ||
[]uxHelpers.Process{{ | ||
F: uxHelpers.CheckZeropsProcess(serviceAddOutput.Process.Id, cmdData.RestApiClient), | ||
RunningMessage: i18n.T(i18n.ServiceAdding), | ||
ErrorMessageMessage: i18n.T(i18n.ServiceAddFailed), | ||
SuccessMessage: i18n.T(i18n.ServiceAdded), | ||
}}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
} |
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,83 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sort" | ||
|
||
"github.com/zeropsio/zcli/src/entity" | ||
"github.com/zeropsio/zcli/src/zeropsRestApiClient" | ||
"github.com/zeropsio/zerops-go/errorCode" | ||
"github.com/zeropsio/zerops-go/types/stringId" | ||
) | ||
|
||
func GetServiceStackTypes( | ||
ctx context.Context, | ||
restApiClient *zeropsRestApiClient.Handler, | ||
) (result []entity.ServiceStackType, _ error) { | ||
settings, err := restApiClient.GetSettings(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
settingsOutput, err := settings.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, serviceStackType := range settingsOutput.ServiceStackList { | ||
e := entity.ServiceStackType{ | ||
ID: serviceStackType.Id, | ||
Name: serviceStackType.Name, | ||
} | ||
|
||
for _, serviceStackTypeVersion := range serviceStackType.ServiceStackTypeVersionList { | ||
if !serviceStackTypeVersion.Status.IsActive() { | ||
continue | ||
} | ||
if serviceStackTypeVersion.IsBuild.Native() { | ||
continue | ||
} | ||
if serviceStackTypeVersion.Name.Native() == "prepare_runtime" { | ||
continue | ||
} | ||
e.Versions = append(e.Versions, entity.ServiceStackTypeVersion{ | ||
ID: serviceStackTypeVersion.Id, | ||
Name: serviceStackTypeVersion.Name, | ||
ExactVersionNumber: serviceStackTypeVersion.ExactVersionNumber, | ||
}) | ||
} | ||
if len(e.Versions) == 0 { | ||
continue | ||
} | ||
result = append(result, e) | ||
} | ||
sort.Slice(result, func(i, j int) bool { return result[i].Name.Native() < result[j].Name.Native() }) | ||
return result, nil | ||
} | ||
|
||
func GetServiceStackTypeById( | ||
ctx context.Context, | ||
restApiClient *zeropsRestApiClient.Handler, | ||
serviceStackTypeId stringId.ServiceStackTypeId, | ||
) (result entity.ServiceStackType, _ error) { | ||
serviceStackTypes, err := GetServiceStackTypes(ctx, restApiClient) | ||
if err != nil { | ||
return result, err | ||
} | ||
for _, serviceStackType := range serviceStackTypes { | ||
if serviceStackType.ID == serviceStackTypeId { | ||
return serviceStackType, nil | ||
} | ||
if serviceStackType.Name.Native() == serviceStackTypeId.Native() { | ||
return serviceStackType, nil | ||
} | ||
for _, serviceStackTypeVersion := range serviceStackType.Versions { | ||
if serviceStackTypeVersion.ID.Native() == serviceStackTypeId.Native() { | ||
return serviceStackType, nil | ||
} | ||
if serviceStackTypeVersion.Name.Native() == serviceStackTypeId.Native() { | ||
return serviceStackType, nil | ||
} | ||
} | ||
} | ||
return result, errors.New(string(errorCode.ServiceStackTypeVersionNotFound)) | ||
} |
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,18 @@ | ||
package entity | ||
|
||
import ( | ||
"github.com/zeropsio/zerops-go/types" | ||
"github.com/zeropsio/zerops-go/types/stringId" | ||
) | ||
|
||
type ServiceStackType struct { | ||
ID stringId.ServiceStackTypeId | ||
Name types.String | ||
Versions []ServiceStackTypeVersion | ||
} | ||
|
||
type ServiceStackTypeVersion struct { | ||
ID stringId.ServiceStackTypeVersionId | ||
Name types.String | ||
ExactVersionNumber types.EmptyString | ||
} |
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 options | ||
|
||
type OptionError[T any] func(*T) error | ||
|
||
func ApplyOptionsError[K any, T ~func(*K) error](in ...T) (K, []error) { | ||
var emptyValue K | ||
return ApplyOptionsErrorWithDefault[K](emptyValue, in...) | ||
} | ||
|
||
func ApplyOptionsErrorWithDefault[K any, T ~func(*K) error](k K, in ...T) (K, []error) { | ||
var errors []error | ||
for _, o := range in { | ||
if err := o(&k); err != nil { | ||
errors = append(errors, err) | ||
} | ||
} | ||
return k, errors | ||
} | ||
|
||
type Option[T any] func(*T) | ||
|
||
func ApplyOptions[K any, T ~func(*K)](in ...T) K { | ||
var emptyValue K | ||
return ApplyOptionsWithDefault(emptyValue, in...) | ||
} | ||
|
||
func ApplyOptionsWithDefault[K any, T ~func(*K)](k K, in ...T) K { | ||
for _, o := range in { | ||
o(&k) | ||
} | ||
return k | ||
} |
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,31 @@ | ||
package uxHelpers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/zeropsio/zcli/src/entity" | ||
"github.com/zeropsio/zcli/src/entity/repository" | ||
"github.com/zeropsio/zcli/src/i18n" | ||
"github.com/zeropsio/zcli/src/uxBlock" | ||
"github.com/zeropsio/zcli/src/zeropsRestApiClient" | ||
) | ||
|
||
func PrintServiceStackTypeSelector( | ||
ctx context.Context, | ||
uxBlocks uxBlock.UxBlocks, | ||
restApiClient *zeropsRestApiClient.Handler, | ||
) (result entity.ServiceStackType, _ error) { | ||
serviceStackTypes, err := repository.GetServiceStackTypes(ctx, restApiClient) | ||
if err != nil { | ||
return result, err | ||
} | ||
|
||
return SelectOne(ctx, uxBlocks, serviceStackTypes, | ||
SelectOneWithHeader[entity.ServiceStackType]("ID", "Name"), | ||
SelectOneWithSelectLabel[entity.ServiceStackType](i18n.T(i18n.ServiceStackTypeSelectorPrompt)), | ||
SelectOneWithNotFound[entity.ServiceStackType](i18n.T(i18n.ServiceStackTypeSelectorOutOfRangeError)), | ||
SelectOneWithRow(func(in entity.ServiceStackType) []string { | ||
return []string{string(in.ID), in.Name.String()} | ||
}), | ||
) | ||
} |
Oops, something went wrong.