Skip to content

Commit

Permalink
clean output and add verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed Oct 12, 2021
1 parent b27af96 commit 663808e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 87 deletions.
10 changes: 5 additions & 5 deletions cmd/filegather.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/uselagoon/lagoon-facts-app/gatherers"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"os"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-facts-app/gatherers"
)

var gatheredFileName string
Expand All @@ -21,7 +22,6 @@ var filegatherCmd = &cobra.Command{

var facts []gatherers.GatheredFact


//we can just unmarshall the file data into the facts ...
if gatheredFileName == "" {
fmt.Errorf("Filename should be passed as argument")
Expand All @@ -32,7 +32,7 @@ var filegatherCmd = &cobra.Command{
_ = json.Unmarshal([]byte(file), &facts)

if !dryRun {
err := gatherers.Writefacts(projectName, environment, facts)
err := gatherers.Writefacts(projectName, environmentName, facts)
if err != nil {
log.Println(err.Error())
}
Expand All @@ -41,7 +41,7 @@ var filegatherCmd = &cobra.Command{
if dryRun {
if facts != nil {
log.Println("---- Dry run ----")
log.Printf("Would post the follow facts to '%s:%s'", projectName, environment)
log.Printf("Would post the follow facts to '%s:%s'", projectName, environmentName)
s, _ := json.MarshalIndent(facts, "", "\t")
log.Println(string(s))
}
Expand Down
42 changes: 29 additions & 13 deletions cmd/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package cmd

import (
"encoding/json"
"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-facts-app/gatherers"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uselagoon/lagoon-facts-app/gatherers"
)

var projectName string
var environment string
var environmentName string
var gatherer bool
var dryRun bool

Expand All @@ -21,16 +24,22 @@ var gatherCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) {
//get the basic env vars
if projectName == "" {
projectName = os.Getenv("LAGOON_PROJECT")
project, exists := os.LookupEnv("LAGOON_PROJECT")
if exists {
projectName = strings.Replace(project, "_", "-", -1)
}
}
if projectName == "" {
projectName = os.Getenv("LAGOON_SAFE_PROJECT")
project, exists := os.LookupEnv("LAGOON_SAFE_PROJECT")
if exists {
projectName = strings.Replace(project, "_", "-", -1)
}
}
if environment == "" {
environment = os.Getenv("LAGOON_GIT_BRANCH")
if environmentName == "" {
environmentName = os.Getenv("LAGOON_GIT_BRANCH")
}

if environment == "" || projectName == "" {
if environmentName == "" || projectName == "" {
log.Fatalf("PROJECT OR ENVIRONMENT NOT SET - exiting")
os.Exit(1)
}
Expand Down Expand Up @@ -60,13 +69,20 @@ var gatherCmd = &cobra.Command{
log.Println(err.Error())
continue
}
facts = append(facts, gatheredFacts...)
if verbose := viper.Get("verbose"); verbose == true {
for _, f := range gatheredFacts {
if f.Value != "" {
log.Printf("Registering %s", f.Name)
facts = append(facts, gatheredFacts...)
}
}
}
}
}
}

if !dryRun {
err := gatherers.Writefacts(projectName, environment, facts)
err := gatherers.Writefacts(projectName, environmentName, facts)

if err != nil {
log.Println(err.Error())
Expand All @@ -76,7 +92,7 @@ var gatherCmd = &cobra.Command{
if dryRun {
if facts != nil {
log.Println("---- Dry run ----")
log.Printf("Would post the follow facts to '%s:%s'", projectName, environment)
log.Printf("Would post the follow facts to '%s:%s'", projectName, environmentName)
s, _ := json.MarshalIndent(facts, "", "\t")
log.Println(string(s))
}
Expand All @@ -87,8 +103,8 @@ var gatherCmd = &cobra.Command{
var GatherCommand = gatherCmd

func init() {
gatherCmd.PersistentFlags().StringVarP(&projectName, "project-name", "p", "", "The Lagoon project name")
gatherCmd.PersistentFlags().StringVarP(&environment, "environment-name", "e", "", "The Lagoon environment name")
gatherCmd.PersistentFlags().StringVarP(&projectName, "project", "p", "", "The Lagoon project name")
gatherCmd.PersistentFlags().StringVarP(&environmentName, "environment", "e", "", "The Lagoon environment name")
gatherCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "run gathers and print to screen without running write methods")
rootCmd.AddCommand(gatherCmd)
}
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string
var argStatic bool
var argDynamic bool
var verbose bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -57,6 +59,8 @@ func init() {
viper.BindPFlag("static", rootCmd.PersistentFlags().Lookup("static"))
rootCmd.PersistentFlags().BoolVar(&argDynamic, "dynamic", false, "Run only dynamic gatherers")
viper.BindPFlag("dynamic", rootCmd.PersistentFlags().Lookup("dynamic"))
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Run in verbose")
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down
11 changes: 6 additions & 5 deletions gatherers/ApplicationGatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package gatherers
import (
"encoding/json"
"fmt"
"github.com/uselagoon/lagoon-facts-app/utils"
"log"

"github.com/uselagoon/lagoon-facts-app/utils"
)

type applicationGatherer struct {
Expand All @@ -16,9 +17,9 @@ func (p *applicationGatherer) GetGathererCmdType() string {
}

type composerShowOutput struct {
Name string `json:"name,omitempty"`
Versions []string `json:"versions,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
Versions []string `json:"versions,omitempty"`
Description string `json:"description,omitempty"`
}

func (p *applicationGatherer) AppliesToEnvironment() bool {
Expand All @@ -45,7 +46,7 @@ func (p *applicationGatherer) AppliesToEnvironment() bool {
Value: result.Versions[0],
Source: "application_via_composer",
Description: result.Description,
Category: Application,
Category: Application,
})
}

Expand Down
23 changes: 12 additions & 11 deletions gatherers/DrushGatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"encoding/json"
"fmt"
"log"

"github.com/uselagoon/lagoon-facts-app/utils"
)

type drushGatherer struct {
DrupalVersion string
DrushVersion string
DrushVersion string
}

func (p *drushGatherer) GetGathererCmdType() string {
Expand All @@ -19,7 +20,7 @@ func (p *drushGatherer) GetGathererCmdType() string {
func (p *drushGatherer) AppliesToEnvironment() bool {
err, stdOut, stdErr := utils.Shellout("drush status --format=json 2> /dev/null")
if err != nil {
log.Printf("Drush gatherer cannot be applied: %v", stdErr)
log.Printf("Drush status gatherer cannot be applied: %v", stdErr)
return false
}

Expand All @@ -38,22 +39,22 @@ func (p *drushGatherer) AppliesToEnvironment() bool {
func (p *drushGatherer) GatherFacts() ([]GatheredFact, error) {
return []GatheredFact{
{
Name: "drupal-version",
Value: p.DrupalVersion,
Source: "drush_status",
Name: "drupal-version",
Value: p.DrupalVersion,
Source: "drush_status",
Description: "Currently installed version of Drupal on the Environment",
Category: Drupal,
Category: Drupal,
},
{
Name: "drush-version",
Value: p.DrushVersion,
Source: "drush_status",
Name: "drush-version",
Value: p.DrushVersion,
Source: "drush_status",
Description: "Currently installed version of Drush on the Environment",
Category: Drupal,
Category: Drupal,
},
}, nil
}

func init() {
func init() {
RegisterGatherer("Drush gatherer", &drushGatherer{})
}
24 changes: 12 additions & 12 deletions gatherers/DrushPMLGatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package gatherers
import (
"encoding/json"
"fmt"
"github.com/uselagoon/lagoon-facts-app/utils"
"log"

"github.com/uselagoon/lagoon-facts-app/utils"
)

type drushPmlGatherer struct {
Expand All @@ -17,18 +18,17 @@ func (p *drushPmlGatherer) GetGathererCmdType() string {

type drushPmlEntry struct {
Package string
Name string
Type string
Status string
Name string
Type string
Status string
Version interface{}
}


func (p *drushPmlGatherer) AppliesToEnvironment() bool {

err, stdOut, stdErr := utils.Shellout("drush pml --format=json 2> /dev/null")
if err != nil {
log.Printf("Drush gatherer cannot be applied: %v", stdErr)
log.Printf("Drush pml gatherer cannot be applied: %v", stdErr)
return false
}

Expand All @@ -41,11 +41,11 @@ func (p *drushPmlGatherer) AppliesToEnvironment() bool {

for key, element := range result {
p.GatheredFacts = append(p.GatheredFacts, GatheredFact{
Name: key,
Value: fmt.Sprintf("%v", element.Version),
Source: "drush_pml",
Description: "Drupal " + element.Type + " status: " + element.Status,
Category: Drupal,
Name: key,
Value: fmt.Sprintf("%v", element.Version),
Source: "drush_pml",
Description: "Drupal " + element.Type + " status: " + element.Status,
Category: Drupal,
})
}

Expand All @@ -56,6 +56,6 @@ func (p *drushPmlGatherer) GatherFacts() ([]GatheredFact, error) {
return p.GatheredFacts, nil
}

func init() {
func init() {
RegisterGatherer("Drupal Module List Gatherer", &drushPmlGatherer{})
}
47 changes: 19 additions & 28 deletions gatherers/EnvVarGatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type envVarGatherer struct {
}

type envVar struct {
Key string
Value string
Key string
Value string
Description string
}

Expand All @@ -20,46 +20,37 @@ func (p *envVarGatherer) GetGathererCmdType() string {

func (p *envVarGatherer) AppliesToEnvironment() bool {
var lagoonDomain = os.Getenv("LAGOON_DOMAIN")
if (lagoonDomain == "") {
lagoonDomain = "N/A - UNSET"
}

var lagoonEnvType = os.Getenv("LAGOON_ENVIRONMENT_TYPE")
if (lagoonEnvType == "") {
lagoonEnvType = "N/A - UNSET"
}

var composerVersion = os.Getenv("COMPOSER_VERSION")
if (composerVersion == "") {
composerVersion = "N/A - UNSET"
}

envVars := map[string]envVar{
"LAGOON_DOMAIN": {
Key: "LAGOON_DOMAIN",
Value: lagoonDomain,
Key: "LAGOON_DOMAIN",
Value: lagoonDomain,
Description: "The domain address of this environment",
},
"LAGOON_ENVIRONMENT_TYPE": {
Key: "LAGOON_ENVIRONMENT_TYPE",
Value: lagoonEnvType,
Key: "LAGOON_ENVIRONMENT_TYPE",
Value: lagoonEnvType,
Description: "This is a '" + lagoonEnvType + "' environment type",
},
"COMPOSER_VERSION": {
Key: "COMPOSER_VERSION",
Value: composerVersion,
Key: "COMPOSER_VERSION",
Value: composerVersion,
Description: "Composer version '" + composerVersion + "' was found",
},
}

for key, element := range envVars {
p.GatheredFacts = append(p.GatheredFacts, GatheredFact{
Name: key,
Value: element.Value,
Source: "env",
Description: element.Description,
Category: EnvVar,
})
if element.Value != "" {
p.GatheredFacts = append(p.GatheredFacts, GatheredFact{
Name: key,
Value: element.Value,
Source: "env",
Description: element.Description,
Category: EnvVar,
})
}
}

return true
Expand All @@ -69,6 +60,6 @@ func (p *envVarGatherer) GatherFacts() ([]GatheredFact, error) {
return p.GatheredFacts, nil
}

func init() {
func init() {
RegisterGatherer("Environment variables gatherer", &envVarGatherer{})
}
}
Loading

0 comments on commit 663808e

Please sign in to comment.