forked from sigstore/rekor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
188 lines (159 loc) · 4.71 KB
/
root.go
File metadata and controls
188 lines (159 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Copyright 2021 The Sigstore Authors.
//
// 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 app
import (
"errors"
"fmt"
"net/url"
"os"
"strings"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/util"
)
var rootCmd = &cobra.Command{
Use: "rekor",
Short: "Rekor CLI",
Long: `Rekor command line interface tool`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return initConfig(cmd)
},
}
// Execute runs the base CLI
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.rekor.yaml)")
rootCmd.PersistentFlags().Bool("store_tree_state", true, "whether to store tree state in between invocations for additional verification")
rootCmd.PersistentFlags().Var(&urlFlag{url: "https://api.rekor.dev"}, "rekor_server", "Server address:port")
rootCmd.PersistentFlags().Var(&formatFlag{format: "default"}, "format", "Command output format")
rootCmd.PersistentFlags().String("api-key", "", "API key for api.rekor.dev")
// these are bound here and not in PreRun so that all child commands can use them
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initConfig(cmd *cobra.Command) error {
viper.SetEnvPrefix("rekor")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
// manually set all values provided from viper through pflag validation logic
var changedFlags []string
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && viper.IsSet(f.Name) {
changedFlags = append(changedFlags, f.Name)
}
})
for _, flag := range changedFlags {
val := viper.Get(flag)
if err := cmd.Flags().Set(flag, fmt.Sprintf("%v", val)); err != nil {
return err
}
}
if viper.GetString("config") != "" {
viper.SetConfigFile(viper.GetString("config"))
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
return err
}
viper.AddConfigPath(home)
viper.SetConfigName(".rekor")
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
default:
return err
}
} else if viper.GetString("format") == "default" {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
return nil
}
func GetRekorClient(rekorServerURL string) (*client.Rekor, error) {
url, err := url.Parse(rekorServerURL)
if err != nil {
return nil, err
}
rt := httptransport.New(url.Host, client.DefaultBasePath, []string{url.Scheme})
rt.Consumers["application/yaml"] = util.YamlConsumer()
rt.Consumers["application/x-pem-file"] = runtime.TextConsumer()
rt.Producers["application/yaml"] = util.YamlProducer()
if viper.GetString("api-key") != "" {
rt.DefaultAuthentication = httptransport.APIKeyAuth("apiKey", "query", viper.GetString("api-key"))
}
return client.New(rt, strfmt.Default), nil
}
type urlFlag struct {
url string
}
func (u *urlFlag) String() string {
return u.url
}
func (u *urlFlag) Set(s string) error {
if s == "" {
return errors.New("flag must be specified")
}
url, err := url.Parse(s)
if err != nil {
return fmt.Errorf("malformed URL: %w", err)
}
if !url.IsAbs() {
return fmt.Errorf("URL must be absolute, got %s", s)
}
lowercaseScheme := strings.ToLower(url.Scheme)
if lowercaseScheme != "http" && lowercaseScheme != "https" {
return fmt.Errorf("URL must be a valid HTTP or HTTPS URL, got %s", s)
}
u.url = s
return nil
}
func (u *urlFlag) Type() string {
return "url"
}
type formatFlag struct {
format string
}
func (f *formatFlag) String() string {
return f.format
}
func (f *formatFlag) Set(s string) error {
choices := map[string]struct{}{"default": {}, "json": {}}
if s == "" {
f.format = "default"
return nil
}
if _, ok := choices[s]; ok {
f.format = s
return nil
}
return fmt.Errorf("invalid flag value: %s, valid values are [default, json]", s)
}
func (f *formatFlag) Type() string {
return "format"
}