1
0
mirror of https://github.com/ko-build/ko.git synced 2025-01-20 18:28:32 +02:00

Set UA to something ko-specific (#116)

This commit is contained in:
jonjohnsonjr 2019-12-13 15:08:52 -08:00 committed by GitHub
parent 4ff72e36de
commit d24b60a88f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 9 deletions

View File

@ -15,9 +15,10 @@
package main package main
import ( import (
"github.com/google/ko/pkg/commands"
"log" "log"
"github.com/google/ko/pkg/commands"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View File

@ -42,7 +42,9 @@ func getBaseImage(s string) (v1.Image, error) {
ref = defaultBaseImage ref = defaultBaseImage
} }
log.Printf("Using base %s for %s", ref, s) log.Printf("Using base %s for %s", ref, s)
return remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain)) return remote.Image(ref,
remote.WithTransport(defaultTransport()),
remote.WithAuthFromKeychain(authn.DefaultKeychain))
} }
func getCreationTime() (*v1.Time, error) { func getCreationTime() (*v1.Time, error) {

View File

@ -15,13 +15,14 @@
package commands package commands
import ( import (
"context"
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"os" "os"
"sync" "sync"
@ -36,6 +37,31 @@ import (
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
) )
func defaultTransport() http.RoundTripper {
return &useragentTransport{
inner: http.DefaultTransport,
useragent: ua(),
}
}
type useragentTransport struct {
useragent string
inner http.RoundTripper
}
func ua() string {
if v := version(); v != "" {
return "ko/" + v
}
return "ko"
}
// RoundTrip implements http.RoundTripper
func (ut *useragentTransport) RoundTrip(in *http.Request) (*http.Response, error) {
in.Header.Set("User-Agent", ut.useragent)
return ut.inner.RoundTrip(in)
}
func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) { func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
creationTime, err := getCreationTime() creationTime, err := getCreationTime()
if err != nil { if err != nil {
@ -104,6 +130,7 @@ func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *option
} }
return publish.NewDefault(repoName, return publish.NewDefault(repoName,
publish.WithTransport(defaultTransport()),
publish.WithAuthFromKeychain(authn.DefaultKeychain), publish.WithAuthFromKeychain(authn.DefaultKeychain),
publish.WithNamer(namer), publish.WithNamer(namer),
publish.WithTags(ta.Tags), publish.WithTags(ta.Tags),

View File

@ -30,19 +30,23 @@ func addVersion(topLevel *cobra.Command) {
Use: "version", Use: "version",
Short: `Print ko version.`, Short: `Print ko version.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
version() v := version()
if v == "" {
fmt.Println("could not determine build information")
} else {
fmt.Println(v)
}
}, },
}) })
} }
func version() { func version() string {
if Version == "" { if Version == "" {
i, ok := debug.ReadBuildInfo() i, ok := debug.ReadBuildInfo()
if !ok { if !ok {
fmt.Println("could not determine build information") return ""
return
} }
Version = i.Main.Version Version = i.Main.Version
} }
fmt.Println(Version) return Version
} }

View File

@ -89,6 +89,7 @@ func NewDefault(base string, options ...Option) (Interface, error) {
return nil, err return nil, err
} }
} }
return do.Open() return do.Open()
} }