MCPcopy Create free account
hub / github.com/dotzero/git-profile / Import

Function Import

cmd/import.go:14–47  ·  view source on GitHub ↗

Import returns `import` command

(cfg storage)

Source from the content-addressed store, hash-verified

12
13// Import returns `import` command
14func 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}

Callers 2

TestImportFunction · 0.85
initMethod · 0.85

Calls 4

PrintErrlnFunction · 0.92
PrintlnFunction · 0.92
StoreMethod · 0.65
SaveMethod · 0.65

Tested by 1

TestImportFunction · 0.68