88 "fmt"
99 "io/fs"
1010 "log/slog"
11+ "net/http"
1112 "os"
1213 "os/exec"
1314 "path/filepath"
@@ -22,6 +23,7 @@ import (
2223 githubkeychain "github.com/google/go-containerregistry/pkg/authn/github"
2324 "github.com/google/go-containerregistry/pkg/name"
2425 "github.com/google/go-containerregistry/pkg/v1/remote"
26+ "github.com/google/go-containerregistry/pkg/v1/remote/transport"
2527 "github.com/google/go-github/v72/github"
2628 "github.com/spf13/pflag"
2729 "golang.org/x/mod/semver"
@@ -590,12 +592,32 @@ func (c *command) pluginReleasesURL(releaseName string) string {
590592
591593func fetchRegistryImageAndImageID (plugin * plugin.Plugin ) (string , string , error ) {
592594 identity := plugin .Identity
593- imageName := fmt .Sprintf ("ghcr.io/%s/plugins-%s-%s:%s" , release .GithubOwnerBufbuild , identity .Owner (), identity .Plugin (), plugin .PluginVersion )
595+ imageName := fmt .Sprintf (
596+ "ghcr.io/%s/plugins-%s-%s:%s" ,
597+ release .GithubOwnerBufbuild ,
598+ identity .Owner (),
599+ identity .Plugin (),
600+ plugin .PluginVersion ,
601+ )
602+ registryImage , imageID , err := fetchImageAndImageIDFromRegistry (
603+ imageName ,
604+ remote .WithAuthFromKeychain (githubkeychain .Keychain ),
605+ )
606+ if err != nil {
607+ if isImageNotFoundError (err ) {
608+ return "" , "" , nil
609+ }
610+ return "" , "" , err
611+ }
612+ return registryImage , imageID , nil
613+ }
614+
615+ func fetchImageAndImageIDFromRegistry (imageName string , options ... remote.Option ) (string , string , error ) {
594616 parsedName , err := name .ParseReference (imageName )
595617 if err != nil {
596618 return "" , "" , err
597619 }
598- remoteImage , err := remote .Image (parsedName , remote . WithAuthFromKeychain ( githubkeychain . Keychain ) )
620+ remoteImage , err := remote .Image (parsedName , options ... )
599621 if err != nil {
600622 return "" , "" , err
601623 }
@@ -609,3 +631,11 @@ func fetchRegistryImageAndImageID(plugin *plugin.Plugin) (string, string, error)
609631 }
610632 return fmt .Sprintf ("%s@%s" , imageName , remoteDigest .String ()), manifest .Config .Digest .String (), nil
611633}
634+
635+ func isImageNotFoundError (err error ) bool {
636+ var transportErr * transport.Error
637+ if ! errors .As (err , & transportErr ) {
638+ return false
639+ }
640+ return transportErr .StatusCode == http .StatusNotFound
641+ }
0 commit comments