Skip to content

Commit

Permalink
feat(import): allow to take yaml content from stdin in import commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tikinang committed Oct 29, 2024
1 parent 4590633 commit 91404e1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
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
}

0 comments on commit 91404e1

Please sign in to comment.