github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/operator_snapshot_inspect.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/nomad/helper/snapshot"
     9  	"github.com/posener/complete"
    10  )
    11  
    12  type OperatorSnapshotInspectCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *OperatorSnapshotInspectCommand) Help() string {
    17  	helpText := `
    18  Usage: nomad operator snapshot inspect [options] <file>
    19  
    20    Displays information about a snapshot file on disk.
    21  
    22    To inspect the file "backup.snap":
    23      $ nomad operator snapshot inspect backup.snap
    24  `
    25  	return strings.TrimSpace(helpText)
    26  }
    27  
    28  func (c *OperatorSnapshotInspectCommand) AutocompleteFlags() complete.Flags {
    29  	return complete.Flags{}
    30  }
    31  
    32  func (c *OperatorSnapshotInspectCommand) AutocompleteArgs() complete.Predictor {
    33  	return complete.PredictNothing
    34  }
    35  
    36  func (c *OperatorSnapshotInspectCommand) Synopsis() string {
    37  	return "Displays information about a Nomad snapshot file"
    38  }
    39  
    40  func (c *OperatorSnapshotInspectCommand) Name() string { return "operator snapshot inspect" }
    41  
    42  func (c *OperatorSnapshotInspectCommand) Run(args []string) int {
    43  	// Check that we either got no filename or exactly one.
    44  	if len(args) != 1 {
    45  		c.Ui.Error("This command takes one argument: <filename>")
    46  		c.Ui.Error(commandErrorText(c))
    47  		return 1
    48  	}
    49  
    50  	path := args[0]
    51  	f, err := os.Open(path)
    52  	if err != nil {
    53  		c.Ui.Error(fmt.Sprintf("Error opening snapshot file: %s", err))
    54  		return 1
    55  	}
    56  	defer f.Close()
    57  
    58  	meta, err := snapshot.Verify(f)
    59  	if err != nil {
    60  		c.Ui.Error(fmt.Sprintf("Error verifying snapshot: %s", err))
    61  		return 1
    62  	}
    63  
    64  	output := []string{
    65  		fmt.Sprintf("ID|%s", meta.ID),
    66  		fmt.Sprintf("Size|%d", meta.Size),
    67  		fmt.Sprintf("Index|%d", meta.Index),
    68  		fmt.Sprintf("Term|%d", meta.Term),
    69  		fmt.Sprintf("Version|%d", meta.Version),
    70  	}
    71  
    72  	c.Ui.Output(formatList(output))
    73  	return 0
    74  }