Skip to content

Commit ae66a55

Browse files
committed
Merge branch 'master' of github.com:AliceO2Group/Control into feature/OCONF-184-coconut-conf-history-command
2 parents 53791bc + 0009161 commit ae66a55

10 files changed

Lines changed: 3174 additions & 3333 deletions

File tree

coconut/cmd/template_list.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
package cmd
2626

2727
import (
28-
"github.com/spf13/cobra"
2928
"github.com/AliceO2Group/Control/coconut/control"
29+
"github.com/spf13/cobra"
3030
)
3131

3232
// templateListCmd represents the template list command
@@ -35,10 +35,30 @@ var templateListCmd = &cobra.Command{
3535
Aliases: []string{"list", "ls", "l"},
3636
Short: "list available workflow templates",
3737
Long: `The template list command shows a list of available workflow templates.
38-
These workflow templates can then be loaded to create an environment.`,
38+
These workflow templates can then be loaded to create an environment.
39+
40+
` + "`coconut templ list` " + `can be called with
41+
1) a combination of the ` + "`--repo` " + `, ` + "`--revision` " + `, ` + "`--all-branches` " + `, ` + "`--all-tags`" + `flags, or with
42+
2) an argument in the form of [repo-pattern]@[revision-pattern], where the patterns are globbing.`,
43+
Example:
44+
` * ` + "`coconut templ list`" + ` lists templates from the HEAD of master for all git repositories
45+
* ` + "`coconut templ list *AliceO2Group*`" + ` lists all templates coming from the HEAD of master of git repositories that match the pattern *AliceO2Group*
46+
* ` + "`coconut templ list *@v*`" + ` lists templates coming from revisions matching the ` + "`v*`" + `pattern for all git repositories
47+
* ` + "`coconut templ list --repo=*AliceO2Group*`" + ` lists all templates coming from the HEAD of master of git repositories that match the pattern *AliceO2Group*
48+
* ` + "`coconut templ list --revision=dev*`" + ` lists templates coming from revisions matching the ` + "`dev*`" + `pattern for all git repositories
49+
* ` + "`coconut templ list --repo=*gitlab.cern.ch* --revision=master`" + ` lists templates for revisions ` + "`master`" + `for git repositories matching ` + "`*gitlab.cern.ch*`" + `
50+
* ` + "`coconut templ list --all-branches`" + ` lists templates from all branches for all git repositories
51+
* ` + "`coconut templ list --repo=*github.com* --all-tags`" + ` lists templates from all tags for git repositories which match the *github.com* pattern
52+
* ` + "`coconut templ list --revision=5c7f1c1fded1b87243998579ed876c8035a08377 `" + ` lists templates from the commit corresponding to the hash for all git repositories`,
53+
3954
Run: control.WrapCall(control.ListWorkflowTemplates),
4055
}
4156

4257
func init() {
4358
templateCmd.AddCommand(templateListCmd)
44-
}
59+
60+
templateListCmd.Flags().StringP("repository", "r", "*", "repositories to list templates from")
61+
templateListCmd.Flags().StringP("revision", "i", "master", "revisions (branches/tags) to list templates from")
62+
templateListCmd.Flags().BoolP("all-branches", "b", false, "list templates from all branches")
63+
templateListCmd.Flags().BoolP("all-tags", "t", false, "list templates from all tags")
64+
}

coconut/control/control.go

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/xlab/treeprint"
3434
"io"
3535
"os"
36+
"regexp"
3637
"strconv"
3738
"strings"
3839
"time"
@@ -490,13 +491,90 @@ func QueryRoles(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Command,
490491

491492
// ListWorkflowTemplates lists the available workflow templates and the git repo on which they reside.
492493
func ListWorkflowTemplates(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Command, args []string, o io.Writer) (err error) {
493-
if len(args) != 0 {
494-
err = errors.New(fmt.Sprintf("accepts no arg(s), received %d", len(args)))
495-
return err
494+
repoPattern := "*"
495+
revisionPattern := "master"
496+
allBranches := false
497+
allTags := false
498+
499+
if len(args) == 0 {
500+
repoPattern, err = cmd.Flags().GetString("repository")
501+
if err != nil {
502+
return
503+
}
504+
if len(repoPattern) == 0 {
505+
err = errors.New("cannot query for an empty repo")
506+
return
507+
}
508+
509+
revisionPattern, err = cmd.Flags().GetString("revision")
510+
if err != nil {
511+
return
512+
}
513+
514+
if len(revisionPattern) == 0 {
515+
err = errors.New("cannot query for an empty revision")
516+
return
517+
}
518+
519+
allBranches, err = cmd.Flags().GetBool("all-branches")
520+
if err != nil {
521+
return
522+
}
523+
524+
allTags, err = cmd.Flags().GetBool("all-tags")
525+
if err != nil {
526+
return
527+
}
528+
529+
if allBranches || allTags {
530+
if revisionPattern != "master" {
531+
fmt.Fprintln(o, "Ignoring `--all-{branches,tags}` flags, as a valid revision has been specified")
532+
allBranches = false
533+
allTags = false
534+
}
535+
}
536+
537+
} else if len(args) == 1 { // If we have an argument, give priority over the flags
538+
simpleRepoRegex := regexp.MustCompile("\\A[^@]+\\z")
539+
repoRevisionRegex := regexp.MustCompile("\\A[^@]+@[^@]+\\z")
540+
541+
if simpleRepoRegex.MatchString(args[0]) {
542+
repoPattern = args[0]
543+
} else if repoRevisionRegex.MatchString(args[0]) {
544+
slicedArgument := strings.Split(args[0], "@")
545+
repoPattern = slicedArgument[0]
546+
revisionPattern = slicedArgument[1]
547+
} else {
548+
err = errors.New("arguments should be in the form of [repo-pattern](@[revision-pattern])")
549+
return
550+
}
551+
552+
if checkForFlag, _ := cmd.Flags().GetString("repository"); checkForFlag != "*" { // "*" comes from the flag's default value
553+
fmt.Fprintln(o, "Ignoring `--repo` flag, as a valid argument has been passed ")
554+
}
555+
556+
if checkForFlag, _ := cmd.Flags().GetString("revision"); checkForFlag != "master" {
557+
fmt.Fprintln(o, "Ignoring `--revision` flag, as a valid argument has been passed")
558+
}
559+
560+
if checkForFlag, _ := cmd.Flags().GetBool("all-branches"); checkForFlag != false {
561+
fmt.Fprintln(o, "Ignoring `--all-branches` flag, as a valid argument has been passed")
562+
}
563+
564+
if checkForFlag, _ := cmd.Flags().GetBool("all-tags"); checkForFlag != false {
565+
fmt.Fprintln(o, "Ignoring `--all-tags` flag, as a valid argument has been passed")
566+
}
567+
568+
} else {
569+
err = errors.New(fmt.Sprintf("expecting one argument or a combination of the --repo and --revision flags, %d args received", len(args)))
570+
571+
return
496572
}
497573

574+
498575
var response *pb.GetWorkflowTemplatesReply
499-
response, err = rpc.GetWorkflowTemplates(cxt, &pb.GetWorkflowTemplatesRequest{}, grpc.EmptyCallOption{})
576+
response, err = rpc.GetWorkflowTemplates(cxt, &pb.GetWorkflowTemplatesRequest{RepoPattern: repoPattern, RevisionPattern: revisionPattern,
577+
AllBranches: allBranches, AllTags: allTags}, grpc.EmptyCallOption{})
500578
if err != nil {
501579
return err
502580
}
@@ -506,20 +584,30 @@ func ListWorkflowTemplates(cxt context.Context, rpc *coconut.RpcClient, cmd *cob
506584
fmt.Fprintln(o, "No templates found.")
507585
} else {
508586
var prevRepo string
587+
var prevRevision string
509588
aTree := treeprint.New()
510589
aTree.SetValue("Available templates in loaded configuration:")
511590

591+
revBranch := treeprint.New()
592+
512593
for _, tmpl := range templates {
513-
if prevRepo != tmpl.GetRepo() {
514-
fmt.Fprintln(o, aTree.String())
594+
if prevRepo != tmpl.GetRepo() { // Create the root node of the tree w/ the repo name
595+
fmt.Fprint(o, aTree.String())
515596
aTree = treeprint.New()
516597
aTree.SetValue(blue(tmpl.GetRepo()))
517598
prevRepo = tmpl.GetRepo()
599+
prevRevision = "" // Reinitialize the previous revision
600+
}
601+
602+
if prevRevision != tmpl.GetRevision() { // Create the first leaf of the root node w/ the revision name
603+
revBranch = aTree.AddBranch(tmpl.GetRevision)
604+
revBranch.SetValue(yellow("[revision] " + tmpl.GetRevision())) // Otherwise the pointer value was set as the branch's value
605+
prevRevision = tmpl.GetRevision()
518606
}
519-
aTree.AddNode(tmpl.GetTemplate())
607+
revBranch.AddNode(tmpl.GetTemplate())
520608
}
521609

522-
fmt.Fprintln(o, aTree.String())
610+
fmt.Fprint(o, aTree.String())
523611
}
524612

525613
return nil

0 commit comments

Comments
 (0)