| 1 |
// Package cmd contains a CLI to interact with TPM. |
| 2 |
package cmd |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
|
| 9 |
"github.com/spf13/cobra" |
| 10 |
"google.golang.org/protobuf/encoding/prototext" |
| 11 |
) |
| 12 |
|
| 13 |
// RootCmd is the entrypoint for gotpm. |
| 14 |
var RootCmd = &cobra.Command{ |
| 15 |
Use: "gotpm", |
| 16 |
Long: `Command line tool for the go-tpm TSS |
| 17 |
|
| 18 |
This tool allows performing TPM2 operations from the command line. |
| 19 |
See the per-command documentation for more information.`, |
| 20 |
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { |
| 21 |
if quiet && verbose { |
| 22 |
return fmt.Errorf("cannot specify both --quiet and --verbose") |
| 23 |
} |
| 24 |
cmd.SilenceUsage = true |
| 25 |
return nil |
| 26 |
}, |
| 27 |
} |
| 28 |
|
| 29 |
var ( |
| 30 |
quiet bool |
| 31 |
verbose bool |
| 32 |
) |
| 33 |
|
| 34 |
func init() { |
| 35 |
RootCmd.PersistentFlags().BoolVar(&quiet, "quiet", false, |
| 36 |
"print nothing if command is successful") |
| 37 |
RootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, |
| 38 |
"print additional info to stdout") |
| 39 |
hideHelp(RootCmd) |
| 40 |
} |
| 41 |
|
| 42 |
func messageOutput() io.Writer { |
| 43 |
if quiet { |
| 44 |
return io.Discard |
| 45 |
} |
| 46 |
return os.Stdout |
| 47 |
} |
| 48 |
|
| 49 |
func debugOutput() io.Writer { |
| 50 |
if verbose { |
| 51 |
return os.Stdout |
| 52 |
} |
| 53 |
return io.Discard |
| 54 |
} |
| 55 |
|
| 56 |
// Default Text Marshalling options |
| 57 |
var marshalOptions = prototext.MarshalOptions{ |
| 58 |
Multiline: true, |
| 59 |
EmitASCII: true, |
| 60 |
} |
| 61 |
var unmarshalOptions = prototext.UnmarshalOptions{} |