package main import ( "fmt" "io/ioutil" "os" "path/filepath" "strings" "github.com/mattermost/mattermost-server/v5/model" ) func main() { url := strings.TrimPrefix(os.Args[1], "-") // -?https://MACHINE/api/v4/files/FILE s := strings.Split(url, "/") machine, fileId := s[2], s[6] netrcPath, ok := os.LookupEnv("NETRC") if !ok { netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc") } data, err := ioutil.ReadFile(netrcPath) if err != nil { panic(err) } var login string var password string for _, line := range strings.Split(string(data), "\n") { if i := strings.Index(line, "#"); i >= 0 { line = line[:i] } f := strings.Fields(line) if len(f) >= 6 && f[0] == "machine" && f[1] == machine && f[2] == "login" && f[4] == "password" { login, password = f[3], f[5] break } } if login == "" || password == "" { fmt.Fprintln(os.Stderr, "No credentials found for:", machine) os.Exit(1) } Client := model.NewAPIv4Client("https://" + machine) Client.Login(login, password) info, _ := Client.GetFileInfo(fileId) if info == nil { os.Exit(1) } filename := info.Name if len(os.Args) > 2 { filename = os.Args[2] } else { fmt.Fprintln(os.Stderr, "Name:", info.Name) fmt.Fprintln(os.Stderr, "Type:", info.MimeType) fmt.Fprintln(os.Stderr, "Size:", info.Size) fmt.Fprintln(os.Stderr, "Download? Ctrl-D/C") os.Stdin.Read(make([]byte, 1)) } data, _ = Client.GetFile(fileId) if err = ioutil.WriteFile(filename, data, os.FileMode(0666)); err != nil { panic(err) } }