forked from shipwright-io/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Local source code feature (using bundle images)
Reference shipwright-io/build#717 Add `bundle` package that contains convenience code to push a local source code directory as a bundle image to a container registry. Add new flags for container image and local directory settings.
- Loading branch information
1 parent
6a99070
commit dacd79f
Showing
6 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package bundle | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
progressbar "github.com/schollz/progressbar/v3" | ||
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources/sources" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
// Push bundles the provided local directory into a container image and pushes | ||
// it to the given registry. | ||
func Push(ctx context.Context, io *genericclioptions.IOStreams, localDirectory string, targetImage string) error { | ||
tag, err := name.NewTag(targetImage) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
auth, err := authn.DefaultKeychain.Resolve(tag.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
updates := make(chan v1.Update, 1) | ||
done := make(chan struct{}, 1) | ||
go func() { | ||
var progress *progressbar.ProgressBar | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
|
||
case <-done: | ||
return | ||
|
||
case update, ok := <-updates: | ||
if !ok { | ||
return | ||
} | ||
|
||
if progress == nil { | ||
progress = progressbar.NewOptions(int(update.Total), | ||
progressbar.OptionSetWriter(io.ErrOut), | ||
progressbar.OptionEnableColorCodes(true), | ||
progressbar.OptionShowBytes(true), | ||
progressbar.OptionSetWidth(15), | ||
progressbar.OptionSetPredictTime(false), | ||
progressbar.OptionSetDescription("Uploading local source..."), | ||
progressbar.OptionSetTheme(progressbar.Theme{ | ||
Saucer: "[green]=[reset]", | ||
SaucerHead: "[green]>[reset]", | ||
SaucerPadding: " ", | ||
BarStart: "[", | ||
BarEnd: "]"}), | ||
progressbar.OptionClearOnFinish(), | ||
) | ||
defer progress.Close() | ||
} | ||
|
||
progress.ChangeMax64(update.Total) | ||
_ = progress.Set64(update.Complete) | ||
} | ||
} | ||
}() | ||
|
||
fmt.Fprintf(io.Out, "Bundling %q as %q ...\n", localDirectory, targetImage) | ||
_, err = sources.Bundle( | ||
tag, | ||
localDirectory, | ||
remote.WithAuth(auth), | ||
remote.WithProgress(updates), | ||
) | ||
|
||
done <- struct{}{} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters