Skip to content

Commit

Permalink
refactor: use functions instead of static definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Jul 7, 2024
1 parent 972750e commit a646939
Show file tree
Hide file tree
Showing 16 changed files with 698 additions and 568 deletions.
3 changes: 2 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func init() {

// CLI Main Entrypoint
func main() {
cmdErr := cmd.Execute()
rootCommand := cmd.RootCmd()
cmdErr := rootCommand.Execute()
if cmdErr != nil {
log.Fatal().Err(cmdErr).Msg("cli error")
}
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/charlievieth/fastwalk v1.0.4 // indirect
github.com/charlievieth/fastwalk v1.0.6 // indirect
github.com/cidverse/cidverseutils/exec v0.1.0 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
Expand Down Expand Up @@ -89,17 +89,17 @@ require (
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xanzy/go-gitlab v0.106.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Expand Down
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/charlievieth/fastwalk v1.0.4 h1:EG3y5L1XBa8VftvpONuQlfe5sNuf1xzGpm59bdgCDwo=
github.com/charlievieth/fastwalk v1.0.4/go.mod h1:JSfglY/gmL/rqsUS1NCsJTocB5n6sSl9ApAqif4CUbs=
github.com/charlievieth/fastwalk v1.0.6 h1:C7nXgxQIjEkpKWT1fbXGFzQiblwqq2ZsxrR0ohh5IRs=
github.com/charlievieth/fastwalk v1.0.6/go.mod h1:rV19+IF9Y2TYQNy4MqEk5M/spNHjKsA0i71yrsv2p4E=
github.com/cidverse/cidverseutils/ci v0.1.0 h1:vLxi+T3Oj2uJzO8KWLLRUzgEfC8y2hoS14AE9pCr2ew=
github.com/cidverse/cidverseutils/ci v0.1.0/go.mod h1:uOMGB2Kh/0HA936tlnzgBvrxv4hmXYeSbJxP383nHKU=
github.com/cidverse/cidverseutils/compress v0.1.0 h1:GrCp/50/Ltl3rn/WctHxY6lN+wgoms0qBqYpEbWmiZw=
Expand Down Expand Up @@ -227,14 +227,14 @@ golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2Uz
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand All @@ -245,8 +245,8 @@ golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -272,8 +272,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -282,8 +282,8 @@ golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -304,10 +304,10 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:Aqf0fiIdUQEj0Gn9mKFFXoQfTTEaNopWpfVyYADxiSg=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
44 changes: 44 additions & 0 deletions pkg/api/scriptapi/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package scriptapi

import (
"fmt"
"os"
"strconv"
"sync"
"text/tabwriter"

"github.com/cidverse/cid/pkg/app"
"github.com/cidverse/cid/pkg/common/api"
"github.com/cidverse/cid/pkg/core/rules"
"github.com/cidverse/cidverseutils/redact"
"github.com/spf13/cobra"
)

func execCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "lists all stages",
Run: func(cmd *cobra.Command, args []string) {
// find project directory and load config
projectDir := api.FindProjectDir()
cfg := app.Load(projectDir)
env := api.GetCIDEnvironment(cfg.Env, projectDir)

// print list
w := tabwriter.NewWriter(redact.NewProtectedWriter(nil, os.Stdout, &sync.Mutex{}, nil), 1, 1, 1, ' ', 0)
_, _ = fmt.Fprintln(w, "WORKFLOW\tSTAGE\tRULES\tACTIONS")
for _, wf := range cfg.Registry.Workflows {
for _, stage := range wf.Stages {
_, _ = fmt.Fprintln(w, wf.Name+"\t"+
stage.Name+"\t"+
rules.EvaluateRulesAsText(stage.Rules, rules.GetRuleContext(env))+"\t"+
strconv.Itoa(len(stage.Actions)))
}
}
_ = w.Flush()
},
}

return cmd
}
22 changes: 22 additions & 0 deletions pkg/api/scriptapi/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package scriptapi

import (
"os"

"github.com/spf13/cobra"
)

func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "stage",
Aliases: []string{"s"},
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
os.Exit(0)
},
}

cmd.AddCommand(execCmd())

return cmd
}
142 changes: 76 additions & 66 deletions pkg/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,91 @@ import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(actionRootCmd)
actionRootCmd.AddCommand(actionRunCmd)
actionRootCmd.AddCommand(actionListCmd)
actionRunCmd.Flags().StringArrayP("module", "m", []string{}, "limit execution to the specified module(s)")
}
func actionRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "action",
Aliases: []string{"a"},
Short: ``,
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
os.Exit(0)
},
}

cmd.AddCommand(actionListCmd())
cmd.AddCommand(actionRunCmd())

var actionRootCmd = &cobra.Command{
Use: "action",
Aliases: []string{"a"},
Short: ``,
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
os.Exit(0)
},
return cmd
}

var actionListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "lists all actions",
Run: func(cmd *cobra.Command, args []string) {
// find project directory and load config
projectDir := api.FindProjectDir()
cfg := app.Load(projectDir)
env := api.GetCIDEnvironment(cfg.Env, projectDir)
func actionListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "lists all actions",
Run: func(cmd *cobra.Command, args []string) {
// find project directory and load config
projectDir := api.FindProjectDir()
cfg := app.Load(projectDir)
env := api.GetCIDEnvironment(cfg.Env, projectDir)

// print list
w := tabwriter.NewWriter(redact.NewProtectedWriter(nil, os.Stdout, &sync.Mutex{}, nil), 1, 1, 1, ' ', 0)
_, _ = fmt.Fprintln(w, "REPOSITORY\tACTION\tTYPE\tSCOPE\tRULES\tDESCRIPTION")
for _, action := range cfg.Registry.Actions {
ruleEvaluation := "?/" + strconv.Itoa(len(action.Rules))
if action.Scope == catalog.ActionScopeProject {
ruleEvaluation = rules.EvaluateRulesAsText(action.Rules, rules.GetRuleContext(env))
// print list
w := tabwriter.NewWriter(redact.NewProtectedWriter(nil, os.Stdout, &sync.Mutex{}, nil), 1, 1, 1, ' ', 0)
_, _ = fmt.Fprintln(w, "REPOSITORY\tACTION\tTYPE\tSCOPE\tRULES\tDESCRIPTION")
for _, action := range cfg.Registry.Actions {
ruleEvaluation := "?/" + strconv.Itoa(len(action.Rules))
if action.Scope == catalog.ActionScopeProject {
ruleEvaluation = rules.EvaluateRulesAsText(action.Rules, rules.GetRuleContext(env))
}

_, _ = fmt.Fprintln(w, action.Repository+"\t"+
action.Name+"\t"+
string(action.Type)+"\t"+
string(action.Scope)+"\t"+
ruleEvaluation+"\t"+
strings.Replace(action.Description, "\n", "", -1))
}
_ = w.Flush()
},
}

_, _ = fmt.Fprintln(w, action.Repository+"\t"+
action.Name+"\t"+
string(action.Type)+"\t"+
string(action.Scope)+"\t"+
ruleEvaluation+"\t"+
strings.Replace(action.Description, "\n", "", -1))
}
_ = w.Flush()
},
return cmd
}

var actionRunCmd = &cobra.Command{
Use: "run",
Aliases: []string{"r"},
Short: "runs the actions specified in the arguments",
Run: func(cmd *cobra.Command, args []string) {
modules, _ := cmd.Flags().GetStringArray("module")
func actionRunCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Aliases: []string{"r"},
Short: "runs the actions specified in the arguments",
Run: func(cmd *cobra.Command, args []string) {
modules, _ := cmd.Flags().GetStringArray("module")

// find project directory and load config
projectDir := api.FindProjectDir()
cfg := app.Load(projectDir)
env := api.GetCIDEnvironment(cfg.Env, projectDir)

// find project directory and load config
projectDir := api.FindProjectDir()
cfg := app.Load(projectDir)
env := api.GetCIDEnvironment(cfg.Env, projectDir)
// actions
actionName := args[0]

// pass action
action := cfg.Registry.FindAction(actionName)
if action == nil {
log.Error().Str("action", actionName).Msg("action is not known")
os.Exit(1)
}
act := catalog.WorkflowAction{
ID: action.Repository + "/" + action.Name,
Rules: []catalog.WorkflowRule{},
Config: nil,
Module: nil,
}
workflowrun.RunWorkflowAction(cfg, &act, env, projectDir, modules)
},
}

// actions
actionName := args[0]
cmd.Flags().StringArrayP("module", "m", []string{}, "limit execution to the specified module(s)")

// pass action
action := cfg.Registry.FindAction(actionName)
if action == nil {
log.Error().Str("action", actionName).Msg("action is not known")
os.Exit(1)
}
act := catalog.WorkflowAction{
ID: action.Repository + "/" + action.Name,
Rules: []catalog.WorkflowRule{},
Config: nil,
Module: nil,
}
workflowrun.RunWorkflowAction(cfg, &act, env, projectDir, modules)
},
return cmd
}
Loading

0 comments on commit a646939

Please sign in to comment.