Skip to content

Commit

Permalink
Adjustments following PR review
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine THEBAUD <antoine.thebaud@yahoo.fr>
  • Loading branch information
AntoineThebaud committed Jan 19, 2024
1 parent c70559c commit 89987eb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion internal/cli/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ cat ./resources.json | percli apply -f -
opt.AddProjectFlags(cmd, &o.ProjectOption)
opt.AddFileFlags(cmd, &o.FileOption)
opt.AddDirectoryFlags(cmd, &o.DirectoryOption)
cmd.MarkFlagsMutuallyExclusive("file", "directory")
opt.MarkFileAndDirFlagsAsXOR(cmd)
return cmd
}
4 changes: 2 additions & 2 deletions internal/cli/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"testing"

cmdTest "github.com/perses/perses/internal/cli/test"
"github.com/perses/perses/pkg/client/fake/api"
fakeapi "github.com/perses/perses/pkg/client/fake/api"
)

func TestApplyCMD(t *testing.T) {
Expand All @@ -26,7 +26,7 @@ func TestApplyCMD(t *testing.T) {
Title: "empty args",
Args: []string{},
IsErrorExpected: true,
ExpectedMessage: "you need to set the flag --directory or --file for this command",
ExpectedMessage: "at least one of the flags in the group [file directory] is required",
},
{
Title: "not connected to any API",
Expand Down
47 changes: 25 additions & 22 deletions internal/cli/cmd/dac/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ const (
modeStdout = "stdout"
)

// writeToFile writes data to a file
func writeToFile(filePath string, data []byte) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()

_, err = file.Write(data)
return err
}

type option struct {
persesCMD.Option
opt.FileOption
Expand Down Expand Up @@ -82,12 +94,14 @@ func (o *option) Execute() error {
}

// Process regular files only
if !info.IsDir() {
err = o.processFile(path)
if err != nil {
// Tell the user about the error but don't stop the processing
fmt.Printf("error processing file %s: %v\n", path, err)
}
if info.IsDir() {
return nil
}

err = o.processFile(path)
if err != nil {
// Tell the user about the error but don't stop the processing
fmt.Printf("error processing file %s: %v\n", path, err)
}

return nil
Expand All @@ -101,10 +115,11 @@ func (o *option) Execute() error {
}

func (o *option) processFile(file string) error {
// NB: this command is mostly based on the `eval` command of the cue CLI.
// NB: most of the work of the `build` command is actually made by the `eval` command of the cue CLI.
// NB2: Since cue is written in Go, we could consider relying on its code instead of going the exec way.
// However the cue code is (for now at least) not well packaged for such external reuse.
// See https://github.com/cue-lang/cue/blob/master/cmd/cue/cmd/eval.go#L87
// NB3: #nosec is needed here even if the user-fed parts of the command are sanitized upstream
cmd := exec.Command("cue", "eval", file, "--out", o.Output, "--concrete") // #nosec

// Capture the output of the command
Expand All @@ -129,7 +144,7 @@ func (o *option) processFile(file string) error {
}

// Build the path of the file where to store the command output
outputFilePath := buildOutputFilePath(file, o.OutputOption.Output)
outputFilePath := o.buildOutputFilePath(file)

// Write the output to the file
err = writeToFile(outputFilePath, cmdOutput)
Expand All @@ -141,23 +156,11 @@ func (o *option) processFile(file string) error {
}

// buildOutputFilePath generates the output file path based on the input file path
func buildOutputFilePath(inputFilePath string, ext string) string {
func (o *option) buildOutputFilePath(inputFilePath string) string {
// Extract the file name without extension
baseName := strings.TrimSuffix(inputFilePath, filepath.Ext(inputFilePath))
// Build the output file path in the "built" folder with the same name as the input file
return filepath.Join(outputFolderName, fmt.Sprintf("%s_output.%s", baseName, ext)) // Change the extension as needed
}

// writeToFile writes data to a file
func writeToFile(filePath string, data []byte) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()

_, err = file.Write(data)
return err
return filepath.Join(outputFolderName, fmt.Sprintf("%s_output.%s", baseName, o.Output)) // Change the extension as needed
}

func (o *option) SetWriter(writer io.Writer) {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ percli lint -f ./resources.json --online
}
opt.AddFileFlags(cmd, &o.FileOption)
opt.AddDirectoryFlags(cmd, &o.DirectoryOption)
opt.MarkFileFlagAsMandatory(cmd)
opt.MarkFileAndDirFlagsAsXOR(cmd)
cmd.Flags().StringVar(&o.chartsSchemas, "schemas.charts", "", "Path to the CUE schemas for dasbhoard charts.")
cmd.Flags().StringVar(&o.queriesSchemas, "schemas.queries", "", "Path to the CUE schemas for chart queries.")
cmd.Flags().StringVar(&o.datasourcesSchemas, "schemas.datasources", "", "Path to the CUE schemas for the datasources")
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLintCMD(t *testing.T) {
Title: "empty args",
Args: []string{},
IsErrorExpected: true,
ExpectedMessage: `required flag(s) "file" not set`,
ExpectedMessage: `at least one of the flags in the group [file directory] is required`,
},
{
Title: "use args",
Expand Down

0 comments on commit 89987eb

Please sign in to comment.