diff --git a/main.go b/main.go index b6e1ad3..47a641e 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,13 @@ package main import ( - "encoding/json" "fmt" - "io/ioutil" "log" "net/http" "os" "github.com/c-bata/go-prompt" "github.com/indiependente/autoEqMac/autoeq" - "github.com/indiependente/autoEqMac/eqmac" "github.com/indiependente/autoEqMac/eqmac/mapping" "github.com/indiependente/autoEqMac/server" "gopkg.in/alecthomas/kingpin.v2" @@ -69,19 +66,18 @@ func run() error { if err != nil { return fmt.Errorf("could not find fixed band EQ preset: %w", err) } - - jsonPreset, err := json.Marshal([]eqmac.EQPreset{eqPreset}) - if err != nil { - return fmt.Errorf("could not marshal preset to JSON: %w", err) - } + out := os.Stdout if *file != "" { - err := ioutil.WriteFile(*file, jsonPreset, os.ModePerm) + f, err := os.Create(*file) if err != nil { - return fmt.Errorf("could not write preset to file: %w", err) + return fmt.Errorf("could not create preset file: %w", err) } - return nil + out = f + } + err = srv.WritePreset(out, eqPreset) + if err != nil { + return fmt.Errorf("could not write preset to file: %w", err) } - fmt.Printf("%s\n", string(jsonPreset)) return nil } diff --git a/server/interface.go b/server/interface.go index 7e13202..e7c79b9 100644 --- a/server/interface.go +++ b/server/interface.go @@ -1,6 +1,8 @@ package server import ( + "io" + "github.com/indiependente/autoEqMac/autoeq" "github.com/indiependente/autoEqMac/eqmac" ) @@ -9,4 +11,5 @@ type Server interface { ListEQsMetadata() ([]autoeq.EQMetadata, error) GetFixedBandEQPreset(id string) (eqmac.EQPreset, error) GetEQMetadataByName(name string) (autoeq.EQMetadata, error) + WritePreset(w io.Writer, p eqmac.EQPreset) error } diff --git a/server/server.go b/server/server.go index eb98ffa..44631a5 100644 --- a/server/server.go +++ b/server/server.go @@ -1,7 +1,9 @@ package server import ( + "encoding/json" "fmt" + "io" "io/ioutil" "net/http" @@ -103,3 +105,12 @@ func (s HTTPServer) GetEQMetadataByName(name string) (autoeq.EQMetadata, error) } return eqMeta, nil } + +func (s HTTPServer) WritePreset(w io.Writer, p eqmac.EQPreset) error { + jsonPreset, err := json.Marshal([]eqmac.EQPreset{p}) + if err != nil { + return fmt.Errorf("could not marshal preset to JSON: %w", err) + } + _, err = fmt.Fprintln(w, string(jsonPreset)) + return err +}