NewInitCmd returns a 'init' command.
()
| 43 | |
| 44 | // NewInitCmd returns a 'init' command. |
| 45 | func NewInitCmd() *cobra.Command { |
| 46 | return &cobra.Command{ |
| 47 | Use: "init", |
| 48 | Short: "Initialize or re-initialize vt command-line tool", |
| 49 | Long: initCmdHelp, |
| 50 | |
| 51 | Run: func(cmd *cobra.Command, args []string) { |
| 52 | |
| 53 | fmt.Print(vtBanner) |
| 54 | |
| 55 | apiKey := cmd.Flags().Lookup("apikey").Value.String() |
| 56 | |
| 57 | if apiKey == "" { |
| 58 | fmt.Print("Enter your API key: ") |
| 59 | fmt.Scanln(&apiKey) |
| 60 | } |
| 61 | |
| 62 | client := vt.NewClient(apiKey) |
| 63 | |
| 64 | metadata, err := client.GetMetadata() |
| 65 | if err != nil { |
| 66 | fmt.Fprintln(os.Stderr, err) |
| 67 | os.Exit(1) |
| 68 | } |
| 69 | |
| 70 | homeDir, err := os.UserHomeDir() |
| 71 | if err != nil { |
| 72 | fmt.Fprintln(os.Stderr, err) |
| 73 | os.Exit(1) |
| 74 | } |
| 75 | |
| 76 | cacheDir, err := os.UserCacheDir() |
| 77 | if err != nil { |
| 78 | fmt.Fprintln(os.Stderr, err) |
| 79 | os.Exit(1) |
| 80 | } |
| 81 | |
| 82 | if err := os.MkdirAll(cacheDir, 0755); err != nil { |
| 83 | fmt.Fprintln(os.Stderr, err) |
| 84 | os.Exit(1) |
| 85 | } |
| 86 | |
| 87 | relCacheFile, err := os.Create(path.Join(cacheDir, ".vt.relationships.cache")) |
| 88 | if err != nil { |
| 89 | fmt.Fprintln(os.Stderr, err) |
| 90 | os.Exit(1) |
| 91 | } |
| 92 | defer relCacheFile.Close() |
| 93 | |
| 94 | enc := gob.NewEncoder(relCacheFile) |
| 95 | |
| 96 | if err := enc.Encode(metadata.Relationships); err != nil { |
| 97 | fmt.Fprintln(os.Stderr, err) |
| 98 | os.Exit(1) |
| 99 | } |
| 100 | |
| 101 | configFilePath := path.Join(homeDir, ".vt.toml") |
| 102 | configFile, err := os.OpenFile(configFilePath, os.O_CREATE|os.O_WRONLY, 0600) |
no test coverage detected