Initial Commit
This commit is contained in:
57
cmd/delete.go
Normal file
57
cmd/delete.go
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"mortons.site/tps/internal/client"
|
||||
"mortons.site/tps/internal/properties"
|
||||
)
|
||||
|
||||
// deleteCmd represents the delete command
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete [name]",
|
||||
Short: "Deletes a property",
|
||||
Long: `Deletes and existing property`,
|
||||
PreRun: toggleDebug,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
p := []properties.PropertyDelete{{Type: "vcfg:PropertyDelete", Name: args[0]}}
|
||||
pJSON, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debug(string(pJSON))
|
||||
|
||||
hostname := viper.GetString("hostname")
|
||||
username := viper.GetString("username")
|
||||
password := viper.GetString("password")
|
||||
clientId := viper.GetString("clientId")
|
||||
|
||||
err = client.DeleteProperty(&client.Config{Hostname: hostname, Username: username, Password: password, ClientID: clientId}, p)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(deleteCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
71
cmd/list.go
Normal file
71
cmd/list.go
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"mortons.site/tps/internal/client"
|
||||
"mortons.site/tps/internal/properties"
|
||||
)
|
||||
|
||||
var filter string
|
||||
|
||||
// listCmd represents the list command
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Get Properties from the Tenant Properties Service (TPS)",
|
||||
Long: `Get Properties from the Tenant Properties Service (TPS) that have been created or updated.`,
|
||||
PreRun: toggleDebug,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
hostname := viper.GetString("hostname")
|
||||
username := viper.GetString("username")
|
||||
password := viper.GetString("password")
|
||||
clientId := viper.GetString("clientId")
|
||||
|
||||
collectionJSON, err := client.GetProperties(&client.Config{Hostname: hostname, Username: username, Password: password, ClientID: clientId}, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var collection properties.Collection
|
||||
|
||||
if err := json.Unmarshal([]byte(collectionJSON), &collection); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := table.NewWriter()
|
||||
t.SetOutputMirror(os.Stdout)
|
||||
t.AppendHeader(table.Row{"Property Name", "Value", "Last Modified By", "Last Modified Date"})
|
||||
for _, p := range collection.Member {
|
||||
t.AppendRow([]interface{}{p.Name, truncateText(p.Value, 80), p.LastModifiedBy, p.LastModifiedDate})
|
||||
}
|
||||
t.Render()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func truncateText(s string, max int) string {
|
||||
if max > len(s) {
|
||||
return s
|
||||
}
|
||||
return s[:max] + "... (truncated) "
|
||||
}
|
||||
|
||||
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 --fitler \"redaction\"")
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
90
cmd/root.go
Normal file
90
cmd/root.go
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
Hostname string
|
||||
Username string
|
||||
Password string
|
||||
ClientId string
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "tps",
|
||||
Short: "Tenant Properties Service CLI",
|
||||
Long: `Command line interface for accessing and setting Tenant Properties
|
||||
For a full list of System Properties, please refer to the documentation:
|
||||
https://em-docs.verint.com/15_3/em-knowledge-management/Content/SystemProperties/Tenant_Properties_Service.htm`,
|
||||
// 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/.tps.yaml)")
|
||||
rootCmd.PersistentFlags().StringVar(&Hostname, "hostname", "", "Environment hostname")
|
||||
rootCmd.MarkFlagRequired("hostname")
|
||||
viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname"))
|
||||
rootCmd.PersistentFlags().StringVar(&Username, "username", "apiclient", "API username")
|
||||
viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
|
||||
rootCmd.PersistentFlags().StringVar(&Password, "password", "apiclient", "API password")
|
||||
viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
|
||||
rootCmd.PersistentFlags().StringVar(&ClientId, "clientId", "default", "clientId or tenantId for API calls")
|
||||
viper.BindPFlag("clientId", rootCmd.PersistentFlags().Lookup("clientId"))
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose logging")
|
||||
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// 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())
|
||||
}
|
||||
}
|
||||
57
cmd/update.go
Normal file
57
cmd/update.go
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"mortons.site/tps/internal/client"
|
||||
"mortons.site/tps/internal/properties"
|
||||
)
|
||||
|
||||
// updateCmd represents the update command
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update [name] [value]",
|
||||
Short: "Update or Create a property",
|
||||
Long: `Update or create new properties using the Tenant Properties Service`,
|
||||
PreRun: toggleDebug,
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
p := []properties.PropertyUpdateOrCreate{{Type: "vcfg:PropertyUpdateOrCreate", Name: args[0], Value: args[1]}}
|
||||
pJSON, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debug(string(pJSON))
|
||||
|
||||
hostname := viper.GetString("hostname")
|
||||
username := viper.GetString("username")
|
||||
password := viper.GetString("password")
|
||||
clientId := viper.GetString("clientId")
|
||||
|
||||
err = client.UpdateProperty(&client.Config{Hostname: hostname, Username: username, Password: password, ClientID: clientId}, p)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// updateCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// updateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
Reference in New Issue
Block a user