mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
291755ec87
* 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>
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package cli
|
|
|
|
import (
|
|
"encoding/pem"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const resultFilename = "results.zip"
|
|
|
|
func (cli *CLI) NewResultsCmd() *cobra.Command {
|
|
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> --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)
|
|
return
|
|
}
|
|
|
|
cmd.Println("⏳ Retrieving computation result file")
|
|
|
|
privKeyFile, err := os.ReadFile(args[0])
|
|
if err != nil {
|
|
printError(cmd, "Error reading private key file: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
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)
|
|
|
|
privKey, err := decodeKey(pemBlock)
|
|
if err != nil {
|
|
printError(cmd, "Error decoding private key: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
resultFile, err := os.Create(outputPath)
|
|
if err != nil {
|
|
printError(cmd, "Error creating result file: %v ❌ ", err)
|
|
return
|
|
}
|
|
defer resultFile.Close()
|
|
|
|
if err = cli.agentSDK.Result(cmd.Context(), privKey, resultFile); err != nil {
|
|
printError(cmd, "Error retrieving computation result: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|