//Updates the library func (l *library) update() error { var conn *mpd.Client fmt.Println("Connecting to MPD") conn, err := mpd.Dial("tcp", "127.0.0.1:6600") if err != nil { fmt.Println("Error: could not connect to MPD for lib update") return errors.New("Could not connect to MPD!") } defer conn.Close() _, err = conn.Update("") if err != nil { fmt.Println("Error: could not update library!") return err } //Let the update happen time.Sleep(2 * time.Second) songs, err := conn.ListAllInfo("/") if err != nil { fmt.Println("Error: could not retrieve new library!") return err } l.library = songs return nil }
func (m MPDInstance) Render() (item modules.Item) { mpdItem := MPDItem{Name: m.name, Markup: "pango"} mpdFormatData := MPDFormatData{} var client *go_mpd.Client if c, err := go_mpd.Dial("tcp", m.host_name+":"+strconv.Itoa(m.port)); err != nil { log.Error(err.Error()) return nil } else { client = c } defer func(client *go_mpd.Client) { if err := client.Close(); err != nil { log.Error("Failed to disconnect: " + err.Error()) } }(client) if attrs, err := client.Status(); err == nil { if state, ok := attrs["state"]; ok { mpdFormatData.State = state } else { log.Error("Failed to read state.") return nil } } else { log.Error("Failed to obtain status: " + err.Error()) return nil } if attrs, err := client.CurrentSong(); err != nil { log.Error("Failed to obtain current song: " + err.Error()) return nil } else { obj := reflect.ValueOf(&mpdFormatData).Elem() for key, val := range attrs { if f := obj.FieldByName(key); f.IsValid() { val = strings.TrimSpace(val) f.Set(reflect.ValueOf(val)) } } } buffer := bytes.Buffer{} if err := m.template.Execute(&buffer, mpdFormatData); err != nil { log.Error("Failed to render mpd template: " + err.Error()) return nil } else { mpdItem.Text = buffer.String() } item = mpdItem return }
//Create a new queue func newLibrary() *library { var conn *mpd.Client fmt.Println("Connecting to MPD") conn, err := mpd.Dial("tcp", "127.0.0.1:6600") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } defer conn.Close() songs, err := conn.ListAllInfo("/") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } shuffle(songs) return &library{ library: songs, } }
func connClose(con *mpd.Client) { if con != nil { con.Close() } }