-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic implementation of in-process ipfs node, implement 'bacalhau get…
…'. (#273)
- Loading branch information
1 parent
f601215
commit e576dea
Showing
5 changed files
with
820 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package bacalhau | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/filecoin-project/bacalhau/pkg/ipfs" | ||
"github.com/filecoin-project/bacalhau/pkg/system" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var getCmdFlags = struct { | ||
ipfsURL string | ||
outputDir string | ||
}{ | ||
ipfsURL: "ipfs.io", | ||
outputDir: ".", | ||
} | ||
|
||
func init() { | ||
getCmd.Flags().StringVar(&getCmdFlags.ipfsURL, "ipfs-url", | ||
getCmdFlags.ipfsURL, "URL of the IPFS gateway to use.") | ||
getCmd.Flags().StringVar(&getCmdFlags.outputDir, "output-dir", | ||
getCmdFlags.outputDir, "Directory to write the output to.") | ||
} | ||
|
||
var getCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "Get the results of a job", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cm := system.NewCleanupManager() | ||
defer cm.Cleanup() | ||
|
||
log.Debug().Msgf("Fetching results of job '%s'...", args[0]) | ||
job, ok, err := getAPIClient().Get(context.Background(), args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
if !ok { | ||
return fmt.Errorf("job not found") | ||
} | ||
|
||
var resultCIDs []string | ||
for _, jobState := range job.State { | ||
if jobState.ResultsId != "" { | ||
resultCIDs = append(resultCIDs, jobState.ResultsId) | ||
} | ||
} | ||
log.Debug().Msgf("Job has result CIDs: %v", resultCIDs) | ||
|
||
log.Debug().Msg("Spinning up IPFS client...") | ||
cl, err := ipfs.NewClient(cm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, cid := range resultCIDs { | ||
outputDir := filepath.Join(getCmdFlags.outputDir, cid) | ||
log.Debug().Msgf("Downloading result CID '%s' to '%s'...", | ||
cid, outputDir) | ||
|
||
err = cl.Get(context.Background(), cid, outputDir) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.