Skip to content

Commit

Permalink
Refactor and use Cobra (#14)
Browse files Browse the repository at this point in the history
* refactor to use Cobra

* backport meta tag parser fix

* use correct flag for dynamic tasks

* add help messages to commands
  • Loading branch information
waquidvp authored Jun 19, 2022
1 parent 5edd796 commit 75de3c5
Show file tree
Hide file tree
Showing 12 changed files with 910 additions and 817 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download

COPY *.go ./
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -v -o nomad-pipeline

FROM scratch
Expand Down
72 changes: 72 additions & 0 deletions cmd/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/hyperbadger/nomad-pipeline/pkg/controller"
)

var agentCmd = &cobra.Command{
Use: "agent",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var agentInitCmd = &cobra.Command{
Use: "init",
Short: "Initialize job with nomad-pipeline hooks",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
pc := controller.NewPipelineController(cPath)

update := pc.Init()
if update {
err := pc.UpdateJob()
if err != nil {
log.Fatalf("error updating job: %v", err)
}
}
},
}

var agentWaitCmd = &cobra.Command{
Use: "wait",
Short: "Wait for previous task group(s)",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
pc := controller.NewPipelineController(cPath)

pc.Wait(args)
},
}

var agentNextCmd = &cobra.Command{
Use: "next",
Short: "Trigger next task group(s)",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
pc := controller.NewPipelineController(cPath)

update := pc.Next(args, dynamicTasks)
if update {
err := pc.UpdateJob()
if err != nil {
log.Fatalf("error updating job: %v", err)
}
}
},
}

var dynamicTasks string

func init() {
agentNextCmd.Flags().StringVar(&dynamicTasks, "dynamic-tasks", "", "glob of task files relative to alloc dir")

agentCmd.AddCommand(agentInitCmd)
agentCmd.AddCommand(agentWaitCmd)
agentCmd.AddCommand(agentNextCmd)

rootCmd.AddCommand(agentCmd)
}
29 changes: 29 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "nomad-pipeline",
Short: "Run pipeline-style workloads in Nomad",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var cPath string

func init() {
rootCmd.PersistentFlags().StringVar(&cPath, "config", "config.yaml", "path to config")
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion examples/dependencies.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ job "dependencies" {

config {
image = var.image
args = ["-init"]
args = ["agent", "init"]

extra_hosts = var.docker_extra_hosts
auth_soft_fail = true
Expand Down
2 changes: 1 addition & 1 deletion examples/dynamic-job.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ job "dynamic" {

config {
image = var.image
args = ["-init"]
args = ["agent", "init"]

extra_hosts = var.docker_extra_hosts
auth_soft_fail = true
Expand Down
2 changes: 1 addition & 1 deletion examples/fan-out-fan-in.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ job "fan-out-fan-in" {

config {
image = var.image
args = ["-init"]
args = ["agent", "init"]

extra_hosts = var.docker_extra_hosts
auth_soft_fail = true
Expand Down
2 changes: 1 addition & 1 deletion examples/happy-job.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ job "happy" {

config {
image = var.image
args = ["-init"]
args = ["agent", "init"]

extra_hosts = var.docker_extra_hosts
auth_soft_fail = true
Expand Down
2 changes: 1 addition & 1 deletion examples/leader-task-group.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ job "leader-task-group" {

config {
image = var.image
args = ["-init"]
args = ["agent", "init"]

extra_hosts = var.docker_extra_hosts
auth_soft_fail = true
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/hyperbadger/nomad-pipeline
go 1.18

require (
github.com/hashicorp/nomad/api v0.0.0-20220525183220-49ae238604f4
github.com/hashicorp/nomad/api v0.0.0-20220617091522-08811312cc87
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -13,7 +14,9 @@ require (
github.com/hashicorp/cronexpr v1.1.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
)
17 changes: 13 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk=
Expand All @@ -9,8 +10,10 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/nomad/api v0.0.0-20220525183220-49ae238604f4 h1:4+zKZ4d9VniGHPunwAjfDv2ZEJF2ooAZAWwJMKKEgFs=
github.com/hashicorp/nomad/api v0.0.0-20220525183220-49ae238604f4/go.mod h1:b/AoT79m3PEpb6tKCFKva/M+q1rKJNUk5mdu1S8DymM=
github.com/hashicorp/nomad/api v0.0.0-20220617091522-08811312cc87 h1:OIzs4HfBl9gRRTldyOtRLhEJPcz/Sa9Rg0pw+MCLJzU=
github.com/hashicorp/nomad/api v0.0.0-20220617091522-08811312cc87/go.mod h1:b/AoT79m3PEpb6tKCFKva/M+q1rKJNUk5mdu1S8DymM=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand All @@ -21,14 +24,20 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c h1:aFV+BgZ4svzjfabn8ERpuB4JI4N6/rdy1iusx77G3oU=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 75de3c5

Please sign in to comment.