Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ko build ./... #644

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/modules-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
test:
name: Module Tests
strategy:
fail-fast: false
matrix:
go-version: [1.16.x, 1.17.x]
runs-on: 'ubuntu-latest'
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
if err := commands.Root.ExecuteContext(ctx); err != nil {
log.Fatal("error during command execution:", err)
log.Fatal("error during command execution: ", err)
}
}
35 changes: 33 additions & 2 deletions pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package commands

import (
"errors"
"fmt"
"path/filepath"

"github.com/google/ko/pkg/commands/options"
"github.com/spf13/cobra"
"golang.org/x/tools/go/packages"
)

// addBuild augments our CLI surface with build.
Expand Down Expand Up @@ -61,9 +64,13 @@ func addBuild(topLevel *cobra.Command) {
if err := options.Validate(po, bo); err != nil {
return fmt.Errorf("validating options: %w", err)
}

ctx := cmd.Context()

importpaths, err := importPaths(bo.WorkingDirectory, args)
if err != nil {
return fmt.Errorf("resolving import paths: %w", err)
}

bo.InsecureRegistry = po.InsecureRegistry
builder, err := makeBuilder(ctx, bo)
if err != nil {
Expand All @@ -74,7 +81,7 @@ func addBuild(topLevel *cobra.Command) {
return fmt.Errorf("error creating publisher: %w", err)
}
defer publisher.Close()
images, err := publishImages(ctx, args, publisher, builder)
images, err := publishImages(ctx, importpaths, publisher, builder)
if err != nil {
return fmt.Errorf("failed to publish images: %w", err)
}
Expand All @@ -88,3 +95,27 @@ func addBuild(topLevel *cobra.Command) {
options.AddBuildOptions(build, bo)
topLevel.AddCommand(build)
}

// importPaths resolves a list of importpath strings that may contain wildcards
// like "./cmd/..." or "./...", and returns the 'package main' packages matched
// by those importpaths.
func importPaths(dir string, args []string) ([]string, error) {
dir = filepath.Clean(dir)
if dir == "." {
dir = ""
}
ps, err := packages.Load(&packages.Config{Dir: dir, Mode: packages.NeedName}, args...)
if err != nil {
return nil, fmt.Errorf("loading packages: %w", err)
}
out := []string{}
for _, p := range ps {
if p.Name == "main" {
out = append(out, p.String())
}
}
if len(out) == 0 {
return nil, errors.New("no package main packages matched")
}
return out, nil
}