diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000000000000000000000000000000000000..5f6b56d57c91d0936bc7123da0c8cbfc5d17bc19 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,93 @@ +// Copyright © 2018 Privategrity Corporation +// +// All rights reserved. + +// Package cmd initializes the CLI and config parsers as well as the logger. +package cmd + +import ( + "os" + "github.com/spf13/cobra" + "github.com/spf13/viper" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/privategrity/client/api" +) + +var verbose bool +var userId int +var serverAddr string +var message 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() { + if err := rootCmd.Execute(); err != nil { + jww.ERROR.Println(err) + os.Exit(1) + } +} + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "client", + Short: "Runs a client for cMix anonymous communication platform", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + api.Login(userId, serverAddr) + api.Send(userId, message) + for { + msg := api.TryReceive() + if msg != "" { + jww.INFO.Printf("Message Received: %s", msg) + break + } + } + }, +} + +// init is the initialization function for Cobra which defines commands +// and flags. +func init() { + cobra.OnInitialize(initConfig, initLog) + + // 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().BoolVarP(&verbose, "verbose", "v", false, + "Verbose mode for debugging") + rootCmd.PersistentFlags().IntVarP(&userId, "userid", "i", 0, + "UserID to sign in as") + rootCmd.MarkPersistentFlagRequired("userid") + rootCmd.PersistentFlags().StringVarP(&serverAddr, "serveraddr", "s", "", + "Server address to send messages to") + rootCmd.MarkPersistentFlagRequired("serveraddr") + + // 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") + rootCmd.Flags().StringVarP(&message, "message", "m", "", "Message to send") +} + +// initConfig reads in config file and ENV variables if set. +func initConfig() {} + +// initLog initializes logging thresholds and the log path. +func initLog() { + // If verbose flag set then log more info for debugging + if verbose || viper.GetBool("verbose") { + jww.SetLogThreshold(jww.LevelDebug) + jww.SetStdoutThreshold(jww.LevelDebug) + } else { + jww.SetLogThreshold(jww.LevelInfo) + jww.SetStdoutThreshold(jww.LevelInfo) + } + // Create log file, overwrites if existing + logPath := viper.GetString("logPath") + logFile, err := os.Create(logPath) + if err != nil { + jww.WARN.Println("Invalid or missing log path, default path used.") + } else { + jww.SetLogOutput(logFile) + } +} diff --git a/glide.yaml b/glide.yaml index 50a3ef734ce3d47099143084aea3127684194c47..abc24f385aca9018d06567ed06f41b57fd0e6f3f 100644 --- a/glide.yaml +++ b/glide.yaml @@ -8,3 +8,7 @@ import: version: ClientComms repo: git@gitlab.com:privategrity/comms vcs: git +- package: github.com/spf13/cobra + version: ^0.0.1 + subpackages: + - cobra diff --git a/globals/message.go b/globals/message.go index 41603b04002d0ba842f00e06b6458eafafa9a34f..87f11882280c9d3ed1d40f649f7635c8ce630c5e 100644 --- a/globals/message.go +++ b/globals/message.go @@ -2,6 +2,7 @@ package globals import ( "encoding/binary" + jww "github.com/spf13/jwalterweatherman" ) // Defines message structure. Based the "Basic Message Structure" doc @@ -56,7 +57,9 @@ func NewMessage(sid uint64, messageString string) *Message { func ConstructMessageFromBytes(msg *[]byte) *Message { if uint32(len(*msg)) != TOTAL_LEN || (*msg)[0] != 0 { - panic("invalid message bytes passed") + jww.ERROR.Printf("Invalid message bytes passed! Got %v expected %v", + len(*msg), TOTAL_LEN) + panic("Invalid message bytes passed") return nil } diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..c8a942fc96816e11334b1c090dd38bfcc0c1029b --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +// Copyright © 2018 Privategrity Corporation +// +// All rights reserved. + +package main + +import "gitlab.com/privategrity/client/cmd" + +// main needs no introduction. +func main() { + cmd.Execute() +} \ No newline at end of file