-
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.
Add info and read sub commands to example program
To make room for a new convert sub command, using parallel convert. This is also a better example, having only the relevant argument and separate file for every example command. Example usage: % ./go-qcow2reader-example Usage: ./go-qcow2reader-example COMMAND [OPTIONS...] Available commands: info show image information read read image data and print to stdout % ./go-qcow2reader-example info /tmp/images/test.zlib.qcow2 { "type": "qcow2", "size": 3758096384, ... % time ./go-qcow2reader-example read /tmp/images/test.zlib.qcow2 >/dev/null ./go-qcow2reader-example read /tmp/images/test.zlib.qcow2 > /dev/null 10.05s user 0.35s system 101% cpu 10.279 total Signed-off-by: Nir Soffer <[email protected]>
- Loading branch information
Showing
4 changed files
with
174 additions
and
75 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,67 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/lima-vm/go-qcow2reader" | ||
"github.com/lima-vm/go-qcow2reader/image" | ||
) | ||
|
||
func cmdInfo(args []string) error { | ||
var ( | ||
// Required | ||
filename string | ||
|
||
// Options | ||
debug bool | ||
) | ||
|
||
fs := flag.NewFlagSet("info", flag.ExitOnError) | ||
fs.Usage = func() { | ||
fmt.Fprintf(fs.Output(), "Usage: %s info [OPTIONS...] FILE\n", os.Args[0]) | ||
fs.PrintDefaults() | ||
} | ||
fs.BoolVar(&debug, "debug", false, "enable printing debug messages") | ||
if err := fs.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
switch len(fs.Args()) { | ||
case 0: | ||
return errors.New("no file was specified") | ||
case 1: | ||
filename = fs.Arg(0) | ||
default: | ||
return errors.New("too many files specified") | ||
} | ||
|
||
f, err := os.Open(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, err := qcow2reader.Open(f) | ||
if err != nil { | ||
return err | ||
} | ||
defer img.Close() | ||
|
||
imgInfo := image.NewImageInfo(img) | ||
j, err := json.MarshalIndent(imgInfo, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = fmt.Println(string(j)); err != nil { | ||
return err | ||
} | ||
if err = img.Readable(); err != nil { | ||
logWarn(err.Error()) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/lima-vm/go-qcow2reader" | ||
"github.com/lima-vm/go-qcow2reader/log" | ||
) | ||
|
||
func cmdRead(args []string) error { | ||
var ( | ||
// Required | ||
filename string | ||
|
||
// Options | ||
debug bool | ||
bufferSize int | ||
offset int64 | ||
length int64 | ||
) | ||
|
||
fs := flag.NewFlagSet("read", flag.ExitOnError) | ||
fs.Usage = func() { | ||
fmt.Fprintf(fs.Output(), "Usage: %s read [OPTIONS...] FILE\n", os.Args[0]) | ||
flag.PrintDefaults() | ||
} | ||
fs.BoolVar(&debug, "debug", false, "enable printing debug messages") | ||
fs.IntVar(&bufferSize, "buffer-size", 65536, "buffer size") | ||
fs.Int64Var(&offset, "offset", 0, "offset to read") | ||
fs.Int64Var(&length, "length", -1, "length to read") | ||
if err := fs.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
if debug { | ||
log.SetDebugFunc(logDebug) | ||
} | ||
|
||
switch len(fs.Args()) { | ||
case 0: | ||
return errors.New("no file was specified") | ||
case 1: | ||
filename = fs.Arg(0) | ||
default: | ||
return errors.New("too many files were specified") | ||
} | ||
|
||
f, err := os.Open(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, err := qcow2reader.Open(f) | ||
if err != nil { | ||
return err | ||
} | ||
defer img.Close() | ||
|
||
if length < 0 { | ||
length = img.Size() | ||
} | ||
|
||
buf := make([]byte, bufferSize) | ||
sr := io.NewSectionReader(img, offset, length) | ||
_, err = io.CopyBuffer(os.Stdout, sr, buf) | ||
|
||
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