Skip to content

Commit

Permalink
feat: add logging with apex
Browse files Browse the repository at this point in the history
  • Loading branch information
alexppg committed Mar 28, 2021
1 parent b8ce1d2 commit c22a455
Show file tree
Hide file tree
Showing 28 changed files with 330 additions and 106 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ linters:
- gosec
- interfacer
- unconvert
- dupl
- goconst
- maligned
- depguard
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static: | $(GOLANGCI-LINT) $(GOPHERBADGER)
$(GOPHERBADGER) -md="README.md"

unit:
go test ./... -cover -v
go test ./... -cover

PLATFORMS := linux-amd64 linux-386 darwin-amd64 windows-amd64 windows-386
temp = $(subst -, ,$@)
Expand Down
44 changes: 29 additions & 15 deletions cmd/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/apex/log"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/chartutil"

Expand All @@ -18,44 +20,56 @@ func chart(cmd *cobra.Command, args []string) {
var driver string = "kind"
var chartName string = args[0]
var supportedDrivers = []string{driver}
var lint string = "set -e\nhelm lint"
var debug bool
var err error
var configuration config.ParticleConfiguration
var lint string = "set -e\nhelm lint"

debug, _ = cmd.Flags().GetBool("debug")

logger := helpers.GetLogger(debug)

logger.Info("Begin initialization")

driver, err = cmd.Flags().GetString("driver")
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

if !helpers.StringInSlice(supportedDrivers, driver) {
fmt.Printf("\"%s\" is not a valid value for the flag \"driver\".\n", driver)
os.Exit(1)
logger.Error(fmt.Sprintf("\"%s\" is not a valid value for the flag \"driver\"\n", driver))
}

// Check if the chart's directory exists and create it if not
_, err = os.Stat(chartName)
if !os.IsNotExist(err) {
fmt.Println("Chart already exists.")
os.Exit(1)
err = errors.New("Chart already exists")
helpers.CheckGenericError(logger, err)
}

err = os.MkdirAll(chartName, 0755)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

// Create chart
if _, err = chartutil.Create(chartName, ""); err != nil {
fmt.Println("Could not create chart: ")
fmt.Println(err)
os.Exit(1)
err = errors.New("Could not create chart")
helpers.CheckGenericError(logger, err)
}

fmt.Println("Chart created.")

configuration.Driver.Name = driver
configuration.Provisioner.Name = helm
configuration.Lint = lint
configuration.Verifier.Name = "helm"
configuration.Verifier.Name = helm

logger.WithFields(log.Fields{
"driver": driver,
"provisioner": helm,
"verifier": helm,
"lint": strings.Replace(lint, "\n", " && ", -1),
}).Debug("Configuration to create")

err = config.CreateConfiguration(chartName, scenario, configuration)
helpers.CheckGenericError(err)
fmt.Println("Particle initialized.")
helpers.CheckGenericError(logger, err)

logger.Info("Initialization finished")
}

// chartCmd represents the chart command
Expand Down
24 changes: 22 additions & 2 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/spf13/cobra"

"github.com/little-angry-clouds/particle/internal/cli"
Expand All @@ -13,12 +16,29 @@ func cleanup(cmd *cobra.Command, args []string) {
var scenario string = "default"
var configuration config.ParticleConfiguration
var err error
var debug bool

debug, _ = cmd.Flags().GetBool("debug")

logger := helpers.GetLogger(debug)

logger.Info("Begin cleanup")

configuration, err = config.ReadConfiguration(scenario)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

logger.WithFields(log.Fields{
"driver": configuration.Driver.Name,
"provisioner": configuration.Provisioner.Name,
"verifier": configuration.Verifier.Name,
"lint": strings.Replace(configuration.Lint, "\n", " && ", -1),
}).Debug("Configuration to use")

err = cli.Cleanup(scenario, configuration)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)
err = cli.Cleanup(scenario, configuration, logger)

logger.Info("Cleanup finished")
}

// cleanupCmd represents the cleanup command
Expand Down
24 changes: 22 additions & 2 deletions cmd/converge.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/spf13/cobra"

"github.com/little-angry-clouds/particle/internal/cli"
Expand All @@ -13,12 +16,29 @@ func converge(cmd *cobra.Command, args []string) {
var scenario string = "default"
var configuration config.ParticleConfiguration
var err error
var debug bool

debug, _ = cmd.Flags().GetBool("debug")

logger := helpers.GetLogger(debug)

logger.Info("Begin converge")

configuration, err = config.ReadConfiguration(scenario)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

logger.WithFields(log.Fields{
"driver": configuration.Driver.Name,
"provisioner": configuration.Provisioner.Name,
"verifier": configuration.Verifier.Name,
"lint": strings.Replace(configuration.Lint, "\n", " && ", -1),
}).Debug("Configuration to use")

err = cli.Converge(scenario, configuration)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)
err = cli.Converge(scenario, configuration, logger)

logger.Info("Converge finished")
}

// convergeCmd represents the converge command
Expand Down
24 changes: 22 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/spf13/cobra"

"github.com/little-angry-clouds/particle/internal/cli"
Expand All @@ -13,12 +16,29 @@ func create(cmd *cobra.Command, args []string) {
var scenario string = "default"
var configuration config.ParticleConfiguration
var err error
var debug bool

debug, _ = cmd.Flags().GetBool("debug")

logger := helpers.GetLogger(debug)

logger.Info("Begin create")

configuration, err = config.ReadConfiguration(scenario)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

logger.WithFields(log.Fields{
"driver": configuration.Driver.Name,
"provisioner": configuration.Provisioner.Name,
"verifier": configuration.Verifier.Name,
"lint": strings.Replace(configuration.Lint, "\n", " && ", -1),
}).Debug("Configuration to use")

err = cli.Create(scenario, configuration)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)
err = cli.Create(scenario, configuration, logger)

logger.Info("Create finished")
}

// createCmd represents the create command
Expand Down
24 changes: 22 additions & 2 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/spf13/cobra"

"github.com/little-angry-clouds/particle/internal/cli"
Expand All @@ -13,12 +16,29 @@ func destroy(cmd *cobra.Command, args []string) {
var scenario string = "default"
var configuration config.ParticleConfiguration
var err error
var debug bool

debug, _ = cmd.Flags().GetBool("debug")

logger := helpers.GetLogger(debug)

logger.Info("Begin destroy")

configuration, err = config.ReadConfiguration(scenario)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

logger.WithFields(log.Fields{
"driver": configuration.Driver.Name,
"provisioner": configuration.Provisioner.Name,
"verifier": configuration.Verifier.Name,
"lint": strings.Replace(configuration.Lint, "\n", " && ", -1),
}).Debug("Configuration to use")

err = cli.Destroy(scenario, configuration)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)
err = cli.Destroy(scenario, configuration, logger)

logger.Info("Destroy finished")
}

// destroyCmd represents the destroy command
Expand Down
25 changes: 22 additions & 3 deletions cmd/lint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/spf13/cobra"

"github.com/little-angry-clouds/particle/internal/cli"
Expand All @@ -13,18 +16,34 @@ func lint(cmd *cobra.Command, args []string) {
var scenario string = "default"
var err error
var configuration config.ParticleConfiguration
var debug bool

debug, _ = cmd.Flags().GetBool("debug")

logger := helpers.GetLogger(debug)

logger.Info("Begin linting")

configuration, err = config.ReadConfiguration(scenario)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

logger.WithFields(log.Fields{
"driver": configuration.Driver.Name,
"provisioner": configuration.Provisioner.Name,
"verifier": configuration.Verifier.Name,
"lint": strings.Replace(configuration.Lint, "\n", " && ", -1),
}).Debug("Configuration to use")

err = cli.Lint(scenario, configuration)
helpers.CheckGenericError(err)
helpers.CheckGenericError(logger, err)

logger.Info("Linting finished")
}

// lintCmd represents the lint command
var lintCmd = &cobra.Command{
Use: "lint",
Short: "Lint command executes external linters",
Short: "Lint executes external linters",
Run: lint,
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "particle",
Short: "A brief description of your application",
Short: "Particle is a project designed to aid in the development and testing of kubernetes resources",
}

func Execute() {
rootCmd.PersistentFlags().Bool("debug", false, "when true gets more verbose logs")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
20 changes: 0 additions & 20 deletions cmd/sideEffect.go

This file was deleted.

Loading

0 comments on commit c22a455

Please sign in to comment.