Skip to content
Snippets Groups Projects
Commit ab3ccd6b authored by Luke Hinds's avatar Luke Hinds
Browse files

Get Leaf command

Gets a specific leaf by the leaf Index

`rekor getleaf --index ($index)
parent 4178382e
No related branches found
No related tags found
No related merge requests found
/*
Copyright © 2020 Luke Hinds <lhinds@redhat.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/google/trillian"
"github.com/projectrekor/rekor-cli/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type getLeafResponse struct {
Leaf *trillian.GetLeavesByIndexResponse
}
// getleafCmd represents the getleaf command
var getleafCmd = &cobra.Command{
Use: "getleaf",
Short: "Rekor Get Leaf Command",
Long: `Get Leaf entry by Index`,
Run: func(cmd *cobra.Command, args []string) {
log := log.Logger
rekorServer := viper.GetString("rekor_server")
leafIndex := viper.GetString("leafIndex")
u := rekorServer + "/api/v1/getleaf"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
request, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil {
log.Fatal(err)
}
request.URL.RawQuery += fmt.Sprintf("leafindex=%s", leafIndex)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
resp := getLeafResponse{}
if err := json.Unmarshal(content, &resp); err != nil {
log.Fatal(err)
}
log.Info("Leaf content", resp.Leaf)
},
}
func init() {
rootCmd.AddCommand(getleafCmd)
getleafCmd.PersistentFlags().String("index", "", "Leaf Index")
viper.BindPFlag("linkfile", getleafCmd.PersistentFlags().Lookup("index"))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment