Skip to content

Commit

Permalink
CLI,proto,service: templates loading,DB backup&restore
Browse files Browse the repository at this point in the history
  • Loading branch information
mame82 committed Oct 24, 2018
1 parent ecceb6b commit 7bc35ae
Show file tree
Hide file tree
Showing 14 changed files with 1,145 additions and 509 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ installkali:
cp -R dist/scripts /usr/local/P4wnP1/
cp -R dist/HIDScripts /usr/local/P4wnP1/
cp -R dist/www /usr/local/P4wnP1/
cp -R dist/db /usr/local/P4wnP1/
cp build/webapp.js /usr/local/P4wnP1/www
cp build/webapp.js.map /usr/local/P4wnP1/www

Expand All @@ -102,6 +103,7 @@ install:
cp -R dist/scripts /usr/local/P4wnP1/
cp -R dist/HIDScripts /usr/local/P4wnP1/
cp -R dist/www /usr/local/P4wnP1/
cp -R dist/db /usr/local/P4wnP1/
cp dist/bin/* /usr/local/bin/
cp build/webapp.js /usr/local/P4wnP1/www
cp build/webapp.js.map /usr/local/P4wnP1/www
Expand Down
97 changes: 97 additions & 0 deletions cli_client/cmd_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cli_client

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"os"
)



var (
// deploy
tmpDBBackupName = ""
)


func init() {
cmdDB := &cobra.Command{
Use: "db",
Short: "Database backup and restore",
}

cmdDBBackup := &cobra.Command{
Use: "backup",
Short: "Backup DB",
Run: func(cmd *cobra.Command, args []string) {
if len(tmpDBBackupName) == 0 {
fmt.Println("A name for the backup has to be provided with the '--name' flag")
os.Exit(-1)
}
fmt.Print("Creating backup ...")
err := ClientDBBackup(TIMEOUT_LONG, StrRemoteHost, StrRemotePort, tmpDBBackupName)
if err != nil {
fmt.Println(" failed")
fmt.Println(err.Error())
os.Exit(-1)
}
fmt.Println(" success")
},

}

cmdDBRestore := &cobra.Command{
Use: "restore",
Short: "Restore DB",
Run: func(cmd *cobra.Command, args []string) {
if len(tmpDBBackupName) == 0 {
fmt.Println("A name for the backup has to be provided with the '--name' flag")
os.Exit(-1)
}
fmt.Print("Restoring ...")
err := ClientDBRestore(TIMEOUT_LONG, StrRemoteHost, StrRemotePort, tmpDBBackupName)
if err != nil {
fmt.Println(" failed")
fmt.Println(err.Error())
os.Exit(-1)
}
fmt.Println(" success")
},

}

cmdDBList := &cobra.Command{
Use: "list",
Short: "List backups",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
list,err := ClientDBList(TIMEOUT_LONG, StrRemoteHost, StrRemotePort)
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}

if BoolJson {
b, err := json.Marshal(list)
if err == nil {
fmt.Println(string(b))
}
} else {
fmt.Println("Database backups:")
for _, item := range list {
fmt.Println(item)
}
}
},
}



rootCmd.AddCommand(cmdDB)
cmdDB.AddCommand(cmdDBBackup, cmdDBList, cmdDBRestore)

cmdDBBackup.Flags().StringVarP(&tmpDBBackupName, "name", "n", "","Name of backup")
cmdDBRestore.Flags().StringVarP(&tmpDBBackupName, "name", "n", "","Name of backup")

}
4 changes: 3 additions & 1 deletion cli_client/cmd_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
var (
StrRemoteHost string
StrRemotePort string
BoolJson bool
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "P4wnP1_cli",
Short: "P4wnP1 (remote) CLI configuration",
Long: `The cli_client tool could be used to configure P4wnP1
Long: `The CLI client tool could be used to configure P4wnP1
from the command line. The tool relies on RPC so it could be used
remotely.`,
}
Expand All @@ -31,6 +32,7 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringVar(&StrRemoteHost, "host", "localhost", "The host with the listening P4wnP1 RPC server")
rootCmd.PersistentFlags().StringVar(&StrRemotePort, "port", "50051", "The port on which the P4wnP1 RPC server is listening")
rootCmd.PersistentFlags().BoolVar(&BoolJson, "json", false, "Output results as JSON if applicable")

/*
// Cobra also supports local flags, which will only run
Expand Down
Loading

0 comments on commit 7bc35ae

Please sign in to comment.