2016-02-26 17:25:32 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2017-02-19 19:25:47 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2017-11-16 19:28:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
|
2016-02-26 17:25:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
2017-02-19 19:25:47 +00:00
|
|
|
|
|
|
|
"github.com/abcum/surreal/log"
|
2016-02-26 17:25:32 +00:00
|
|
|
)
|
|
|
|
|
2018-08-20 06:53:11 +00:00
|
|
|
var (
|
|
|
|
exportUser string
|
|
|
|
exportPass string
|
|
|
|
exportConn string
|
|
|
|
exportNS string
|
|
|
|
exportDB string
|
|
|
|
)
|
|
|
|
|
2016-02-26 17:25:32 +00:00
|
|
|
var exportCmd = &cobra.Command{
|
2017-02-19 19:25:47 +00:00
|
|
|
Use: "export [flags] <file>",
|
2017-02-16 10:00:06 +00:00
|
|
|
Short: "Export data from an existing database",
|
2018-08-20 06:53:11 +00:00
|
|
|
Example: " surreal export --auth root:root backup.sql",
|
2017-02-19 19:25:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
|
|
|
|
var fle *os.File
|
|
|
|
var req *http.Request
|
|
|
|
var res *http.Response
|
|
|
|
|
|
|
|
// Ensure that the command has a filepath
|
|
|
|
// as the output file argument. If no filepath
|
|
|
|
// has been provided then return an error.
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
log.Fatalln("No filepath provided.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to open or create the specified file
|
|
|
|
// in write-only mode, and if there is a problem
|
|
|
|
// creating the file, then return an error.
|
|
|
|
|
|
|
|
if fle, err = os.OpenFile(args[0], os.O_CREATE|os.O_WRONLY, 0644); err != nil {
|
|
|
|
log.Fatalln("Export failed - please check the filepath and try again.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fle.Close()
|
|
|
|
|
|
|
|
// Create a new http request object that we
|
|
|
|
// can use to connect to the export endpoint
|
|
|
|
// using a GET http request type.
|
|
|
|
|
2018-08-20 06:53:11 +00:00
|
|
|
url := fmt.Sprintf("%s/export", exportConn)
|
|
|
|
|
2017-02-19 19:25:47 +00:00
|
|
|
if req, err = http.NewRequest("GET", url, nil); err != nil {
|
|
|
|
log.Fatalln("Connection failed - check the connection details and try again.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specify that the request is an octet stream
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/octet-stream")
|
|
|
|
|
2018-08-20 06:53:11 +00:00
|
|
|
// Specify the db authentication settings
|
|
|
|
|
|
|
|
req.SetBasicAuth(exportUser, exportPass)
|
|
|
|
|
|
|
|
// Specify the namespace to export
|
|
|
|
|
|
|
|
req.Header.Set("NS", exportNS)
|
|
|
|
|
|
|
|
// Specify the database to export
|
|
|
|
|
|
|
|
req.Header.Set("DB", exportDB)
|
|
|
|
|
2017-02-19 19:25:47 +00:00
|
|
|
// Attempt to dial the export endpoint and
|
|
|
|
// if there is an error then stop execution
|
|
|
|
// and return the connection error.
|
|
|
|
|
|
|
|
if res, err = http.DefaultClient.Do(req); err != nil {
|
|
|
|
log.Fatalln("Connection failed - check the connection details and try again.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:28:41 +00:00
|
|
|
// Ensure that we close the body, otherwise
|
|
|
|
// if the Body is not closed, the Client can
|
|
|
|
// not use the underlying transport again.
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
// Ensure that we didn't receive a 401 status
|
2017-02-19 19:25:47 +00:00
|
|
|
// code back from the server, otherwise there
|
|
|
|
// was a problem with our authentication.
|
|
|
|
|
2017-11-16 19:28:41 +00:00
|
|
|
if res.StatusCode == 401 {
|
|
|
|
log.Fatalln("Authentication failed - check the connection details and try again.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we received a http 200 status
|
|
|
|
// code back from the server, otherwise there
|
|
|
|
// was a problem with our request.
|
|
|
|
|
2017-02-19 19:25:47 +00:00
|
|
|
if res.StatusCode != 200 {
|
2017-11-16 19:28:41 +00:00
|
|
|
bdy, _ := ioutil.ReadAll(res.Body)
|
|
|
|
log.Fatalf("%s", bdy)
|
2017-02-19 19:25:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the http response body to stdOut so
|
|
|
|
// that we can pipe the response to other
|
|
|
|
// commands or processes.
|
|
|
|
|
|
|
|
if _, err = io.Copy(fle, res.Body); err != nil {
|
|
|
|
log.Fatalln("Export failed - there was an error saving the database content.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-02-26 17:25:32 +00:00
|
|
|
},
|
|
|
|
}
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
2018-08-20 06:53:11 +00:00
|
|
|
exportCmd.PersistentFlags().StringVarP(&exportUser, "user", "u", "root", "Database authentication username to use when connecting.")
|
|
|
|
exportCmd.PersistentFlags().StringVarP(&exportPass, "pass", "p", "pass", "Database authentication password to use when connecting.")
|
|
|
|
exportCmd.PersistentFlags().StringVarP(&exportConn, "conn", "c", "https://surreal.io", "Remote database server url to connect to.")
|
|
|
|
exportCmd.PersistentFlags().StringVar(&exportNS, "ns", "", "Master authentication details to use when connecting.")
|
|
|
|
exportCmd.PersistentFlags().StringVar(&exportDB, "db", "", "Master authentication details to use when connecting.")
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
}
|