-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #5800 from filecoin-project/feat/allinfo-utils
shed command to unpack miner info dumps
- Loading branch information
Showing
3 changed files
with
134 additions
and
20 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,113 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
var minerCmd = &cli.Command{ | ||
Name: "miner", | ||
Usage: "miner-related utilities", | ||
Subcommands: []*cli.Command{ | ||
minerUnpackInfoCmd, | ||
}, | ||
} | ||
|
||
var minerUnpackInfoCmd = &cli.Command{ | ||
Name: "unpack-info", | ||
Usage: "unpack miner info all dump", | ||
ArgsUsage: "[allinfo.txt] [dir]", | ||
Action: func(cctx *cli.Context) error { | ||
if cctx.Args().Len() != 2 { | ||
return xerrors.Errorf("expected 2 args") | ||
} | ||
|
||
src, err := homedir.Expand(cctx.Args().Get(0)) | ||
if err != nil { | ||
return xerrors.Errorf("expand src: %w", err) | ||
} | ||
|
||
f, err := os.Open(src) | ||
if err != nil { | ||
return xerrors.Errorf("open file: %w", err) | ||
} | ||
defer f.Close() // nolint | ||
|
||
dest, err := homedir.Expand(cctx.Args().Get(1)) | ||
if err != nil { | ||
return xerrors.Errorf("expand dest: %w", err) | ||
} | ||
|
||
var outf *os.File | ||
|
||
r := bufio.NewReader(f) | ||
for { | ||
l, _, err := r.ReadLine() | ||
if err == io.EOF { | ||
if outf != nil { | ||
return outf.Close() | ||
} | ||
} | ||
if err != nil { | ||
return xerrors.Errorf("read line: %w", err) | ||
} | ||
sl := string(l) | ||
|
||
if strings.HasPrefix(sl, "#") { | ||
if strings.Contains(sl, "..") { | ||
return xerrors.Errorf("bad name %s", sl) | ||
} | ||
|
||
if strings.HasPrefix(sl, "#: ") { | ||
if outf != nil { | ||
if err := outf.Close(); err != nil { | ||
return xerrors.Errorf("close out file: %w", err) | ||
} | ||
} | ||
p := filepath.Join(dest, sl[len("#: "):]) | ||
if err := os.MkdirAll(filepath.Dir(p), 0775); err != nil { | ||
return xerrors.Errorf("mkdir: %w", err) | ||
} | ||
outf, err = os.Create(p) | ||
if err != nil { | ||
return xerrors.Errorf("create out file: %w", err) | ||
} | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(sl, "##: ") { | ||
if outf != nil { | ||
if err := outf.Close(); err != nil { | ||
return xerrors.Errorf("close out file: %w", err) | ||
} | ||
} | ||
p := filepath.Join(dest, "Per Sector Infos", sl[len("##: "):]) | ||
if err := os.MkdirAll(filepath.Dir(p), 0775); err != nil { | ||
return xerrors.Errorf("mkdir: %w", err) | ||
} | ||
outf, err = os.Create(p) | ||
if err != nil { | ||
return xerrors.Errorf("create out file: %w", err) | ||
} | ||
continue | ||
} | ||
} | ||
|
||
if outf != nil { | ||
if _, err := outf.Write(l); err != nil { | ||
return xerrors.Errorf("write line: %w", err) | ||
} | ||
if _, err := outf.Write([]byte("\n")); err != nil { | ||
return xerrors.Errorf("write line end: %w", 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