Skip to content

Commit

Permalink
release: Support -o commit on oc adm release extract --git
Browse files Browse the repository at this point in the history
Allows someone to get the list of repos and commits from a release
quickly. Will be used for automation around tagging a commit within
a repo.
  • Loading branch information
smarterclayton committed Mar 26, 2020
1 parent 24e2741 commit 2725cfa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions contrib/completions/bash/oc
Original file line number Diff line number Diff line change
Expand Up @@ -5262,6 +5262,10 @@ _oc_adm_release_extract()
flags+=("--max-per-registry=")
two_word_flags+=("--max-per-registry")
local_nonpersistent_flags+=("--max-per-registry=")
flags+=("--output=")
two_word_flags+=("--output")
two_word_flags+=("-o")
local_nonpersistent_flags+=("--output=")
flags+=("--registry-config=")
two_word_flags+=("--registry-config")
two_word_flags+=("-a")
Expand Down
4 changes: 4 additions & 0 deletions contrib/completions/zsh/oc
Original file line number Diff line number Diff line change
Expand Up @@ -5404,6 +5404,10 @@ _oc_adm_release_extract()
flags+=("--max-per-registry=")
two_word_flags+=("--max-per-registry")
local_nonpersistent_flags+=("--max-per-registry=")
flags+=("--output=")
two_word_flags+=("--output")
two_word_flags+=("-o")
local_nonpersistent_flags+=("--output=")
flags+=("--registry-config=")
two_word_flags+=("--registry-config")
two_word_flags+=("-a")
Expand Down
47 changes: 40 additions & 7 deletions pkg/cli/admin/release/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func NewExtract(f kcmdutil.Factory, parentName string, streams genericclioptions
flags.StringVar(&o.Command, "command", o.Command, "Specify 'oc' or 'openshift-install' to extract the client for your operating system.")
flags.StringVar(&o.CommandOperatingSystem, "command-os", o.CommandOperatingSystem, "Override which operating system command is extracted (mac, windows, linux). You map specify '*' to extract all tool archives.")
flags.StringVar(&o.FileDir, "dir", o.FileDir, "The directory on disk that file:// images will be copied under.")

flags.StringVarP(&o.Output, "output", "o", o.Output, "Output format. Supports 'commit' when used with '--git'.")
return cmd
}

Expand All @@ -92,6 +94,8 @@ type ExtractOptions struct {
SecurityOptions imagemanifest.SecurityOptions
ParallelOptions imagemanifest.ParallelOptions

Output string

From string

Tools bool
Expand Down Expand Up @@ -144,6 +148,10 @@ func (o *ExtractOptions) Run() error {
sources++
}

if len(o.Output) > 0 && len(o.GitExtractDir) == 0 {
return fmt.Errorf("--output is only supported with --git")
}

switch {
case sources > 1:
return fmt.Errorf("only one of --tools, --command, --file, or --git may be specified")
Expand Down Expand Up @@ -244,6 +252,12 @@ func (o *ExtractOptions) Run() error {
}

func (o *ExtractOptions) extractGit(dir string) error {
switch o.Output {
case "commit", "":
default:
return fmt.Errorf("the only supported option for --output is 'commit'")
}

if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
Expand Down Expand Up @@ -291,14 +305,33 @@ func (o *ExtractOptions) extractGit(dir string) error {
return
}

klog.V(2).Infof("Checkout %s from %s ...", commit, repo)
buf.Reset()
if err := extractedRepo.CheckoutCommit(repo, commit); err != nil {
once.Do(func() { hadErrors = true })
fmt.Fprintf(o.ErrOut, "error: checking out commit for %s: %v\n%s\n", repo, err, buf.String())
return
switch o.Output {
case "commit":
klog.V(2).Infof("Checkout %s from %s ...", commit, repo)
buf.Reset()
ok, err := extractedRepo.VerifyCommit(repo, commit)
if err != nil {
once.Do(func() { hadErrors = true })
fmt.Fprintf(o.ErrOut, "error: could not find commit %s in %s: %v\n%s\n", commit, repo, err, buf.String())
return
}
if !ok {
once.Do(func() { hadErrors = true })
fmt.Fprintf(o.ErrOut, "error: could not find commit %s in %s", commit, repo)
return
}
fmt.Fprintf(o.Out, "%s %s\n", extractedRepo.path, commit)

case "":
klog.V(2).Infof("Checkout %s from %s ...", commit, repo)
buf.Reset()
if err := extractedRepo.CheckoutCommit(repo, commit); err != nil {
once.Do(func() { hadErrors = true })
fmt.Fprintf(o.ErrOut, "error: checking out commit for %s: %v\n%s\n", repo, err, buf.String())
return
}
fmt.Fprintf(o.Out, "%s\n", extractedRepo.path)
}
fmt.Fprintf(o.Out, "%s\n", extractedRepo.path)
})
}
})
Expand Down
15 changes: 15 additions & 0 deletions pkg/cli/admin/release/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ func (g *git) basename() string {
return filepath.Base(g.path)
}

func (g *git) VerifyCommit(repo, commit string) (bool, error) {
_, err := g.exec("rev-parse", commit)
if err == nil {
return true, nil
}

// try to fetch by URL
klog.V(4).Infof("failed to find commit, fetching: %v", err)
if err := ensureFetchedRemoteForRepo(g, repo); err != nil {
return false, err
}
_, err = g.exec("rev-parse", commit)
return err == nil, nil
}

func (g *git) CheckoutCommit(repo, commit string) error {
_, err := g.exec("checkout", commit)
if err == nil {
Expand Down

0 comments on commit 2725cfa

Please sign in to comment.