This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from stealthrocket/timecraft-export
timecraft: add export command
- Loading branch information
Showing
6 changed files
with
145 additions
and
50 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
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,98 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"os" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stealthrocket/timecraft/format" | ||
"github.com/stealthrocket/timecraft/internal/print/human" | ||
) | ||
|
||
const exportUsage = ` | ||
Usage: timecraft export <resource type> <resource id> <output file> | ||
The export command reads resources from the time machine registry and writes | ||
them to local files. This command is useful to extract data generated by | ||
timecraft and visualize it with other tools. | ||
The last argument is the location where the resource is exported, typically | ||
a path on the file system. The special value "-" may be set to write the | ||
resource to stdout. | ||
Options: | ||
-h, --help Show this usage information | ||
-r, --registry path Path to the timecraft registry (default to ~/.timecraft) | ||
` | ||
|
||
func export(ctx context.Context, args []string) error { | ||
var ( | ||
registryPath = human.Path("~/.timecraft") | ||
) | ||
|
||
flagSet := newFlagSet("timecraft export", exportUsage) | ||
customVar(flagSet, ®istryPath, "r", "registry") | ||
args = parseFlags(flagSet, args) | ||
|
||
if len(args) != 3 { | ||
return errors.New(`expected resource type, id, and output file as argument` + useCmd("export")) | ||
} | ||
|
||
resource, err := findResource("describe", args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
registry, err := openRegistry(registryPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resource.typ == "log" { | ||
// How should we handle logs? | ||
// - write the manifest.json + segments to a tar archive? | ||
// - combines the segments into a single log file? | ||
// - ??? | ||
return errors.New(`TODO`) | ||
} | ||
|
||
var hash format.Hash | ||
if resource.typ == "process" { | ||
processID, err := uuid.Parse(args[1]) | ||
if err != nil { | ||
return errors.New(`malformed process id (not a UUID)`) | ||
} | ||
manifest, err := registry.LookupLogManifest(ctx, processID) | ||
if err != nil { | ||
return err | ||
} | ||
hash = manifest.Process.Digest | ||
} else { | ||
desc, err := registry.LookupDescriptor(ctx, format.ParseHash(args[1])) | ||
if err != nil { | ||
return err | ||
} | ||
hash = desc.Digest | ||
} | ||
|
||
r, err := registry.LookupResource(ctx, hash) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() | ||
|
||
w := io.Writer(os.Stdout) | ||
if outputFile := args[2]; outputFile != "-" { | ||
f, err := os.Create(outputFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
w = f | ||
} | ||
|
||
_, err = io.Copy(w, r) | ||
return err | ||
} |
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
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