From 2cef7791062c018b2779893ac7ce9e5d1ae90b38 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 18 May 2019 12:33:51 +0100 Subject: [PATCH] Rename command line `sql` command to `import` --- cli/cli.go | 2 +- cli/{sql.go => import.go} | 55 +++++++++++++++------------------------ 2 files changed, 22 insertions(+), 35 deletions(-) rename cli/{sql.go => import.go} (65%) diff --git a/cli/cli.go b/cli/cli.go index 025741d4..5de54843 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -32,8 +32,8 @@ var mainCmd = &cobra.Command{ func init() { mainCmd.AddCommand( - sqlCmd, startCmd, + importCmd, exportCmd, versionCmd, ) diff --git a/cli/sql.go b/cli/import.go similarity index 65% rename from cli/sql.go rename to cli/import.go index 9deb87da..a2b701d6 100644 --- a/cli/sql.go +++ b/cli/import.go @@ -27,18 +27,17 @@ import ( ) var ( - sqlForm string - sqlUser string - sqlPass string - sqlConn string - sqlNS string - sqlDB string + importUser string + importPass string + importConn string + importNS string + importDB string ) -var sqlCmd = &cobra.Command{ - Use: "sql [flags] ", +var importCmd = &cobra.Command{ + Use: "import [flags] ", Short: "Execute a SQL script against an existing database", - Example: " surreal sql --auth root:root backup.sql", + Example: " surreal import --auth root:root backup.sql", RunE: func(cmd *cobra.Command, args []string) (err error) { var fle *os.File @@ -69,39 +68,28 @@ var sqlCmd = &cobra.Command{ // and specify the authentication header using // basic auth for root login. - url := fmt.Sprintf("%s/sql", sqlConn) + url := fmt.Sprintf("%s/sql", importConn) if req, err = http.NewRequest("POST", url, fle); err != nil { log.Fatalln("Connection failed - check the connection details and try again.") return } + // Specify that the request is plain text + + req.Header.Set("Content-Type", "text/plain") + // Specify the db authentication settings - req.SetBasicAuth(sqlUser, sqlPass) + req.SetBasicAuth(importUser, importPass) // Specify the namespace to import - req.Header.Set("NS", sqlNS) + req.Header.Set("NS", importNS) // Specify the database to import - req.Header.Set("DB", sqlDB) - - // Specify that the request is an octet stream - // so that we can stream the file contents to - // the server without reading the whole file. - - switch sqlForm { - case "pack": - req.Header.Set("Content-Type", "application/msgpack") - case "json": - req.Header.Set("Content-Type", "application/json") - case "cork": - req.Header.Set("Content-Type", "application/cork") - default: - req.Header.Set("Content-Type", "text/plain") - } + req.Header.Set("DB", importDB) // Attempt to dial the sql endpoint and // if there is an error then stop execution @@ -144,11 +132,10 @@ var sqlCmd = &cobra.Command{ func init() { - sqlCmd.PersistentFlags().StringVarP(&sqlUser, "user", "u", "root", "Database authentication username to use when connecting.") - sqlCmd.PersistentFlags().StringVarP(&sqlPass, "pass", "p", "pass", "Database authentication password to use when connecting.") - sqlCmd.PersistentFlags().StringVarP(&sqlConn, "conn", "c", "https://surreal.io", "Remote database server url to connect to.") - sqlCmd.PersistentFlags().StringVarP(&sqlForm, "format", "f", "text", "The output format for the server response data.") - sqlCmd.PersistentFlags().StringVar(&sqlNS, "ns", "", "Master authentication details to use when connecting.") - sqlCmd.PersistentFlags().StringVar(&sqlDB, "db", "", "Master authentication details to use when connecting.") + importCmd.PersistentFlags().StringVarP(&importUser, "user", "u", "root", "Database authentication username to use when connecting.") + importCmd.PersistentFlags().StringVarP(&importPass, "pass", "p", "pass", "Database authentication password to use when connecting.") + importCmd.PersistentFlags().StringVarP(&importConn, "conn", "c", "https://surreal.io", "Remote database server url to connect to.") + importCmd.PersistentFlags().StringVar(&importNS, "ns", "", "Master authentication details to use when connecting.") + importCmd.PersistentFlags().StringVar(&importDB, "db", "", "Master authentication details to use when connecting.") }