Skip to content
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

z0 - add projest service-add #154

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ func projectCmd() *cmdBuilder.Cmd {
AddChildrenCmd(projectListCmd()).
AddChildrenCmd(projectDeleteCmd()).
AddChildrenCmd(projectServiceImportCmd()).
AddChildrenCmd(projectImportCmd())
AddChildrenCmd(projectImportCmd()).
AddChildrenCmd(projectServiceAddCmd())
}
103 changes: 103 additions & 0 deletions src/cmd/projectServiceAdd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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/enum"
"github.com/zeropsio/zerops-go/types/stringId"
)

const serviceAddArgName = "serviceAddName"
const serviceAddArgType = "type"
const serviceAddArgHa = "ha"

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.ServiceAddTypeFlag)).
BoolFlag(serviceAddArgHa, false, i18n.T(i18n.ServiceAddHaFlag)).
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
}

mode := enum.ServiceStackModeEnumNonHa
if cmdData.Params.GetBool(serviceAddArgHa) {
mode = enum.ServiceStackModeEnumHa
}

serviceAddResponse, err := cmdData.RestApiClient.PostServiceStack(ctx,
path.ServiceStackServiceStackTypeVersionId{
ServiceStackTypeVersionId: typeNameVersionId,
},
body.PostStandardServiceStack{
ProjectId: cmdData.Project.ID,
Name: types.NewString(name),
Mode: &mode,
},
)
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
})
}
1 change: 1 addition & 0 deletions src/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func serviceCmd() *cmdBuilder.Cmd {
AddChildrenCmd(serviceLogCmd()).
AddChildrenCmd(serviceStartCmd()).
AddChildrenCmd(serviceStopCmd()).
AddChildrenCmd(serviceEnvCmd()).
AddChildrenCmd(servicePushCmd()).
AddChildrenCmd(serviceEnableSubdomainCmd()).
AddChildrenCmd(serviceDeployCmd())
Expand Down
129 changes: 129 additions & 0 deletions src/cmd/serviceEnv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"text/template"

"github.com/pkg/errors"
"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/zerops-go/dto/input/body"
"github.com/zeropsio/zerops-go/types"
"github.com/zeropsio/zerops-go/types/enum"
"gopkg.in/yaml.v3"

"github.com/zeropsio/zcli/src/i18n"
)

func serviceEnvCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("env").
Short(i18n.T(i18n.CmdDescServiceEnv)).
ScopeLevel(scope.Service).
Arg(scope.ServiceArgName, cmdBuilder.OptionalArg()).
StringFlag("format", "env", i18n.T(i18n.ServiceEnvFormatFlag)).
BoolFlag("no-secrets", false, i18n.T(i18n.ServiceEnvNoSecretsFlag)).
HelpFlag(i18n.T(i18n.CmdHelpServiceEnv)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
var userDataSetup repository.GetUserDataSetup
if cmdData.Params.GetBool("no-secrets") {
userDataSetup.EsFilters(func(filter body.EsFilter) body.EsFilter {
filter.Search = append(filter.Search, body.EsSearchItem{
Name: "type",
Operator: "ne",
Value: types.String(enum.UserDataTypeEnumSecret),
})
return filter
})
}

userData, err := repository.GetUserDataByProjectId(
ctx,
cmdData.RestApiClient,
cmdData.Project,
userDataSetup,
)
if err != nil {
return err
}

allEnvs := make(map[string]entity.UserData, len(userData))
for _, env := range userData {
allEnvs[fmt.Sprintf("%s_%s", env.ServiceName, env.Key)] = env
}

envs := make(map[string]entity.UserData)
for key, env := range allEnvs {
c := env.Content.Native()
c = os.Expand(c, func(s string) string {
e, ok := allEnvs[s]
if ok {
return e.Content.Native()
}
return s
})
env.Content = types.NewText(c)
if env.ServiceId == cmdData.Service.ID {
envs[key] = env
}
}

format := cmdData.Params.GetString("format")
formatSplit := strings.SplitN(format, "=", 2)
formatKind := formatSplit[0]

switch formatKind {
case "json":
enc := json.NewEncoder(cmdData.Stdout)
enc.SetIndent("", "\t")
out := make(map[string]string, len(envs))
for _, e := range envs {
out[e.Key.Native()] = e.Content.Native()
}
if err := enc.Encode(out); err != nil {
return err
}
case "yaml":
enc := yaml.NewEncoder(cmdData.Stdout)
out := make(map[string]string, len(envs))
for _, e := range envs {
out[e.Key.Native()] = e.Content.Native()
}
if err := enc.Encode(out); err != nil {
return err
}
case "value":
for _, env := range envs {
cmdData.Stdout.Println(env.Content)
}
case "go-template":
if len(formatSplit) < 2 {
return errors.New(i18n.T(i18n.ServiceEnvNoTemplateData))
}
formatTemplate := formatSplit[1]
t, err := template.New("go").Parse(formatTemplate + "\n")
if err != nil {
return err
}
for _, value := range envs {
if err := t.Execute(cmdData.Stdout, value); err != nil {
return err
}
}
case "env":
for _, env := range envs {
cmdData.Stdout.Printf("%s=%s\n", env.Key, env.Content)
}
default:
return errors.New(i18n.T(i18n.ServiceEnvInvalidFormatKind, formatKind))
}

return nil
})
}
83 changes: 83 additions & 0 deletions src/entity/repository/serviceStackType.go
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))
}
Loading
Loading