Skip to content

Commit

Permalink
logging for cmd construction
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 21, 2024
1 parent 7f17c11 commit 8e0bb2f
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 100 deletions.
12 changes: 6 additions & 6 deletions tooling/templatize/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package generate

import (
"log"
"context"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
func NewCommand() (*cobra.Command, error) {
opts := DefaultGenerationOptions()
cmd := &cobra.Command{
Use: "generate",
Short: "generate",
Long: "generate",
RunE: func(cmd *cobra.Command, args []string) error {
return generate(opts)
return generate(cmd.Context(), opts)
},
}
if err := BindGenerationOptions(opts, cmd); err != nil {
log.Fatal(err)
return nil, err
}
return cmd
return cmd, nil
}

func generate(opts *RawGenerationOptions) error {
func generate(ctx context.Context, opts *RawGenerationOptions) error {
validated, err := opts.Validate()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion tooling/templatize/cmd/generate/options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generate

import (
"context"
"fmt"
"path/filepath"
"testing"
Expand All @@ -27,6 +28,6 @@ func TestRawOptions(t *testing.T) {
Input: "../../testdata/pipeline.yaml",
Output: fmt.Sprintf("%s/pipeline.yaml", tmpdir),
}
assert.NoError(t, generate(opts))
assert.NoError(t, generate(context.Background(), opts))
testutil.CompareFileWithFixture(t, filepath.Join(tmpdir, "pipeline.yaml"))
}
12 changes: 6 additions & 6 deletions tooling/templatize/cmd/inspect/cmd.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package inspect

import (
"context"
"fmt"
"log"

"github.com/spf13/cobra"

options "github.com/Azure/ARO-HCP/tooling/templatize/cmd"
output "github.com/Azure/ARO-HCP/tooling/templatize/internal/utils"
)

func NewCommand() *cobra.Command {
func NewCommand() (*cobra.Command, error) {
opts := options.DefaultRolloutOptions()

format := "json"
Expand All @@ -19,17 +19,17 @@ func NewCommand() *cobra.Command {
Short: "inspect",
Long: "inspect",
RunE: func(cmd *cobra.Command, args []string) error {
return dumpConfig(format, opts)
return dumpConfig(cmd.Context(), format, opts)
},
}
if err := options.BindRolloutOptions(opts, cmd); err != nil {
log.Fatal(err)
return nil, err
}
cmd.Flags().StringVar(&format, "format", format, "output format (json, yaml)")
return cmd
return cmd, nil
}

func dumpConfig(format string, opts *options.RawRolloutOptions) error {
func dumpConfig(ctx context.Context, format string, opts *options.RawRolloutOptions) error {
validated, err := opts.Validate()
if err != nil {
return err
Expand Down
18 changes: 14 additions & 4 deletions tooling/templatize/cmd/pipeline/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/Azure/ARO-HCP/tooling/templatize/cmd/pipeline/run"
)

func NewCommand() *cobra.Command {
func NewCommand() (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "pipeline",
Short: "pipeline",
Expand All @@ -18,8 +18,18 @@ func NewCommand() *cobra.Command {
HiddenDefaultCmd: true,
},
}
cmd.AddCommand(run.NewCommand())
cmd.AddCommand(inspect.NewCommand())

return cmd
commands := []func() (*cobra.Command, error){
run.NewCommand,
inspect.NewCommand,
}
for _, newCmd := range commands {
c, err := newCmd()
if err != nil {
return nil, err
}
cmd.AddCommand(c)
}

return cmd, nil
}
15 changes: 4 additions & 11 deletions tooling/templatize/cmd/pipeline/inspect/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@ package inspect

import (
"context"
"log"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
func NewCommand() (*cobra.Command, error) {
opts := DefaultOptions()
cmd := &cobra.Command{
Use: "inspect",
Short: "inspect aspects of a pipeline.yaml file",
Long: "inspect aspects of a pipeline.yaml file",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
defer stop()

return runInspect(ctx, opts)
return runInspect(cmd.Context(), opts)
},
}
if err := BindOptions(opts, cmd); err != nil {
log.Fatal(err)
return nil, err
}
return cmd
return cmd, nil
}

func runInspect(ctx context.Context, opts *RawInspectOptions) error {
Expand Down
15 changes: 4 additions & 11 deletions tooling/templatize/cmd/pipeline/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@ package run

import (
"context"
"log"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
func NewCommand() (*cobra.Command, error) {
opts := DefaultOptions()
cmd := &cobra.Command{
Use: "run",
Short: "run a pipeline.yaml file towards an Azure Resourcegroup / AKS cluster",
Long: "run a pipeline.yaml file towards an Azure Resourcegroup / AKS cluster",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
defer stop()

return runPipeline(ctx, opts)
return runPipeline(cmd.Context(), opts)
},
}
if err := BindOptions(opts, cmd); err != nil {
log.Fatal(err)
return nil, err
}
return cmd
return cmd, nil
}

func runPipeline(ctx context.Context, opts *RawRunOptions) error {
Expand Down
6 changes: 3 additions & 3 deletions tooling/templatize/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.3.0
github.com/Masterminds/sprig/v3 v3.3.0
github.com/dusted-go/logging v1.3.0
github.com/go-logr/logr v1.4.2
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/gookit/goutil v0.6.17
github.com/microsoft/kiota-authentication-azure-go v1.1.0
github.com/microsoftgraph/msgraph-sdk-go v1.51.0
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -34,7 +36,6 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
10 changes: 4 additions & 6 deletions tooling/templatize/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dusted-go/logging v1.3.0 h1:SL/EH1Rp27oJQIte+LjWvWACSnYDTqNx5gZULin0XRY=
github.com/dusted-go/logging v1.3.0/go.mod h1:s58+s64zE5fxSWWZfp+b8ZV0CHyKHjamITGyuY1wzGg=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down Expand Up @@ -82,10 +86,6 @@ github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af h1:kmjWCqn2qkEml422C2
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/gookit/goutil v0.6.17 h1:SxmbDz2sn2V+O+xJjJhJT/sq1/kQh6rCJ7vLBiRPZjI=
github.com/gookit/goutil v0.6.17/go.mod h1:rSw1LchE1I3TDWITZvefoAC9tS09SFu3lHXLCV7EaEY=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
Expand Down Expand Up @@ -177,8 +177,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
Expand Down
45 changes: 41 additions & 4 deletions tooling/templatize/main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,69 @@
package main

import (
"context"
"log"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/go-logr/logr"
"github.com/spf13/cobra"

"github.com/dusted-go/logging/prettylog"

"github.com/Azure/ARO-HCP/tooling/templatize/cmd/generate"
"github.com/Azure/ARO-HCP/tooling/templatize/cmd/inspect"
"github.com/Azure/ARO-HCP/tooling/templatize/cmd/pipeline"
)

func main() {
// Create a root context with the logger and signal handling
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

var logVerbosity int

cmd := &cobra.Command{
Use: "templatize",
Short: "templatize",
Long: "templatize",
SilenceUsage: true,
TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
prettyHandler := prettylog.NewHandler(&slog.HandlerOptions{
Level: slog.Level(logVerbosity * -1),
AddSource: false,
ReplaceAttr: nil,
})
logger := logr.FromSlogHandler(prettyHandler)
ctx = logr.NewContext(ctx, logger)
cmd.SetContext(ctx)
},
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
}
cmd.AddCommand(generate.NewCommand())
cmd.AddCommand(inspect.NewCommand())
cmd.AddCommand(pipeline.NewCommand())

cmd.PersistentFlags().IntVarP(&logVerbosity, "verbosity", "v", 0, "set the verbosity level")

commands := []func() (*cobra.Command, error){
generate.NewCommand,
inspect.NewCommand,
pipeline.NewCommand,
}
for _, newCmd := range commands {
c, err := newCmd()
if err != nil {
log.Fatal(err)
}
cmd.AddCommand(c)
}

cmd.SetHelpCommand(&cobra.Command{Hidden: true})

if err := cmd.Execute(); err != nil {
log.Fatal(err)
logr.FromContextOrDiscard(ctx).Error(err, "command failed")
}
}
Loading

0 comments on commit 8e0bb2f

Please sign in to comment.