move client initialization back to subcommands

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-05 17:48:26 -07:00
parent 2923d106ff
commit 155a7c9111
6 changed files with 31 additions and 26 deletions

View file

@ -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,

View file

@ -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

View file

@ -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 [<file path> | oci://<OCI image URI>]",
@ -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)

View file

@ -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 [<file path> | oci://<OCI image URI>]",
@ -51,8 +51,6 @@ func NewInspectCmd(f *cmdutil.Factory, oc oci.Client) *cobra.Command {
$ gh attestation inspect oci://<my-OCI-image> --bundle <path-to-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)

View file

@ -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 <mirror-url> --root <root.json>",
Args: cobra.ExactArgs(0),
Use: "tuf-root-verify --mirror <mirror-url> --root <root.json>",
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.

View file

@ -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 <artifact-path-or-url>",
@ -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)