Skip to content
Snippets Groups Projects
Commit f554844f authored by Jake Taylor's avatar Jake Taylor :lips:
Browse files

Added command line interface

parent 0512114a
No related branches found
No related tags found
No related merge requests found
// 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)
}
}
......@@ -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
......@@ -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
}
......
main.go 0 → 100644
// 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment