Skip to content

feat: project env command #222

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

Open
wants to merge 1 commit 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 @@ -14,5 +14,6 @@ func projectCmd() *cmdBuilder.Cmd {
AddChildrenCmd(projectListCmd()).
AddChildrenCmd(projectDeleteCmd()).
AddChildrenCmd(projectServiceImportCmd()).
AddChildrenCmd(projectImportCmd())
AddChildrenCmd(projectImportCmd()).
AddChildrenCmd(projectEnvCmd())
}
77 changes: 77 additions & 0 deletions src/cmd/projectEnv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"bufio"
"context"
"strings"
"text/template"

"github.com/pkg/errors"
"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zerops-go/dto/input/path"
"github.com/zeropsio/zerops-go/dto/input/query"
"github.com/zeropsio/zerops-go/types/enum"
)

func projectEnvCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("env").
ScopeLevel(cmdBuilder.ScopeProject()).
Short("Loads project envs.").
HelpFlag("Help for the project env command.").
BoolFlag("export", false, "Prepends export to each env in output 'export {{.Key}}={{.Value}}'.").
StringFlag("template", "{{.Key}}={{.Value}}", "Output template.").
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
project, err := cmdData.Project.Expect("project is nil")

Check failure on line 25 in src/cmd/projectEnv.go

View workflow job for this annotation

GitHub Actions / build && tests for windows amd64

ineffectual assignment to err (ineffassign)

Check failure on line 25 in src/cmd/projectEnv.go

View workflow job for this annotation

GitHub Actions / build && tests for darwin arm64

ineffectual assignment to err (ineffassign)

Check failure on line 25 in src/cmd/projectEnv.go

View workflow job for this annotation

GitHub Actions / build && tests for darwin amd64

ineffectual assignment to err (ineffassign)

Check failure on line 25 in src/cmd/projectEnv.go

View workflow job for this annotation

GitHub Actions / build && tests for linux amd64

ineffectual assignment to err (ineffassign)

templateString := cmdData.Params.GetString("template")
if cmdData.Params.GetBool("export") {
templateString = "export {{.Key}}={{.Value}}"
}

tmpl, err := template.New("envs").Parse(templateString)
if err != nil {
return errors.WithStack(err)
}

response, err := cmdData.RestApiClient.GetProjectEnvFile(
ctx,
path.ProjectId{Id: project.Id},
query.GetProjectEnvFile{
OverrideEnvIsolation: enum.GetProjectEnvFileOverrideEnvIsolationEnumNone,
},
)
if err != nil {
return errors.WithStack(err)
}

envs, err := response.Output()
if err != nil {
return errors.WithStack(err)
}

output := new(strings.Builder)
scanner := bufio.NewScanner(strings.NewReader(envs.EnvFile.String()))
for scanner.Scan() {
parts := strings.SplitN(scanner.Text(), "=", 2)
if len(parts) != 2 {
return errors.New("unexpected env file format")
}
if err := tmpl.Execute(output, Env{
Key: parts[0],
Value: parts[1],
}); err != nil {
return errors.WithStack(err)
}
output.WriteRune('\n')
}

cmdData.Stdout.Println(output.String()[:output.Len()-1])

return nil
})
}

type Env struct {
Key, Value string
}
3 changes: 3 additions & 0 deletions src/cmdBuilder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func warpStringForTerminal(s string) string {

// warpString wraps string after num of runes (excluding space as rune), keeping words intact
func warpString(s string, runeCutCount uint) string {
if s == "" {
return ""
}
output := strings.Builder{}
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Split(bufio.ScanWords)
Expand Down
Loading