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

feat(import): allow to take yaml content from stdin in import commands #188

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
24 changes: 16 additions & 8 deletions src/cmd/projectImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func projectImportCmd() *cmdBuilder.Cmd {
Long(i18n.T(i18n.CmdDescProjectImportLong)).
Arg(projectImportArgName).
StringFlag("orgId", "", i18n.T(i18n.OrgIdFlag)).
StringFlag("workingDie", "./", i18n.T(i18n.BuildWorkingDir)).
StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)).
HelpFlag(i18n.T(i18n.CmdHelpProjectImport)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks
Expand All @@ -33,13 +33,21 @@ func projectImportCmd() *cmdBuilder.Cmd {
return err
}

yamlContent, err := yamlReader.ReadContent(
uxBlocks,
cmdData.Args[projectImportArgName][0],
cmdData.Params.GetString("workingDir"),
)
if err != nil {
return err
var yamlContent []byte
if cmdData.Args[projectImportArgName][0] == "-" {
yamlContent, err = yamlReader.ReadContentFromStdin(uxBlocks)
if err != nil {
return err
}
} else {
yamlContent, err = yamlReader.ReadContent(
uxBlocks,
cmdData.Args[projectImportArgName][0],
cmdData.Params.GetString("workingDir"),
)
if err != nil {
return err
}
}

importProjectResponse, err := cmdData.RestApiClient.PostProjectImport(
Expand Down
19 changes: 16 additions & 3 deletions src/cmd/projectServiceImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ func projectServiceImportCmd() *cmdBuilder.Cmd {
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

yamlContent, err := yamlReader.ReadContent(uxBlocks, cmdData.Args[serviceImportArgName][0], "./")
if err != nil {
return err
var err error
var yamlContent []byte
if cmdData.Args[serviceImportArgName][0] == "-" {
yamlContent, err = yamlReader.ReadContentFromStdin(uxBlocks)
if err != nil {
return err
}
} else {
yamlContent, err = yamlReader.ReadContent(
uxBlocks,
cmdData.Args[serviceImportArgName][0],
"./",
)
if err != nil {
return err
}
}

importServiceResponse, err := cmdData.RestApiClient.PostServiceStackImport(
Expand Down
4 changes: 2 additions & 2 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ and your %s.`,
ProjectDeleted: "Project was deleted",

// project import
CmdHelpProjectImport: "the project import command.",
CmdHelpProjectImport: "The project import command. Use \"-\" as importYamlPath for taking yaml content from stdin.",
CmdDescProjectImport: "Creates a new project with one or more services.",
CmdDescProjectImportLong: "Creates a new project with one or more services according to the definition in the import YAML file.",
ProjectImported: "project imported",

// project service import
CmdHelpProjectServiceImport: "the project service import command.",
CmdHelpProjectServiceImport: "The project service import command. Use \"-\" as importYamlPath for taking yaml content from stdin.",
CmdDescProjectServiceImport: "Creates one or more Zerops services in an existing project.",
ServiceImported: "service(s) imported",

Expand Down
22 changes: 22 additions & 0 deletions src/yamlReader/readYaml.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package yamlReader

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
Expand Down Expand Up @@ -52,3 +55,22 @@ func ReadContent(uxBlocks uxBlock.UxBlocks, importYamlPath string, workingDir st
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ImportYamlOk)))
return yamlContent, nil
}

func ReadContentFromStdin(uxBlocks uxBlock.UxBlocks) ([]byte, error) {
buf := new(bytes.Buffer)
size, err := io.Copy(buf, os.Stdin)
if err != nil {
return nil, fmt.Errorf("copying from stdin failed: %w", err)
}

if size == 0 {
return nil, errors.New(i18n.T(i18n.ImportYamlEmpty))
}

if size > 100*1024 {
return nil, errors.New(i18n.T(i18n.ImportYamlTooLarge))
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ImportYamlOk)))
return buf.Bytes(), nil
}