From 155a7c9111c790886111cb71955113b6f9e8f268 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Tue, 5 Mar 2024 17:48:26 -0700 Subject: [PATCH] move client initialization back to subcommands Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 4 ++-- pkg/cmd/attestation/attestation.go | 9 +++------ pkg/cmd/attestation/download/download.go | 16 ++++++++++------ pkg/cmd/attestation/inspect/inspect.go | 6 +++--- .../attestation/tufrootverify/tuf-root-verify.go | 6 +++--- pkg/cmd/attestation/verify/verify.go | 16 ++++++++++------ 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index ef51e99c9..ca292d3c8 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -29,8 +29,8 @@ type LiveClient struct { api apiClient } -func NewLiveClient() *LiveClient { - liveAPIClient := api.NewClientFromHTTP(http.DefaultClient) +func NewLiveClient(hc *http.Client) *LiveClient { + liveAPIClient := api.NewClientFromHTTP(hc) return &LiveClient{ host: "https://api.github.com", api: liveAPIClient, diff --git a/pkg/cmd/attestation/attestation.go b/pkg/cmd/attestation/attestation.go index 31f1ef6cb..a6f8674fd 100644 --- a/pkg/cmd/attestation/attestation.go +++ b/pkg/cmd/attestation/attestation.go @@ -1,7 +1,6 @@ package attestation import ( - "github.com/cli/cli/v2/pkg/cmd/attestation/artifact/oci" "github.com/cli/cli/v2/pkg/cmd/attestation/download" "github.com/cli/cli/v2/pkg/cmd/attestation/inspect" "github.com/cli/cli/v2/pkg/cmd/attestation/tufrootverify" @@ -29,11 +28,9 @@ func NewCmdAttestation(f *cmdutil.Factory) *cobra.Command { `, "`"), } - ociClient := oci.NewLiveClient() - - root.AddCommand(download.NewDownloadCmd(f, ociClient)) - root.AddCommand(inspect.NewInspectCmd(f, ociClient)) - root.AddCommand(verify.NewVerifyCmd(f, ociClient)) + root.AddCommand(download.NewDownloadCmd(f)) + root.AddCommand(inspect.NewInspectCmd(f)) + root.AddCommand(verify.NewVerifyCmd(f)) root.AddCommand(tufrootverify.NewTUFRootVerifyCmd(f)) return root diff --git a/pkg/cmd/attestation/download/download.go b/pkg/cmd/attestation/download/download.go index 47fcdf4f5..817f55d44 100644 --- a/pkg/cmd/attestation/download/download.go +++ b/pkg/cmd/attestation/download/download.go @@ -16,7 +16,7 @@ import ( "github.com/spf13/cobra" ) -func NewDownloadCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { +func NewDownloadCmd(f *cmdutil.Factory) *cobra.Command { opts := &Options{} downloadCmd := &cobra.Command{ Use: "download [ | oci://]", @@ -56,14 +56,9 @@ func NewDownloadCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { // If an error is returned, its message will be printed to the terminal // along with information about how use the command PreRunE: func(cmd *cobra.Command, args []string) error { - opts.APIClient = api.NewLiveClient() - // Create a logger for use throughout the download command opts.Logger = logging.NewLogger(f.IOStreams, false, opts.Verbose) - // Configure the live OCI client - opts.OCIClient = oc - // set the artifact path opts.ArtifactPath = args[0] @@ -77,6 +72,15 @@ func NewDownloadCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { // when RunE is used, the command usage will be printed // We only want to print the error, not usage Run: func(cmd *cobra.Command, args []string) { + hc, err := f.HttpClient() + if err != nil { + opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) + os.Exit(1) + } + opts.APIClient = api.NewLiveClient(hc) + + opts.OCIClient = oci.NewLiveClient() + if err := auth.IsHostSupported(); err != nil { opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) os.Exit(1) diff --git a/pkg/cmd/attestation/inspect/inspect.go b/pkg/cmd/attestation/inspect/inspect.go index e0d48bd39..0f14f2128 100644 --- a/pkg/cmd/attestation/inspect/inspect.go +++ b/pkg/cmd/attestation/inspect/inspect.go @@ -16,7 +16,7 @@ import ( "github.com/spf13/cobra" ) -func NewInspectCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { +func NewInspectCmd(f *cmdutil.Factory) *cobra.Command { opts := &Options{} inspectCmd := &cobra.Command{ Use: "inspect [ | oci://]", @@ -51,8 +51,6 @@ func NewInspectCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { $ gh attestation inspect oci:// --bundle `), PreRunE: func(cmd *cobra.Command, args []string) error { - opts.OCIClient = oc - // Create a logger for use throughout the inspect command opts.Logger = logging.NewDefaultLogger(f.IOStreams) @@ -73,6 +71,8 @@ func NewInspectCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { // when RunE is used, the command usage will be printed // We only want to print the error, not usage Run: func(cmd *cobra.Command, args []string) { + opts.OCIClient = oci.NewLiveClient() + if err := auth.IsHostSupported(); err != nil { opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) os.Exit(1) diff --git a/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go b/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go index 02e6f72d9..2ca57c340 100644 --- a/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go +++ b/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go @@ -18,10 +18,10 @@ func NewTUFRootVerifyCmd(f *cmdutil.Factory) *cobra.Command { var mirror string var root string var cmd = cobra.Command{ - Use: "tuf-root-verify --mirror --root ", - Args: cobra.ExactArgs(0), + Use: "tuf-root-verify --mirror --root ", + Args: cobra.ExactArgs(0), Hidden: true, - Short: "Verify the TUF repository from a provided TUF root", + Short: "Verify the TUF repository from a provided TUF root", Long: heredoc.Docf(` Verify a TUF repository with a local TUF root. diff --git a/pkg/cmd/attestation/verify/verify.go b/pkg/cmd/attestation/verify/verify.go index 96eb53437..0d7184a01 100644 --- a/pkg/cmd/attestation/verify/verify.go +++ b/pkg/cmd/attestation/verify/verify.go @@ -20,7 +20,7 @@ import ( var ErrNoMatchingSLSAPredicate = fmt.Errorf("the attestation does not have the expected SLSA predicate type: %s", SLSAPredicateType) -func NewVerifyCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { +func NewVerifyCmd(f *cmdutil.Factory) *cobra.Command { opts := &Options{} verifyCmd := &cobra.Command{ Use: "verify ", @@ -70,14 +70,9 @@ func NewVerifyCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { // If an error is returned, its message will be printed to the terminal // along with information about how use the command PreRunE: func(cmd *cobra.Command, args []string) error { - opts.APIClient = api.NewLiveClient() - // Create a logger for use throughout the verify command opts.Logger = logging.NewLogger(f.IOStreams, opts.Quiet, opts.Verbose) - // Configure the live OCI client - opts.OCIClient = oc - // set the artifact path opts.ArtifactPath = args[0] @@ -98,6 +93,15 @@ func NewVerifyCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command { // when RunE is used, the command usage will be printed // We only want to print the error, not usage Run: func(cmd *cobra.Command, args []string) { + hc, err := f.HttpClient() + if err != nil { + opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) + os.Exit(1) + } + opts.APIClient = api.NewLiveClient(hc) + + opts.OCIClient = oci.NewLiveClient() + if err := auth.IsHostSupported(); err != nil { opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) os.Exit(1)