-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
223 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
// RootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "cumulus", | ||
Short: "Cumulus is an interface to interacting with the Cumulus network", | ||
Long: `Cumulus is used to create mining or verification nodes on the Cumulus network, | ||
create transactions, and check the balance of wallets.`, | ||
} | ||
|
||
// Execute adds all child commands to the root command 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 { | ||
fmt.Println(err) | ||
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/.cumulus.yaml)") | ||
// 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 != "" { // enable ability to specify config file via flag | ||
viper.SetConfigFile(cfgFile) | ||
} | ||
|
||
viper.SetConfigName(".cumulus") // name of config file (without extension) | ||
viper.AddConfigPath(".") // add cwd as first search path | ||
viper.AddConfigPath("$HOME") // adding home directory as second search path | ||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/ubclaunchpad/cumulus/peer" | ||
) | ||
|
||
// runCmd represents the run command | ||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run creates and runs a node on the Cumulus network", | ||
Long: `Run creates a new Cumulus node and connects to the specified target node. | ||
If a target is not provided, listen for incoming connections.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
port, _ := cmd.Flags().GetInt("port") | ||
ip, _ := cmd.Flags().GetString("interface") | ||
target, _ := cmd.Flags().GetString("target") | ||
verbose, _ := cmd.Flags().GetBool("verbose") | ||
run(port, ip, target, verbose) | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(runCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// runCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
runCmd.Flags().IntP("port", "p", peer.DefaultPort, "Port to bind to") | ||
runCmd.Flags().StringP("interface", "i", peer.DefaultIP, "IP address to listen on") | ||
runCmd.Flags().StringP("target", "t", "", "Multiaddress of peer to connect to") | ||
runCmd.Flags().BoolP("verbose", "v", false, "Enable verbose logging") | ||
} | ||
|
||
func run(port int, ip, target string, verbose bool) { | ||
log.Info("Starting Cumulus Peer") | ||
|
||
if verbose { | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
|
||
// Set up a new host on the Cumulus network | ||
host, err := peer.New(ip, port) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Set the host StreamHandler for the Cumulus Protocol and use | ||
// BasicStreamHandler as its StreamHandler. | ||
host.SetStreamHandler(peer.CumulusProtocol, host.Receive) | ||
if target == "" { | ||
// No target was specified, wait for incoming connections | ||
log.Info("No target provided. Listening for incoming connections...") | ||
select {} // Hang until someone connects to us | ||
} | ||
|
||
stream, err := host.Connect(target) | ||
if err != nil { | ||
log.WithError(err).Fatal("Error connecting to target: ", target) | ||
} | ||
|
||
// Send a message to the peer | ||
_, err = stream.Write([]byte("Hello, world!")) | ||
if err != nil { | ||
log.WithError(err).Fatal("Error sending a message to the peer") | ||
} | ||
|
||
// Read the reply from the peer | ||
reply, err := ioutil.ReadAll(stream) | ||
if err != nil { | ||
log.WithError(err).Fatal("Error reading a message from the peer") | ||
} | ||
|
||
log.Debugf("Peer %s read reply: %s", host.ID(), string(reply)) | ||
|
||
host.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// sendCmd represents the send command | ||
var sendCmd = &cobra.Command{ | ||
Use: "send", | ||
Short: "Send creates a transaction", | ||
Long: `Send creates a transaction and broadcasts it to the network.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// TODO: Work your own magic here | ||
fmt.Println("send called") | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(sendCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// sendCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// sendCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/ubclaunchpad/cumulus/peer" | ||
) | ||
import "github.com/ubclaunchpad/cumulus/cmd" | ||
|
||
func main() { | ||
log.Info("Starting Cumulus Peer") | ||
|
||
// Get and parse command line arguments | ||
// targetPeer is a Multiaddr representing the target peer to connect to | ||
// when joining the Cumulus Network. | ||
// port is the port to communicate over (defaults to peer.DefaultPort). | ||
// ip is the public IP address of the this host. | ||
targetPeer := flag.String("t", "", "target peer to connect to") | ||
port := flag.Int("p", peer.DefaultPort, "TCP port to use for this host") | ||
ip := flag.String("i", peer.DefaultIP, "IP address to use for this host") | ||
debug := flag.Bool("d", false, "Enable debug logging") | ||
flag.Parse() | ||
|
||
if *debug { | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
|
||
// Set up a new host on the Cumulus network | ||
host, err := peer.New(*ip, *port) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Set the host StreamHandler for the Cumulus Protocol and use | ||
// BasicStreamHandler as its StreamHandler. | ||
host.SetStreamHandler(peer.CumulusProtocol, host.Receive) | ||
if *targetPeer == "" { | ||
// No target was specified, wait for incoming connections | ||
log.Info("No target provided. Listening for incoming connections...") | ||
select {} // Hang until someone connects to us | ||
} | ||
|
||
stream, err := host.Connect(*targetPeer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Send a message to the peer | ||
_, err = stream.Write([]byte("Hello, world!")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Read the reply from the peer | ||
reply, err := ioutil.ReadAll(stream) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Debugf("Peer %s read reply: %s", host.ID(), string(reply)) | ||
|
||
host.Close() | ||
cmd.Execute() | ||
} |