forked from google/go-containerregistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
101 lines (92 loc) · 3.29 KB
/
publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
gb "go/build"
"log"
"os"
"path/filepath"
"strings"
"github.com/aaron-prindle/go-containerregistry/pkg/authn"
"github.com/aaron-prindle/go-containerregistry/pkg/ko/build"
"github.com/aaron-prindle/go-containerregistry/pkg/ko/publish"
"github.com/aaron-prindle/go-containerregistry/pkg/name"
)
func qualifyLocalImport(importpath, gopathsrc, pwd string) (string, error) {
if !strings.HasPrefix(pwd, gopathsrc) {
return "", fmt.Errorf("pwd (%q) must be on $GOPATH/src (%q) to support local imports", pwd, gopathsrc)
}
// Given $GOPATH/src and $PWD (which must be within $GOPATH/src), trim
// off $GOPATH/src/ from $PWD and append local importpath to get the
// fully-qualified importpath.
return filepath.Join(strings.TrimPrefix(pwd, gopathsrc+string(filepath.Separator)), importpath), nil
}
func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions) {
opt, err := gobuildOptions()
if err != nil {
log.Fatalf("error setting up builder options: %v", err)
}
b, err := build.NewGo(opt...)
if err != nil {
log.Fatalf("error creating go builder: %v", err)
}
for _, importpath := range importpaths {
if gb.IsLocalImport(importpath) {
// Qualify relative imports to their fully-qualified
// import path, assuming $PWD is within $GOPATH/src.
gopathsrc := filepath.Join(gb.Default.GOPATH, "src")
pwd, err := os.Getwd()
if err != nil {
log.Fatalf("error getting current working directory: %v", err)
}
importpath, err = qualifyLocalImport(importpath, gopathsrc, pwd)
if err != nil {
log.Fatal(err)
}
}
if !b.IsSupportedReference(importpath) {
log.Fatalf("importpath %q is not supported", importpath)
}
img, err := b.Build(importpath)
if err != nil {
log.Fatalf("error building %q: %v", importpath, err)
}
var pub publish.Interface
repoName := os.Getenv("KO_DOCKER_REPO")
var namer publish.Namer
if no.PreserveImportPaths {
namer = preserveImportPath
} else if no.BaseImportPaths {
namer = baseImportPaths
} else {
namer = packageWithMD5
}
if lo.Local || repoName == publish.LocalDomain {
pub = publish.NewDaemon(namer)
} else {
if _, err := name.NewRepository(repoName, name.WeakValidation); err != nil {
log.Fatalf("the environment variable KO_DOCKER_REPO must be set to a valid docker repository, got %v", err)
}
opts := []publish.Option{publish.WithAuthFromKeychain(authn.DefaultKeychain), publish.WithNamer(namer)}
pub, err = publish.NewDefault(repoName, opts...)
if err != nil {
log.Fatalf("error setting up default image publisher: %v", err)
}
}
if _, err := pub.Publish(img, importpath); err != nil {
log.Fatalf("error publishing %s: %v", importpath, err)
}
}
}