github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/cmd/bacalhau/get.go (about)

     1  package bacalhau
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/downloader/util"
     7  	"github.com/filecoin-project/bacalhau/pkg/model"
     8  	"github.com/filecoin-project/bacalhau/pkg/system"
     9  	"github.com/filecoin-project/bacalhau/pkg/util/templates"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  	"k8s.io/kubectl/pkg/util/i18n"
    13  )
    14  
    15  var (
    16  	getLong = templates.LongDesc(i18n.T(`
    17  		Get the results of the job, including stdout and stderr.
    18  `))
    19  
    20  	//nolint:lll // Documentation
    21  	getExample = templates.Examples(i18n.T(`
    22  		# Get the results of a job.
    23  		bacalhau get 51225160-807e-48b8-88c9-28311c7899e1
    24  
    25  		# Get the results of a job, with a short ID.
    26  		bacalhau get ebd9bf2f
    27  `))
    28  )
    29  
    30  type GetOptions struct {
    31  	IPFSDownloadSettings *model.DownloaderSettings
    32  }
    33  
    34  func NewGetOptions() *GetOptions {
    35  	return &GetOptions{
    36  		IPFSDownloadSettings: util.NewDownloadSettings(),
    37  	}
    38  }
    39  
    40  func newGetCmd() *cobra.Command {
    41  	OG := NewGetOptions()
    42  
    43  	getCmd := &cobra.Command{
    44  		Use:     "get [id]",
    45  		Short:   "Get the results of a job",
    46  		Long:    getLong,
    47  		Example: getExample,
    48  		Args:    cobra.ExactArgs(1),
    49  		PreRun:  applyPorcelainLogLevel,
    50  		RunE: func(cmd *cobra.Command, cmdArgs []string) error {
    51  			return get(cmd, cmdArgs, OG)
    52  		},
    53  	}
    54  
    55  	getCmd.PersistentFlags().AddFlagSet(NewIPFSDownloadFlags(OG.IPFSDownloadSettings))
    56  
    57  	return getCmd
    58  }
    59  
    60  func get(cmd *cobra.Command, cmdArgs []string, OG *GetOptions) error {
    61  	ctx := cmd.Context()
    62  
    63  	cm := cmd.Context().Value(systemManagerKey).(*system.CleanupManager)
    64  
    65  	var err error
    66  
    67  	jobID := cmdArgs[0]
    68  	if jobID == "" {
    69  		var byteResult []byte
    70  		byteResult, err = ReadFromStdinIfAvailable(cmd, cmdArgs)
    71  		if err != nil {
    72  			Fatal(cmd, fmt.Sprintf("Unknown error reading from file: %s\n", err), 1)
    73  			return err
    74  		}
    75  		jobID = string(byteResult)
    76  	}
    77  
    78  	err = downloadResultsHandler(
    79  		ctx,
    80  		cm,
    81  		cmd,
    82  		jobID,
    83  		*OG.IPFSDownloadSettings,
    84  	)
    85  
    86  	if err != nil {
    87  		return errors.Wrap(err, "error downloading job")
    88  	}
    89  
    90  	return nil
    91  }