Import returns `import` command
(cfg storage)
| 12 | |
| 13 | // Import returns `import` command |
| 14 | func Import(cfg storage) *cobra.Command { |
| 15 | return &cobra.Command{ |
| 16 | Use: "import [profile] [json-values]", |
| 17 | Aliases: []string{"i"}, |
| 18 | Short: "Import a profile", |
| 19 | Long: "Import a profile from JSON.", |
| 20 | Args: cobra.ExactArgs(2), |
| 21 | Example: `git-profile import my-profile '[{"key":"user.email","value":"work@example.com"}]'`, |
| 22 | Run: func(cmd *cobra.Command, args []string) { |
| 23 | profile := args[0] |
| 24 | filename, _ := cmd.Flags().GetString("config") |
| 25 | |
| 26 | var entries []config.Entry |
| 27 | |
| 28 | err := json.Unmarshal([]byte(args[1]), &entries) |
| 29 | if err != nil { |
| 30 | ui.PrintErrln(cmd, ui.ErrorStyle, "Unable to decode profile values: %s", err) |
| 31 | os.Exit(1) |
| 32 | } |
| 33 | |
| 34 | for _, entry := range entries { |
| 35 | cfg.Store(profile, entry.Key, entry.Value) |
| 36 | } |
| 37 | |
| 38 | err = cfg.Save(filename) |
| 39 | if err != nil { |
| 40 | ui.PrintErrln(cmd, ui.ErrorStyle, "Unable to save config file: %s", err) |
| 41 | os.Exit(1) |
| 42 | } |
| 43 | |
| 44 | ui.Println(cmd, ui.SuccessStyle, "Successfully imported `%s` profile.", profile) |
| 45 | }, |
| 46 | } |
| 47 | } |