10 "github.com/mattermost/mattermost-server/v5/model"
14 url := strings.TrimPrefix(os.Args[1], "-") // -?https://MACHINE/api/v4/files/FILE
15 s := strings.Split(url, "/")
16 machine, fileId := s[2], s[6]
18 netrcPath, ok := os.LookupEnv("NETRC")
20 netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
22 data, err := ioutil.ReadFile(netrcPath)
28 for _, line := range strings.Split(string(data), "\n") {
29 if i := strings.Index(line, "#"); i >= 0 {
32 f := strings.Fields(line)
34 f[0] == "machine" && f[1] == machine &&
35 f[2] == "login" && f[4] == "password" {
36 login, password = f[3], f[5]
40 if login == "" || password == "" {
41 fmt.Fprintln(os.Stderr, "No credentials found for:", machine)
45 Client := model.NewAPIv4Client("https://" + machine)
46 Client.Login(login, password)
47 info, resp := Client.GetFileInfo(fileId)
49 fmt.Fprintln(os.Stderr, resp)
56 fmt.Fprintln(os.Stderr, "Name:", info.Name)
57 fmt.Fprintln(os.Stderr, "Type:", info.MimeType)
58 fmt.Fprintln(os.Stderr, "Size:", info.Size)
59 fmt.Fprintln(os.Stderr, "Download? Ctrl-D/C")
60 os.Stdin.Read(make([]byte, 1))
62 data, _ = Client.GetFile(fileId)
63 if err = ioutil.WriteFile(filename, data, os.FileMode(0666)); err != nil {