mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-22 20:00:18 +00:00
NOISSUE - Refactor result command to improve output path handling and update usage instructions (#549)
* Refactor result command to improve output path handling and update usage instructions Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor output path handling in NewResultsCmd to simplify directory creation and remove redundant comments Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Update cli/result.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: Sammy Oina <sammyoina@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
de8e198b71
commit
291755ec87
+31
-14
@@ -5,23 +5,23 @@ package cli
|
||||
import (
|
||||
"encoding/pem"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
resultFilePrefix = "results"
|
||||
resultFileExt = ".zip"
|
||||
resultfilename = "results.zip"
|
||||
)
|
||||
const resultFilename = "results.zip"
|
||||
|
||||
func (cli *CLI) NewResultsCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "result",
|
||||
var outputDir string
|
||||
var filename string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "result <private_key_file_path>",
|
||||
Short: "Retrieve computation result file",
|
||||
Example: "result <private_key_file_path> <optional_file_name.zip>",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Example: "result <private_key_file_path> --filename my_results.zip --output-dir /path/to/directory",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if cli.connectErr != nil {
|
||||
printError(cmd, "Failed to connect to agent: %v ❌ ", cli.connectErr)
|
||||
@@ -36,9 +36,20 @@ func (cli *CLI) NewResultsCmd() *cobra.Command {
|
||||
return
|
||||
}
|
||||
|
||||
filename := resultfilename
|
||||
if len(args) > 1 {
|
||||
filename = args[1]
|
||||
var outputPath string
|
||||
if outputDir != "" {
|
||||
if err := os.MkdirAll(outputDir, 0o755); err != nil {
|
||||
printError(cmd, "Error creating output directory: %v ❌ ", err)
|
||||
return
|
||||
}
|
||||
outputPath = filepath.Join(outputDir, filename)
|
||||
} else {
|
||||
outputPath = filename
|
||||
}
|
||||
|
||||
absPath, err := filepath.Abs(outputPath)
|
||||
if err != nil {
|
||||
absPath = outputPath
|
||||
}
|
||||
|
||||
pemBlock, _ := pem.Decode(privKeyFile)
|
||||
@@ -49,7 +60,7 @@ func (cli *CLI) NewResultsCmd() *cobra.Command {
|
||||
return
|
||||
}
|
||||
|
||||
resultFile, err := os.Create(filename)
|
||||
resultFile, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
printError(cmd, "Error creating result file: %v ❌ ", err)
|
||||
return
|
||||
@@ -61,7 +72,13 @@ func (cli *CLI) NewResultsCmd() *cobra.Command {
|
||||
return
|
||||
}
|
||||
|
||||
cmd.Println(color.New(color.FgGreen).Sprintf("Computation result retrieved and saved successfully as %s! ✔ ", filename))
|
||||
cmd.Println(color.New(color.FgGreen).Sprintf("Computation result retrieved and saved successfully! ✔"))
|
||||
cmd.Println(color.New(color.FgCyan).Sprintf("📁 Location: %s", absPath))
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&outputDir, "output-dir", "o", "", "Directory where the result file will be saved")
|
||||
cmd.Flags().StringVarP(&filename, "filename", "f", resultFilename, "Name of the result file")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user