github.com/jaypipes/ghw@v0.21.1/cmd/ghw-snapshot/command/read.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package command
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"os"
    13  
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/jaypipes/ghw"
    17  	ghwcontext "github.com/jaypipes/ghw/pkg/context"
    18  )
    19  
    20  var readCmd = &cobra.Command{
    21  	Use:   "read",
    22  	Short: "Reads a new ghw snapshot",
    23  	RunE:  doRead,
    24  }
    25  
    26  // doRead reads a ghw snapshot from the input snapshot path argument
    27  func doRead(cmd *cobra.Command, args []string) error {
    28  	if len(args) != 1 {
    29  		return errors.New("supply a single argument with the filepath to the snapshot you wish to read")
    30  	}
    31  	inPath := args[0]
    32  	if _, err := os.Stat(inPath); err != nil {
    33  		return err
    34  	}
    35  	os.Setenv("GHW_SNAPSHOT_PATH", inPath)
    36  	ctx := ghwcontext.New()
    37  
    38  	return ctx.Do(func() error {
    39  		info, err := ghw.Host()
    40  		fmt.Println(info.String())
    41  		return err
    42  	})
    43  }
    44  
    45  func init() {
    46  	rootCmd.AddCommand(readCmd)
    47  }