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
import (
"github.com/google/ko/pkg/commands"
"log"
"github.com/google/ko/pkg/commands"
"github.com/spf13/cobra"
)

View File

@ -42,7 +42,9 @@ func getBaseImage(s string) (v1.Image, error) {
ref = defaultBaseImage
}
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) {

View File

@ -15,13 +15,14 @@
package commands
import (
"context"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
@ -36,6 +37,31 @@ import (
"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) {
creationTime, err := getCreationTime()
if err != nil {
@ -104,6 +130,7 @@ func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *option
}
return publish.NewDefault(repoName,
publish.WithTransport(defaultTransport()),
publish.WithAuthFromKeychain(authn.DefaultKeychain),
publish.WithNamer(namer),
publish.WithTags(ta.Tags),
@ -121,7 +148,7 @@ func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *option
type resolvedFuture chan []byte
func resolveFilesToWriter(
ctx context.Context,
ctx context.Context,
builder *build.Caching,
publisher publish.Interface,
fo *options.FilenameOptions,

View File

@ -30,19 +30,23 @@ func addVersion(topLevel *cobra.Command) {
Use: "version",
Short: `Print ko version.`,
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 == "" {
i, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("could not determine build information")
return
return ""
}
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 do.Open()
}