1
0
mirror of https://github.com/ko-build/ko.git synced 2025-03-11 14:09:45 +02:00
ko-build/pkg/commands/publisher.go

70 lines
1.9 KiB
Go
Raw Normal View History

2019-03-14 14:23:47 -04:00
// 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 commands
2019-03-14 14:23:47 -04:00
import (
2019-11-10 01:23:09 +08:00
"context"
2019-03-14 14:23:47 -04:00
"fmt"
gb "go/build"
"github.com/google/go-containerregistry/pkg/name"
2019-03-21 19:07:56 -04:00
"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
"golang.org/x/tools/go/packages"
2019-03-14 14:23:47 -04:00
)
func qualifyLocalImport(importpath string) (string, error) {
cfg := &packages.Config{
Mode: packages.NeedName,
}
pkgs, err := packages.Load(cfg, importpath)
if err != nil {
return "", err
}
if len(pkgs) != 1 {
return "", fmt.Errorf("found %d local packages, expected 1", len(pkgs))
2019-03-14 14:23:47 -04:00
}
return pkgs[0].PkgPath, nil
2019-03-14 14:23:47 -04:00
}
2019-11-10 01:23:09 +08:00
func publishImages(ctx context.Context, importpaths []string, pub publish.Interface, b build.Interface) (map[string]name.Reference, error) {
2019-03-14 14:23:47 -04:00
imgs := make(map[string]name.Reference)
for _, importpath := range importpaths {
if gb.IsLocalImport(importpath) {
var err error
importpath, err = qualifyLocalImport(importpath)
2019-03-14 14:23:47 -04:00
if err != nil {
return nil, err
2019-03-14 14:23:47 -04:00
}
}
if !b.IsSupportedReference(importpath) {
return nil, fmt.Errorf("importpath %q is not supported", importpath)
2019-03-14 14:23:47 -04:00
}
2019-11-10 01:23:09 +08:00
img, err := b.Build(ctx, importpath)
2019-03-14 14:23:47 -04:00
if err != nil {
return nil, fmt.Errorf("error building %q: %v", importpath, err)
2019-03-14 14:23:47 -04:00
}
ref, err := pub.Publish(img, importpath)
if err != nil {
return nil, fmt.Errorf("error publishing %s: %v", importpath, err)
2019-03-14 14:23:47 -04:00
}
imgs[importpath] = ref
}
return imgs, nil
2019-03-14 14:23:47 -04:00
}