More cmd fixes
This commit is contained in:
parent
861e126ab4
commit
7a08bd8fb3
@ -121,12 +121,13 @@ var rootCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var runCmd = &cobra.Command{
|
var buildRunAllCmd = &cobra.Command{
|
||||||
Use: "build-and-run",
|
Use: "build-and-run",
|
||||||
|
Long: "build and then run all clients and nodes",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
paths, ok := cmd.Context().Value("paths").(absolutePaths)
|
paths, ok := cmd.Context().Value("paths").(absolutePaths)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatal("did not get parent context")
|
log.Fatal("did not get context")
|
||||||
}
|
}
|
||||||
|
|
||||||
// checking number of nodes to deploy
|
// checking number of nodes to deploy
|
||||||
@ -142,14 +143,59 @@ var runCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// running the script
|
// running the script
|
||||||
err = runAll(paths, numClients, numNodes)
|
err = buildRunAll(paths, numClients, numNodes)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.With(zap.Error(err)).Fatal("failed to run the command")
|
log.With(zap.Error(err)).Fatal("failed to run the command")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var buildAllCmd = &cobra.Command{
|
||||||
|
Use: "build-all",
|
||||||
|
Long: "builds both the clients and nodes",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
paths, ok := cmd.Context().Value("paths").(absolutePaths)
|
||||||
|
if !ok {
|
||||||
|
log.Fatal("did not get context")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := buildAll(paths)
|
||||||
|
if err != nil {
|
||||||
|
log.With(zap.Error(err)).Fatal("failed to run the command")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var runAllCmd = &cobra.Command{
|
||||||
|
Use: "run-all",
|
||||||
|
Long: "runs all clients and nodes",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
paths, ok := cmd.Context().Value("paths").(absolutePaths)
|
||||||
|
if !ok {
|
||||||
|
log.Fatal("did not get context")
|
||||||
|
}
|
||||||
|
|
||||||
|
// checking number of nodes to deploy
|
||||||
|
numNodes, err := cmd.Flags().GetUint("nodes")
|
||||||
|
if err != nil {
|
||||||
|
log.With(zap.Error(err)).Fatal("number of nodes is not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
// checking number of clients to deploy
|
||||||
|
numClients, err := cmd.Flags().GetUint("clients")
|
||||||
|
if err != nil {
|
||||||
|
log.With(zap.Error(err)).Fatal("number of clients is not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = runAll(paths, numClients, numNodes)
|
||||||
|
if err != nil {
|
||||||
|
log.With(zap.Error(err)).Fatal("failed to run the command")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().String("config-dir", "etc/configs", "generated configs")
|
rootCmd.PersistentFlags().String("config-dir", "etc/configs", "generated configs")
|
||||||
rootCmd.PersistentFlags().String("node-pkg", "github.com/anytypeio/go-anytype-infrastructure-experiments/node/cmd", "node package")
|
rootCmd.PersistentFlags().String("node-pkg", "github.com/anytypeio/go-anytype-infrastructure-experiments/node/cmd", "node package")
|
||||||
@ -159,9 +205,14 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().String("bin", "bin", "path to folder where all the binaries are")
|
rootCmd.PersistentFlags().String("bin", "bin", "path to folder where all the binaries are")
|
||||||
rootCmd.PersistentFlags().String("db-path", "db", "path to folder where the working directories should be placed")
|
rootCmd.PersistentFlags().String("db-path", "db", "path to folder where the working directories should be placed")
|
||||||
|
|
||||||
runCmd.Flags().UintP("nodes", "n", 3, "number of nodes to be generated")
|
buildRunAllCmd.Flags().UintP("nodes", "n", 3, "number of nodes to be generated")
|
||||||
runCmd.Flags().UintP("clients", "c", 2, "number of clients to be generated")
|
buildRunAllCmd.Flags().UintP("clients", "c", 2, "number of clients to be generated")
|
||||||
rootCmd.AddCommand(runCmd)
|
runAllCmd.Flags().UintP("nodes", "n", 3, "number of nodes to be generated")
|
||||||
|
runAllCmd.Flags().UintP("clients", "c", 2, "number of clients to be generated")
|
||||||
|
|
||||||
|
rootCmd.AddCommand(buildRunAllCmd)
|
||||||
|
rootCmd.AddCommand(buildAllCmd)
|
||||||
|
rootCmd.AddCommand(runAllCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -186,13 +237,6 @@ func createAppPaths(paths absolutePaths, binaryPath, appName string, num int) (a
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
// creating directory for each app
|
|
||||||
resPath := path.Join(appTypePath, fmt.Sprintf("%d", i+1))
|
|
||||||
err = createDirectoryIfNotExists(resPath)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// checking if relevant config exists
|
// checking if relevant config exists
|
||||||
cfgPath := path.Join(paths.configPath, fmt.Sprintf("%s%d.yml", appName, i+1))
|
cfgPath := path.Join(paths.configPath, fmt.Sprintf("%s%d.yml", appName, i+1))
|
||||||
if _, err = os.Stat(cfgPath); os.IsNotExist(err) {
|
if _, err = os.Stat(cfgPath); os.IsNotExist(err) {
|
||||||
@ -200,6 +244,13 @@ func createAppPaths(paths absolutePaths, binaryPath, appName string, num int) (a
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// creating directory for each app
|
||||||
|
resPath := path.Join(appTypePath, fmt.Sprintf("%d", i+1))
|
||||||
|
err = createDirectoryIfNotExists(resPath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
appPaths = append(appPaths, appPath{
|
appPaths = append(appPaths, appPath{
|
||||||
wdPath: resPath,
|
wdPath: resPath,
|
||||||
binaryPath: binaryPath,
|
binaryPath: binaryPath,
|
||||||
@ -210,19 +261,17 @@ func createAppPaths(paths absolutePaths, binaryPath, appName string, num int) (a
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func runAll(paths absolutePaths, numClients, numNodes uint) (err error) {
|
func buildRunAll(paths absolutePaths, numClients, numNodes uint) (err error) {
|
||||||
err = build(paths.nodePkgPath, paths.nodeBinaryPath, paths.nodePkgName)
|
err = buildAll(paths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to build node: %w", err)
|
err = fmt.Errorf("failed to build all: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = build(paths.clientPkgPath, paths.clientBinaryPath, paths.clientPkgName)
|
return runAll(paths, numClients, numNodes)
|
||||||
if err != nil {
|
}
|
||||||
err = fmt.Errorf("failed to build client: %w", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func runAll(paths absolutePaths, numClients uint, numNodes uint) (err error) {
|
||||||
nodePaths, err := createAppPaths(paths, paths.nodeBinaryPath, "node", int(numNodes))
|
nodePaths, err := createAppPaths(paths, paths.nodeBinaryPath, "node", int(numNodes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to create working directories for nodes: %w", err)
|
err = fmt.Errorf("failed to create working directories for nodes: %w", err)
|
||||||
@ -257,6 +306,21 @@ func runAll(paths absolutePaths, numClients, numNodes uint) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildAll(paths absolutePaths) (err error) {
|
||||||
|
err = build(paths.nodePkgPath, paths.nodeBinaryPath, paths.nodePkgName)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("failed to build node: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = build(paths.clientPkgPath, paths.clientBinaryPath, paths.clientPkgName)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("failed to build client: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func build(dirPath, binaryPath, packageName string) (err error) {
|
func build(dirPath, binaryPath, packageName string) (err error) {
|
||||||
cmd := exec.Command("go", "build", "-v", "-o", binaryPath, packageName)
|
cmd := exec.Command("go", "build", "-v", "-o", binaryPath, packageName)
|
||||||
cmd.Dir = dirPath
|
cmd.Dir = dirPath
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user