Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: main function, full and light commands with subcommands #97

Merged
merged 15 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/celestia/full.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/node"
)

func init() {
const repoName = "repository"
fullCmd.AddCommand(
cmd.Init(repoName, node.Full),
cmd.Start(repoName, node.Full),
)
fullCmd.PersistentFlags().StringP(repoName,
"r",
"~/.celestia-full",
"The root/home directory of your Celestial Full Node",
)
}

var fullCmd = &cobra.Command{
renaynay marked this conversation as resolved.
Show resolved Hide resolved
Use: "full [subcommand]",
Args: cobra.NoArgs,
}
27 changes: 27 additions & 0 deletions cmd/celestia/light.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/node"
)

func init() {
const repoName = "repository"
lightCmd.AddCommand(
cmd.Init(repoName, node.Light),
cmd.Start(repoName, node.Light),
)
lightCmd.PersistentFlags().StringP(
repoName,
"r",
"~/.celestia-light",
"The root/home directory of your Celestial Light Node",
)
}

var lightCmd = &cobra.Command{
renaynay marked this conversation as resolved.
Show resolved Hide resolved
Use: "light [subcommand]",
Args: cobra.NoArgs,
}
37 changes: 37 additions & 0 deletions cmd/celestia/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"os"

logging "github.com/ipfs/go-log/v2"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(fullCmd, lightCmd)
}

func main() {
renaynay marked this conversation as resolved.
Show resolved Hide resolved
err := run()
if err != nil {
os.Exit(1)
}
}

func run() error {
// TODO(@Wondertan): In practice we won't need all INFO loggers from IPFS/libp2p side
renaynay marked this conversation as resolved.
Show resolved Hide resolved
// so we would need to turn off them somewhere in `logs` package.
logging.SetAllLoggers(logging.LevelInfo)
return rootCmd.Execute()
}

var rootCmd = &cobra.Command{
Use: "celestia [ full || light ] [subcommand]",
Short: `
/ ____/__ / /__ _____/ /_(_)___ _
/ / / _ \/ / _ \/ ___/ __/ / __ /
/ /___/ __/ / __(__ ) /_/ / /_/ /
\____/\___/_/\___/____/\__/_/\__,_/
`,
Args: cobra.NoArgs,
}
41 changes: 41 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/node"
)

// Init constructs a CLI command to initialize Celestia Node of the given type 'tp'.
// It is meant to be used a subcommand and also receive persistent flag name for repository path.
func Init(repoName string, tp node.Type) *cobra.Command {
if !tp.IsValid() {
panic("cmd: Init: invalid Node Type")
}
if len(repoName) == 0 {
panic("parent command must specify a persistent flag name for repository path")
}

const cfgName = "config"
cmd := &cobra.Command{
Use: "init",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfgF := cmd.Flag(cfgName).Value
repo := cmd.Flag(repoName).Value.String()

if len(cfgF.String()) != 0 {
cfg, err := node.LoadConfig(cfgF.String())
if err != nil {
return err
}

return node.InitWith(repo, tp, cfg)
}

return node.Init(repo, tp)
},
}
cmd.Flags().StringP(cfgName, "c", "", "Path to a customized Config")
return cmd
}
60 changes: 60 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"os/signal"
"syscall"

"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/node"
)

// Start constructs a CLI command to start Celestia Node daemon of the given type 'tp'.
// It is meant to be used a subcommand and also receive persistent flag name for repository path.
func Start(repoName string, tp node.Type) *cobra.Command {
if !tp.IsValid() {
panic("cmd: Start: invalid Node Type")
}
if len(repoName) == 0 {
panic("parent command must specify a persistent flag name for repository path")
}
return &cobra.Command{
Use: "start",
Short: "Starts Node daemon. First stopping signal gracefully stops the Node and second terminates it.",
Aliases: []string{"run", "daemon"},
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
repoPath := cmd.Flag(repoName).Value.String()

repo, err := node.Open(repoPath, tp)
renaynay marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

nd, err := node.New(tp, repo)
if err != nil {
return err
}

ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
err = nd.Start(ctx)
if err != nil {
return err
}

<-ctx.Done()
cancel() // ensure we stop reading more signals for start context

ctx, cancel = signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
err = nd.Stop(ctx)
if err != nil {
return err
}

return repo.Close()
mattdf marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
2 changes: 2 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Config = config.Config
// DefaultConfig returns a modified default config of Core node.
func DefaultConfig() *Config {
cfg := config.DefaultConfig()
// TODO(@Wondertan): Instead, we should have celestia-app here or even allow customizing it
cfg.ProxyApp = "kvstore"
renaynay marked this conversation as resolved.
Show resolved Hide resolved
updateDefaults(cfg)
return cfg
}
Expand Down
4 changes: 2 additions & 2 deletions core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const defaultValKeyType = types.ABCIPubKeyTypeSecp256k1
// * P2P comms private key
// * Stub genesis doc file
func Init(path string) (err error) {
log.Info("Initializing Repository over '%s'", path)
defer log.Info("Repository initialized")
log.Infof("Initializing Core Repository over '%s'", path)
defer log.Info("Core Repository initialized")

// 1 - ensure config
cfg := DefaultConfig()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/multiformats/go-base32 v0.0.4
github.com/multiformats/go-multiaddr v0.4.0
github.com/multiformats/go-multihash v0.0.15
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942
go.uber.org/fx v1.14.2
go.uber.org/zap v1.19.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ github.com/huin/goupnp v1.0.2/go.mod h1:0dxJBVBHqTMjIUMkESDTNgOOx/Mw5wYIfyFmdzSa
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/ipfs/bbloom v0.0.1/go.mod h1:oqo8CVWsJFMOZqTglBG4wydCE4IQA/G2/SEofB0rjUI=
Expand Down Expand Up @@ -1156,12 +1157,14 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cobra v0.0.0-20170417170307-b6cb39589372/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw=
github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v0.0.0-20170417173400-9e4c21054fa1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
Expand Down
31 changes: 18 additions & 13 deletions node/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ import (
"github.com/celestiaorg/celestia-node/libs/utils"
)

// Init initializes the Node FileSystem(FS) Repository for th given Node Type 'tp' in the directory under 'path' with
// the given Config.
// Init initializes the Node FileSystem Repository for the given Node Type 'tp' in the directory under 'path' with
// default Config.
func Init(path string, tp Type) error {
return InitWith(path, tp, DefaultConfig(tp))
}

// InitWith initializes the Node FileSystem Repository for the given Node Type 'tp' in the directory under 'path'
// with the given Config 'cfg'.
func InitWith(path string, tp Type, cfg *Config) error {
path, err := repoPath(path)
if err != nil {
return err
}
log.Info("Initializing Repository for the Node over '%s'", path)
defer log.Info("Repository initialized")
log.Infof("Initializing %s Node Repository over '%s'", tp, path)

err = initRoot(path)
if err != nil {
return err
}

flock, err := fslock.Lock(lockPath(path))
if err != nil {
Expand All @@ -28,11 +38,6 @@ func Init(path string, tp Type) error {
}
defer flock.Unlock() //nolint: errcheck

err = initRoot(path)
if err != nil {
return err
}

err = initDir(keysPath(path))
if err != nil {
return err
Expand All @@ -43,16 +48,15 @@ func Init(path string, tp Type) error {
return err
}

cfg := DefaultConfig(tp)
cfgPath := configPath(path)
if !utils.Exists(cfgPath) {
err = SaveConfig(cfgPath, cfg)
if err != nil {
return err
}
log.Info("New config is generated")
log.Infow("Saving config", "path", cfgPath)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@renaynay, What's the point of this log if it duplicates Initializing %s Node Repository over '%s' above? I mean it logs the path to the repo and cfg is constantly repo_path+config.toml, so not sure if there is a point to add this path here, but it does not hurt anyway.

} else {
log.Info("Config already exists")
log.Infow("Config already exists", "path", cfgPath)
}

// TODO(@Wondertan): This is a lazy hack which prevents Core Repository to be generated for all case, and generates
Expand All @@ -68,10 +72,11 @@ func Init(path string, tp Type) error {
return core.Init(corePath)
}

log.Info("Node Repository initialized")
return nil
}

// IsInit checks whether FS Repository was setup under given 'path'.
// IsInit checks whether FileSystem Repository was setup under given 'path'.
// If any required file/subdirectory does not exist, then false is reported.
func IsInit(path string, tp Type) bool {
path, err := repoPath(path)
Expand Down
3 changes: 0 additions & 3 deletions node/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ func lightComponents(cfg *Config, repo Repository) fx.Option {
return fx.Options(
// manual providing
fx.Provide(context.Background),
fx.Provide(func() Type {
return Light
}),
fx.Provide(func() *Config {
return cfg
}),
Expand Down
Loading