Skip to content

Commit

Permalink
feat: added configuration file support and modified some cli parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rfizzle committed Aug 12, 2020
1 parent 3f434eb commit 8f1f876
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
72 changes: 65 additions & 7 deletions cli.go
Expand Up @@ -2,10 +2,14 @@ package main

import (
"errors"
"fmt"
"github.com/rfizzle/collector-helpers/outputs"
"github.com/rfizzle/collector-helpers/state"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"os"
"path/filepath"
"strings"
)

Expand All @@ -14,11 +18,13 @@ func setupCliFlags() error {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

flag.String("state-path", "google.state", "state file path")
flag.Int("schedule", 30, "time in seconds to collect")
flag.String("google-credentials", "", "google service account creds file path")
flag.String("gsuite-credentials", "", "google service account credential file path")
flag.String("impersonated-user", "", "user to impersonate for API access")
flag.BoolP("verbose", "v", false, "verbose logging")
flag.BoolP("config", "c", false, "enable config file")
flag.String("config-path", "", "config file path")
state.InitCLIParams()
outputs.InitCLIParams()
flag.Parse()
err := viper.BindPFlags(flag.CommandLine)
Expand All @@ -27,6 +33,11 @@ func setupCliFlags() error {
log.Fatalf("Failed parsing flags: %v", err.Error())
}

// Check config
if err := checkConfigParams(); err != nil {
return err
}

// Check parameters
if err := checkRequiredParams(); err != nil {
return err
Expand All @@ -35,22 +46,69 @@ func setupCliFlags() error {
return nil
}

func checkRequiredParams() error {
if viper.GetString("state-path") == "" {
return errors.New("missing State File Path param (--state-path)")
func checkConfigParams() error {
if viper.GetBool("config") {
if !fileExists(viper.GetString("config-path")) {
return errors.New("missing config file path param (--config-path)")
}

dir, file := filepath.Split(viper.GetString("config-path"))
ext := strings.ToLower(filepath.Ext(viper.GetString("config-path")))

supportedTypes := []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"}
if !contains(supportedTypes, ext) {
e := fmt.Sprintf("invalid config file type (supported: %s )", strings.Join(supportedTypes[:], ", "))
return errors.New(e)
}

fileName := strings.TrimSuffix(file, ext)

viper.SetConfigName(fileName)
viper.SetConfigType(ext)
viper.AddConfigPath(dir)

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return fmt.Errorf("Fatal error config file: %s \n", err)
}
}

if viper.GetString("google-credentials") == "" {
return errors.New("missing Google Credentials param (--google-credentials)")
return nil
}

func checkRequiredParams() error {
if viper.GetString("gsuite-credentials") == "" {
return errors.New("missing google credentials param (--gsuite-credentials)")
}

if viper.GetString("impersonated-user") == "" {
return errors.New("missing Impersonate User param (--impersonated-user)")
}

if err := state.ValidateCLIParams(); err != nil {
return err
}

if err := outputs.ValidateCLIParams(); err != nil {
return err
}

return nil
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/rfizzle/gsuite-collector
go 1.14

require (
github.com/rfizzle/collector-helpers v0.1.0
github.com/rfizzle/collector-helpers v1.2.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/tidwall/pretty v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -231,8 +231,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rfizzle/collector-helpers v0.1.0 h1:gPmhG+MGlLQ08OcE6PyIyCjQDN/3CElA6aBRlif9pkU=
github.com/rfizzle/collector-helpers v0.1.0/go.mod h1:Yl0FEdZB8U8rI8Zy2eMzSeyRRC2hVb0HBGARZ9DV79w=
github.com/rfizzle/collector-helpers v1.2.0 h1:eElQyjK6xoC7CkG1UWdGmhspoiCpaYCPdpNKJ2SBy+w=
github.com/rfizzle/collector-helpers v1.2.0/go.mod h1:Yl0FEdZB8U8rI8Zy2eMzSeyRRC2hVb0HBGARZ9DV79w=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -96,7 +96,7 @@ func getEvents(timestamp string, resultChannel chan<- string) (int, time.Time) {
now := time.Now()

// Build an HTTP client with JWT header
googleClient, err := client.BuildClient(viper.GetString("google-credentials"), viper.GetString("impersonated-user"))
googleClient, err := client.BuildClient(viper.GetString("gsuite-credentials"), viper.GetString("impersonated-user"))
if err != nil {
log.Fatalf("Unable to build client: %v", err)
}
Expand Down

0 comments on commit 8f1f876

Please sign in to comment.