|
| 1 | +/* |
| 2 | +Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com> |
| 3 | +
|
| 4 | +This program is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +This program is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/staticbackendhq/backend-go" |
| 25 | +) |
| 26 | + |
| 27 | +// dbListCmd list document in a repository |
| 28 | +var dbCreateCmd = &cobra.Command{ |
| 29 | + Use: `create repo-name json-content`, |
| 30 | + Short: "Create a document.", |
| 31 | + Long: fmt.Sprintf(` |
| 32 | +%s |
| 33 | +
|
| 34 | +To create a document you name the repository and pass a JSON object in a string. |
| 35 | +
|
| 36 | +$> backend db create tasks "{ name: \"task 1\", assign: \"dominic\", done: false }" |
| 37 | + `, |
| 38 | + clbold(clsecondary("Create document")), |
| 39 | + ), |
| 40 | + Run: func(cmd *cobra.Command, args []string) { |
| 41 | + if setBackend() == false { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + tok, ok := getRookToken() |
| 46 | + if !ok { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + if len(args) == 0 { |
| 51 | + fmt.Printf("%s %s %s\n", cldanger("Argument missing"), clerror("repository"), cldanger("please supply a table name.")) |
| 52 | + return |
| 53 | + } else if len(args) == 1 { |
| 54 | + fmt.Printf("%s %s %s\n", cldanger("Argument missing"), clerror("json object"), cldanger("please supply a document json object.")) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + repo, raw := args[0], args[1] |
| 59 | + |
| 60 | + var doc map[string]interface{} |
| 61 | + |
| 62 | + if err := json.Unmarshal([]byte(raw), &raw); err != nil { |
| 63 | + fmt.Printf("%s: %v\n", cldanger("An error occured"), err) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + var result map[string]interface{} |
| 68 | + if err := backend.SudoCreate(tok, repo, doc, &result); err != nil { |
| 69 | + fmt.Printf("%s: %v\n", cldanger("An error occured"), err) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + o := "{\n" |
| 74 | + for k, v := range result { |
| 75 | + o += fmt.Sprintf("\t%s: %v, \n", clsecondary(k), v) |
| 76 | + } |
| 77 | + |
| 78 | + o += "}" |
| 79 | + |
| 80 | + fmt.Println(o) |
| 81 | + }, |
| 82 | +} |
| 83 | + |
| 84 | +func init() { |
| 85 | + dbCmd.AddCommand(dbCreateCmd) |
| 86 | + |
| 87 | + // Here you will define your flags and configuration settings. |
| 88 | + |
| 89 | + // Cobra supports Persistent Flags which will work for this command |
| 90 | + // and all subcommands, e.g.: |
| 91 | + // accountCmd.PersistentFlags().String("foo", "", "A help for foo") |
| 92 | +} |
0 commit comments