...

Source file src/github.com/sigstore/timestamp-authority/cmd/timestamp-cli/app/inspect.go

Documentation: github.com/sigstore/timestamp-authority/cmd/timestamp-cli/app

     1  //
     2  // Copyright 2022 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package app
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/digitorus/timestamp"
    24  	"github.com/sigstore/timestamp-authority/cmd/timestamp-cli/app/format"
    25  	"github.com/sigstore/timestamp-authority/pkg/log"
    26  	"github.com/spf13/cobra"
    27  	"github.com/spf13/viper"
    28  )
    29  
    30  func addInspectFlags(cmd *cobra.Command) {
    31  	cmd.Flags().Var(NewFlagValue(fileFlag, ""), "timestamp", "path to timestamp response to inspect")
    32  	cmd.MarkFlagRequired("timestamp") //nolint:errcheck
    33  }
    34  
    35  type inspectCmdOutput struct {
    36  	TimestampResponse timestamp.Timestamp
    37  }
    38  
    39  func (t *inspectCmdOutput) String() string {
    40  	return fmt.Sprintf("%+v", t.TimestampResponse)
    41  }
    42  
    43  var inspectCmd = &cobra.Command{
    44  	Use:   "inspect",
    45  	Short: "Inspect timestamp",
    46  	Long:  "Inspect the signed timestamp response.",
    47  	PreRunE: func(cmd *cobra.Command, args []string) error {
    48  		if err := viper.BindPFlags(cmd.Flags()); err != nil {
    49  			log.CliLogger.Fatal("Error initializing cmd line args: ", err)
    50  		}
    51  		return nil
    52  	},
    53  	Run: format.WrapCmd(func(args []string) (interface{}, error) {
    54  		tsr := viper.GetString("timestamp")
    55  		tsrBytes, err := os.ReadFile(filepath.Clean(tsr))
    56  		if err != nil {
    57  			return nil, fmt.Errorf("Error reading request from TSR file: %w", err)
    58  		}
    59  
    60  		ts, err := timestamp.ParseResponse(tsrBytes)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  
    65  		return &inspectCmdOutput{
    66  			TimestampResponse: *ts,
    67  		}, nil
    68  	}),
    69  }
    70  
    71  func init() {
    72  	initializePFlagMap()
    73  	addInspectFlags(inspectCmd)
    74  	rootCmd.AddCommand(inspectCmd)
    75  }
    76  

View as plain text