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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
package cli
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"github.com/checkpoint-restore/go-criu/v7/crit"
"github.com/spf13/cobra"
)
var (
// The crit service used to invoke all commands
c crit.Critter
// All members needed for crit struct
inputFilePath string
outputFilePath string
inputDirPath string
pretty bool
noPayload bool
)
// The `crit` command
var rootCmd = &cobra.Command{
Use: "crit",
Short: "CRIU Image Tool(CRIT) to manipulate CRIU image files",
Long: `CRIU Image Tool (CRIT) is a command line tool to investigate
binary image files generated by CRIU and view them in JSON.
This is a Go implementation of the original Python app.
Find the complete documentation is at https://criu.org/CRIT`,
}
// The `crit decode` command
var decodeCmd = &cobra.Command{
Use: "decode",
Short: "Convert binary image to JSON",
Long: `Convert the input binary image to JSON and write it to a file.
If no output file is provided, the JSON is printed to stdout.`,
Run: func(cmd *cobra.Command, args []string) {
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}
c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
entryType, err := GetEntryTypeFromImg(inputFile)
if err != nil {
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
img, err := c.Decode(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))
}
var jsonData []byte
if pretty {
jsonData, err = json.MarshalIndent(img, "", " ")
} else {
jsonData, err = json.Marshal(img)
}
if err != nil {
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
// If no output file, print to stdout
if outputFilePath == "" {
fmt.Println(string(jsonData))
return
}
// Write to output file
jsonFile, err := os.Create(outputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening destination file: %w", err))
}
defer jsonFile.Close()
_, err = jsonFile.Write(jsonData)
if err != nil {
log.Fatal(fmt.Errorf("error writing JSON data: %w", err))
}
},
}
// The `crit encode` command
var encodeCmd = &cobra.Command{
Use: "encode",
Short: "Convert JSON to binary image file",
Long: "Convert the input JSON to a CRIU image file.",
Run: func(cmd *cobra.Command, args []string) {
var (
inputFile, outputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}
if outputFilePath == "" {
outputFile = os.Stdout
} else {
outputFile, err = os.Create(outputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening output file: %w", err))
}
defer outputFile.Close()
}
c = crit.New(inputFile, outputFile,
inputDirPath, pretty, noPayload)
entryType, err := GetEntryTypeFromJSON(inputFile)
if err != nil {
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
// Convert JSON to Go struct
img, err := c.Parse(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error parsing JSON: %w", err))
}
// Write Go struct to binary image file
if err := c.Encode(img); err != nil {
log.Fatal(fmt.Errorf("error writing to file: %w", err))
}
},
}
// The `crit show` command
var showCmd = &cobra.Command{
Use: "show INPATH",
Short: "Convert binary image to human-readable JSON",
Long: "Convert the input binary image to human-readable JSON and print to stdout",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inputFilePath = args[0]
pretty = true
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}
c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
entryType, err := GetEntryTypeFromImg(inputFile)
if err != nil {
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
img, err := c.Decode(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))
}
jsonData, err := json.MarshalIndent(img, "", " ")
if err != nil {
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
fmt.Println(string(jsonData))
},
}
// The `crit info` command
var infoCmd = &cobra.Command{
Use: "info INPATH",
Short: "Show information about the image file",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inputFilePath = args[0]
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}
c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Info()
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))
}
jsonData, err := json.MarshalIndent(img, "", " ")
if err != nil {
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
fmt.Println(string(jsonData))
},
}
// The `crit x` command
var xCmd = &cobra.Command{
Use: "x DIR {ps|fd|mem|rss|sk}",
Short: "Explore the image directory",
Long: "Explore the image directory with one of (ps, fd, mem, rss, sk) options",
// Exactly two arguments are required:
// * Path of the input directory
// * Explore type
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
inputDirPath = args[0]
// We can use an empty interface to hold the
// returned object since we don't really care
// about the data itself, as long as we can
// marshal it into JSON and display it.
var xData any
var err error
c = crit.New(nil, nil,
inputDirPath, pretty, noPayload)
// Switch the explore type and call the handler.
switch args[1] {
case "ps":
xData, err = c.ExplorePs()
case "fd", "fds":
xData, err = c.ExploreFds()
case "mem", "mems":
xData, err = c.ExploreMems()
case "rss":
xData, err = c.ExploreRss()
case "sk":
xData, err = c.ExploreSk()
default:
err = errors.New("invalid explore type (supported: {ps|fd|mem|rss|sk})")
}
if err != nil {
log.Fatal(fmt.Errorf("error exploring directory: %w", err))
}
jsonData, err := json.MarshalIndent(xData, "", " ")
if err != nil {
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
fmt.Println(string(jsonData))
},
}
// Add all commands to the root command and configure flags
func Init() {
// Disable completion generation
rootCmd.CompletionOptions.DisableDefaultCmd = true
// Decode options
decodeCmd.Flags().StringVarP(&inputFilePath, "input", "i", "",
"Path to the binary image file")
decodeCmd.Flags().StringVarP(&outputFilePath, "output", "o", "",
"Path to the destination JSON file")
decodeCmd.Flags().BoolVar(&pretty, "pretty", false,
"Provide indented and multi-line JSON output")
rootCmd.AddCommand(decodeCmd)
// Encode options
encodeCmd.Flags().StringVarP(&inputFilePath, "input", "i", "",
"Path to the JSON file")
encodeCmd.Flags().StringVarP(&outputFilePath, "output", "o", "",
"Path to the destination image file")
rootCmd.AddCommand(encodeCmd)
// Show options
showCmd.Flags().BoolVar(&noPayload, "nopl", false,
"Do not show payload contents")
rootCmd.AddCommand(showCmd)
// Info and X commands
rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(xCmd)
}
func Run() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(fmt.Errorf("error running CLI: %w", err))
}
}
|