Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: --bearer-token option to set Authorization headers. #2

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions cmd/korrel8rcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"os"

rtclient "github.com/go-openapi/runtime/client"
httptransport "github.com/go-openapi/runtime/client"
"github.com/korrel8r/client/pkg/build"
"github.com/korrel8r/client/pkg/swagger/client"
"github.com/spf13/cobra"
Expand All @@ -26,8 +26,9 @@ var (

// Global Flags
output = EnumFlag("yaml", "json-pretty", "json")
korrel8rURL = rootCmd.PersistentFlags().StringP("url", "u", "", "URL of remote korrel8r service (you can also set the KORREL8R_URL environment variable)")
korrel8rURL = rootCmd.PersistentFlags().StringP("url", "u", os.Getenv("KORREL8R_URL"), "URL of remote korrel8r service, environment KORREL8R_URL")
insecure = rootCmd.PersistentFlags().BoolP("insecure", "k", false, "Insecure connection, skip TLS verification.")
bearerToken = rootCmd.PersistentFlags().String("bearer-token", "", "Bearer token for Authorization header, environment KORREL8R_BEARER_TOKEN")
)

func main() {
Expand All @@ -48,9 +49,6 @@ func init() {
}

func newClient() *client.RESTAPI {
if *korrel8rURL == "" {
*korrel8rURL = os.Getenv("KORREL8R_URL")
}
if *korrel8rURL == "" {
check(errors.New("Either command line flag --url or environment variable KORREL8R_URL must be set. "))
}
Expand All @@ -59,15 +57,18 @@ func newClient() *client.RESTAPI {
if u.Path == "" || u.Path == "/" {
u.Path = client.DefaultBasePath
}
var rt *rtclient.Runtime
var transport *httptransport.Runtime
if *insecure {
tlsClient, err := rtclient.TLSClient(rtclient.TLSClientOptions{InsecureSkipVerify: true})
tlsClient, err := httptransport.TLSClient(httptransport.TLSClientOptions{InsecureSkipVerify: *insecure})
check(err)
rt = rtclient.NewWithClient(u.Host, u.Path, []string{u.Scheme}, tlsClient)
transport = httptransport.NewWithClient(u.Host, u.Path, []string{u.Scheme}, tlsClient)
} else {
rt = rtclient.New(u.Host, u.Path, []string{u.Scheme})
transport = httptransport.New(u.Host, u.Path, []string{u.Scheme})
}
if *bearerToken != "" {
transport.DefaultAuthentication = httptransport.BearerToken(*bearerToken)
}
return client.New(rt, nil)
return client.New(transport, nil)
}

func check(err error) {
Expand Down
Loading