This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
/
cli.go
157 lines (139 loc) · 3.21 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"./vhd"
"fmt"
"github.com/urfave/cli"
"github.com/dustin/go-humanize"
"os"
"path/filepath"
"strconv"
"strings"
)
func createVHD(file, size string, options vhd.VHDOptions) {
isize, err := humanize.ParseBytes(size)
if err != nil {
panic(err)
}
vhd.VHDCreateSparse(uint64(isize), file, options)
fmt.Printf("File %s (%s) created\n", file, humanize.IBytes(uint64(isize)))
}
func rawToFixed(file string, options *vhd.VHDOptions) {
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Printf("Error opening file %s: %s\n", file, err)
os.Exit(1)
}
vhd.RawToFixed(f, options)
f.Close()
os.Rename(file, strings.Replace(file, filepath.Ext(file), ".vhd", -1))
}
func vhdInfo(vhdFile string) {
f, err := os.Open(vhdFile)
if err != nil {
fmt.Printf("Error opening file %s: %s\n", vhdFile, err)
os.Exit(1)
}
defer f.Close()
vhd := vhd.FromFile(f)
vhd.PrintInfo()
}
func main() {
app := cli.NewApp()
app.Version = PKG_VERSION
app.Name = PKG_NAME
app.Usage = "Library and tool to manipulate VHD images"
app.Author = "Sergio Rubio"
app.Email = "rubiojr@frameos.org"
app.Commands = []cli.Command{
{
Name: "create",
Usage: "Create a VHD",
Action: func(c *cli.Context) {
if len(c.Args()) != 2 {
println("Missing command arguments.\n")
fmt.Printf("Usage: %s create <file-path> <size MiB|GiB|...>\n",
app.Name)
os.Exit(1)
}
opts := vhd.VHDOptions{}
tstamp := c.String("timestamp")
if tstamp != "" {
itstamp, err := strconv.Atoi(tstamp)
if err != nil {
panic(err)
}
opts.Timestamp = int64(itstamp)
}
uuid := c.String("uuid")
if uuid != "" {
opts.UUID = uuid
}
createVHD(c.Args()[0], c.Args()[1], opts)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "uuid",
Value: "",
Usage: "Set the UUID of the VHD header",
},
cli.StringFlag{
Name: "timestamp",
Value: "",
Usage: "Set the timestamp of the VHD header (UNIX time format)",
},
},
},
{
Name: "info",
Usage: "Print VHD info",
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
println("Missing command arguments.\n")
fmt.Printf("Usage: %s info <file-path> <size MiB|GiB|...>\n",
app.Name)
os.Exit(1)
}
vhdInfo(c.Args()[0])
},
},
{
Name: "raw2fixed",
Usage: "Convert a RAW image to a fixed VHD",
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
println("Missing command arguments.\n")
fmt.Printf("Usage: %s raw2fixed <file-path>\n",
app.Name)
os.Exit(1)
}
opts := vhd.VHDOptions{}
tstamp := c.String("timestamp")
if tstamp != "" {
itstamp, err := strconv.Atoi(tstamp)
if err != nil {
panic(err)
}
opts.Timestamp = int64(itstamp)
}
uuid := c.String("uuid")
if uuid != "" {
opts.UUID = uuid
}
rawToFixed(c.Args()[0], &opts)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "uuid",
Value: "",
Usage: "Set the UUID of the VHD header",
},
cli.StringFlag{
Name: "timestamp",
Value: "",
Usage: "Set the timestamp of the VHD header (UNIX time format)",
},
},
},
}
app.Run(os.Args)
}