initial commit
This commit is contained in:
118
cmd/counts.go
Normal file
118
cmd/counts.go
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
Copyright © 2024 Peter Morton <Peter.Morton@verint.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"verint.com/focusedopps/internal/client"
|
||||
"verint.com/focusedopps/internal/opps"
|
||||
)
|
||||
|
||||
// listCmd represents the list command
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "count",
|
||||
Short: "Counts the focused opps for each Solution Consultant",
|
||||
Long: `Counts the focused opps for each Solution Consultant from the DFE Focused Opps Lists site.`,
|
||||
PreRun: toggleDebug,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
var counters map[int]opps.Counter = make(map[int]opps.Counter)
|
||||
|
||||
var scs map[int]string = make(map[int]string)
|
||||
scs[4] = "Peter Morton"
|
||||
scs[60] = "Jim Gross"
|
||||
scs[122] = "Megan Brown"
|
||||
scs[237] = "Shelley Rose"
|
||||
scs[78] = "Amber Nash"
|
||||
scs[172] = "Robin Zukowski"
|
||||
scs[158] = "Ramzi Banna"
|
||||
scs[385] = "David Young"
|
||||
scs[52] = "Brian Cash"
|
||||
scs[140] = "Scott Dorris"
|
||||
scs[29] = "Dean Vivian"
|
||||
scs[143] = "Mike Leyva"
|
||||
scs[186] = "Paul Tessier"
|
||||
scs[107] = "Jason Valdina"
|
||||
scs[227] = "Adriano Oliveira"
|
||||
|
||||
for key := range scs {
|
||||
counters[key] = opps.Counter{Inactive: 0, Won: 0, Open: 0, Closed: 0}
|
||||
}
|
||||
|
||||
token := viper.GetString("token")
|
||||
|
||||
var fetchAgain = true
|
||||
var next = ""
|
||||
for fetchAgain {
|
||||
|
||||
collectionJSON, err := client.GetOpportunities(&client.Config{Token: token}, next)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var data opps.Result
|
||||
|
||||
if err := json.Unmarshal([]byte(collectionJSON), &data); err != nil {
|
||||
return err
|
||||
}
|
||||
next = data.Data.Next
|
||||
if next == "" {
|
||||
fetchAgain = false
|
||||
}
|
||||
log.WithFields(log.Fields{"next": fmt.Sprintf("%+v", next)}).Debug()
|
||||
|
||||
for _, opp := range data.Data.Opportunities {
|
||||
|
||||
for _, scID := range opp.SolutionConsultants.Results {
|
||||
if tmp, ok := counters[scID]; ok {
|
||||
switch {
|
||||
case opp.Status == "Inactive":
|
||||
tmp.Inactive++
|
||||
case opp.Status == "Won":
|
||||
tmp.Won++
|
||||
case opp.Status == "Open":
|
||||
tmp.Open++
|
||||
case opp.Status == "Lost/Other":
|
||||
tmp.Closed++
|
||||
}
|
||||
counters[scID] = tmp
|
||||
} else {
|
||||
scs[scID] = "unknown(" + strconv.Itoa(scID) + "), see " + opp.Title
|
||||
counters[scID] = opps.Counter{Inactive: 0, Won: 0, Open: 0, Closed: 0}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t := table.NewWriter()
|
||||
t.SetOutputMirror(os.Stdout)
|
||||
t.AppendHeader(table.Row{"Solution Consultant", "Inactive", "Won", "Open", "Lost/Other"})
|
||||
for key, val := range counters {
|
||||
t.AppendRow([]interface{}{scs[key], val.Inactive, val.Won, val.Open, val.Closed})
|
||||
}
|
||||
t.Render()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// listCmd.Flags().StringVarP(&filter, "filter", "f", "", "Filter properties using a query. For example --filter \"redaction\"")
|
||||
// listCmd.Flags().BoolVarP(¬runc, "notrunc", "", false, "Prevent truncation of values")
|
||||
}
|
||||
27
cmd/logging.go
Normal file
27
cmd/logging.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var verbose bool
|
||||
|
||||
type PlainFormatter struct {
|
||||
}
|
||||
|
||||
func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
|
||||
return []byte(fmt.Sprintf("%s\n", entry.Message)), nil
|
||||
}
|
||||
func toggleDebug(cmd *cobra.Command, args []string) {
|
||||
if verbose {
|
||||
log.Info("Debug logs enabled")
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.SetFormatter(&log.TextFormatter{})
|
||||
} else {
|
||||
plainFormatter := new(PlainFormatter)
|
||||
log.SetFormatter(plainFormatter)
|
||||
}
|
||||
}
|
||||
94
cmd/root.go
Normal file
94
cmd/root.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright © 2023 Peter Morton <Peter.Morton@verint.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
Token string
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "focusedopps",
|
||||
Short: "Focused Opps CLI",
|
||||
Long: `Command line interface for accessing DFE Focused Opps list
|
||||
https://verint-my.sharepoint.com/personal/peter_morton_verint_com/Lists/DFE%20Focus%20Opportunities/AllItems.aspx?env=WebViewList`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.focusedopps.yaml)")
|
||||
addStringFlag(&Token, "token", "", "Auth Token")
|
||||
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose logging")
|
||||
err := viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
func addStringFlag(p *string, key string, value string, usage string) {
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(p, key, value, usage)
|
||||
err := viper.BindPFlag(key, rootCmd.PersistentFlags().Lookup(key))
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".tps" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".tps")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user