From 663808eb96899521ce30a45e301f3231662e754b Mon Sep 17 00:00:00 2001 From: Tim Clifford Date: Tue, 12 Oct 2021 16:03:53 +0100 Subject: [PATCH] clean output and add verbosity --- cmd/filegather.go | 10 +++---- cmd/gather.go | 42 +++++++++++++++++++--------- cmd/root.go | 6 +++- gatherers/ApplicationGatherer.go | 11 ++++---- gatherers/DrushGatherer.go | 23 ++++++++-------- gatherers/DrushPMLGatherer.go | 24 ++++++++-------- gatherers/EnvVarGatherer.go | 47 +++++++++++++------------------- gatherers/defs.go | 19 +++++-------- 8 files changed, 95 insertions(+), 87 deletions(-) diff --git a/cmd/filegather.go b/cmd/filegather.go index 637cc26..8c7cf8e 100644 --- a/cmd/filegather.go +++ b/cmd/filegather.go @@ -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 @@ -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") @@ -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()) } @@ -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)) } diff --git a/cmd/gather.go b/cmd/gather.go index 22abfa7..aa6c674 100644 --- a/cmd/gather.go +++ b/cmd/gather.go @@ -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 @@ -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) } @@ -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()) @@ -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)) } @@ -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) } diff --git a/cmd/root.go b/cmd/root.go index df32166..b4cb5ab 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,9 +17,10 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" "os" + "github.com/spf13/cobra" + homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) @@ -27,6 +28,7 @@ import ( 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{ @@ -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. diff --git a/gatherers/ApplicationGatherer.go b/gatherers/ApplicationGatherer.go index d865c7a..149e631 100644 --- a/gatherers/ApplicationGatherer.go +++ b/gatherers/ApplicationGatherer.go @@ -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 { @@ -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 { @@ -45,7 +46,7 @@ func (p *applicationGatherer) AppliesToEnvironment() bool { Value: result.Versions[0], Source: "application_via_composer", Description: result.Description, - Category: Application, + Category: Application, }) } diff --git a/gatherers/DrushGatherer.go b/gatherers/DrushGatherer.go index edf7e55..2c49e2d 100644 --- a/gatherers/DrushGatherer.go +++ b/gatherers/DrushGatherer.go @@ -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 { @@ -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 } @@ -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{}) } diff --git a/gatherers/DrushPMLGatherer.go b/gatherers/DrushPMLGatherer.go index 6b6c280..e9cbc63 100644 --- a/gatherers/DrushPMLGatherer.go +++ b/gatherers/DrushPMLGatherer.go @@ -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 { @@ -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 } @@ -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, }) } @@ -56,6 +56,6 @@ func (p *drushPmlGatherer) GatherFacts() ([]GatheredFact, error) { return p.GatheredFacts, nil } -func init() { +func init() { RegisterGatherer("Drupal Module List Gatherer", &drushPmlGatherer{}) } diff --git a/gatherers/EnvVarGatherer.go b/gatherers/EnvVarGatherer.go index ecf7daf..2317b18 100644 --- a/gatherers/EnvVarGatherer.go +++ b/gatherers/EnvVarGatherer.go @@ -9,8 +9,8 @@ type envVarGatherer struct { } type envVar struct { - Key string - Value string + Key string + Value string Description string } @@ -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 @@ -69,6 +60,6 @@ func (p *envVarGatherer) GatherFacts() ([]GatheredFact, error) { return p.GatheredFacts, nil } -func init() { +func init() { RegisterGatherer("Environment variables gatherer", &envVarGatherer{}) -} \ No newline at end of file +} diff --git a/gatherers/defs.go b/gatherers/defs.go index d779782..7eefbe5 100644 --- a/gatherers/defs.go +++ b/gatherers/defs.go @@ -1,17 +1,13 @@ package gatherers -import ( - "log" -) - // GatheredFact maps to the Lagoon GraphQL AddFactsInput type GatheredFact struct { - Name string `json:"name"` - Value string `json:"value"` - Source string `json:"source"` - Environment int `json:"environment"` - Description string `json:"description"` - Category FactCategory `json:"-"` //`json:"category,omitempty"` + Name string `json:"name"` + Value string `json:"value"` + Source string `json:"source"` + Environment int `json:"environment"` + Description string `json:"description"` + Category FactCategory `json:"-"` //`json:"category,omitempty"` } const ( @@ -28,10 +24,9 @@ type Gatherer interface { var gathererInternalMap []Gatherer func RegisterGatherer(name string, gatherer Gatherer) { - log.Print("registering: " + name) gathererInternalMap = append(gathererInternalMap, gatherer) } func GetGatherers() []Gatherer { return gathererInternalMap -} \ No newline at end of file +}