Created
June 21, 2016 13:02
-
-
Save keegancsmith/d764cdbe470f358c66e34c2b2dc16deb to your computer and use it in GitHub Desktop.
Outputs to stdout the manifest for an image from dockerhub registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
) | |
func printManifest(image string) error { | |
parts := strings.SplitN(image, "@", 2) | |
if len(parts) < 2 { | |
return fmt.Errorf("missing tag in image name %s", image) | |
} | |
name, tag := parts[0], parts[1] | |
token, err := getToken("registry.docker.io", "repository:"+name+":pull") | |
if err != nil { | |
return err | |
} | |
req, err := http.NewRequest("GET", fmt.Sprintf("https://index.docker.io/v2/%s/manifests/%s", name, tag), nil) | |
if err != nil { | |
return err | |
} | |
req.Header.Add("Authorization", "Bearer "+token) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
io.Copy(os.Stdout, resp.Body) | |
return nil | |
} | |
func getToken(service, scope string) (string, error) { | |
// We need to get a token | |
v := url.Values{} | |
v.Set("service", service) | |
v.Set("scope", scope) | |
resp, err := http.Get("https://auth.docker.io/token?" + v.Encode()) | |
if err != nil { | |
return "", err | |
} | |
defer resp.Body.Close() | |
t := struct { | |
Token string `json:"token"` | |
}{} | |
err = json.NewDecoder(resp.Body).Decode(&t) | |
return t.Token, err | |
} | |
func main() { | |
for _, image := range os.Args[1:] { | |
err := printManifest(image) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment