Skip to content

Commit

Permalink
Removed astilog/errors dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Jan 8, 2020
1 parent b3d9d75 commit c99b9e1
Show file tree
Hide file tree
Showing 17 changed files with 290 additions and 322 deletions.
105 changes: 49 additions & 56 deletions astits/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
Expand All @@ -14,9 +16,7 @@ import (
"syscall"

"github.com/asticode/go-astikit"
"github.com/asticode/go-astilog"
"github.com/asticode/go-astits"
"github.com/pkg/errors"
"github.com/pkg/profile"
)

Expand All @@ -38,9 +38,7 @@ func main() {
}
flag.Var(dataTypes, "d", "the datatypes whitelist (all, pat, pmt, pes, eit, nit, sdt, tot)")
cmd := astikit.FlagCmd()
astilog.SetHandyFlags()
flag.Parse()
astilog.FlagInit()

// Handle signals
handleSignals()
Expand All @@ -56,8 +54,7 @@ func main() {
var r io.Reader
var err error
if r, err = buildReader(ctx); err != nil {
astilog.Error(errors.Wrap(err, "astits: parsing input failed"))
return
log.Fatal(fmt.Errorf("astits: parsing input failed: %w", err))
}

// Make sure the reader is closed properly
Expand All @@ -73,21 +70,18 @@ func main() {
case "data":
// Fetch data
if err = data(dmx); err != nil {
astilog.Error(errors.Wrap(err, "astits: fetching data failed"))
return
log.Fatal(fmt.Errorf("astits: fetching data failed: %w", err))
}
case "packets":
// Fetch packets
if err = packets(dmx); err != nil {
astilog.Error(errors.Wrap(err, "astits: fetching packets failed"))
return
log.Fatal(fmt.Errorf("astits: fetching packets failed: %w", err))
}
default:
// Fetch the programs
var pgms []*Program
if pgms, err = programs(dmx); err != nil {
astilog.Error(errors.Wrap(err, "astits: fetching programs failed"))
return
log.Fatal(fmt.Errorf("astits: fetching programs failed: %w", err))
}

// Print
Expand All @@ -96,13 +90,12 @@ func main() {
var e = json.NewEncoder(os.Stdout)
e.SetIndent("", " ")
if err = e.Encode(pgms); err != nil {
astilog.Error(errors.Wrap(err, "astits: json encoding to stdout failed"))
return
log.Fatal(fmt.Errorf("astits: json encoding to stdout failed: %w", err))
}
default:
fmt.Println("Programs are:")
for _, pgm := range pgms {
fmt.Printf("* %s\n", pgm)
log.Printf("* %s\n", pgm)
}
}
}
Expand All @@ -113,7 +106,7 @@ func handleSignals() {
signal.Notify(ch)
go func() {
for s := range ch {
astilog.Debugf("Received signal %s", s)
log.Printf("Received signal %s\n", s)
switch s {
case syscall.SIGABRT, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
cancel()
Expand All @@ -126,14 +119,14 @@ func handleSignals() {
func buildReader(ctx context.Context) (r io.Reader, err error) {
// Validate input
if len(*inputPath) <= 0 {
err = errors.New("Use -i to indicate an input path")
err = errors.New("use -i to indicate an input path")
return
}

// Parse input
var u *url.URL
if u, err = url.Parse(*inputPath); err != nil {
err = errors.Wrap(err, "astits: parsing input path failed")
err = fmt.Errorf("astits: parsing input path failed: %w", err)
return
}

Expand All @@ -143,14 +136,14 @@ func buildReader(ctx context.Context) (r io.Reader, err error) {
// Resolve addr
var addr *net.UDPAddr
if addr, err = net.ResolveUDPAddr("udp", u.Host); err != nil {
err = errors.Wrapf(err, "astits: resolving udp addr %s failed", u.Host)
err = fmt.Errorf("astits: resolving udp addr %s failed: %w", u.Host, err)
return
}

// Listen to multicast UDP
var c *net.UDPConn
if c, err = net.ListenMulticastUDP("udp", nil, addr); err != nil {
err = errors.Wrapf(err, "astits: listening on multicast udp addr %s failed", u.Host)
err = fmt.Errorf("astits: listening on multicast udp addr %s failed: %w", u.Host, err)
return
}
c.SetReadBuffer(4096)
Expand All @@ -159,7 +152,7 @@ func buildReader(ctx context.Context) (r io.Reader, err error) {
// Open file
var f *os.File
if f, err = os.Open(*inputPath); err != nil {
err = errors.Wrapf(err, "astits: opening %s failed", *inputPath)
err = fmt.Errorf("astits: opening %s failed: %w", *inputPath, err)
return
}
r = f
Expand All @@ -170,28 +163,28 @@ func buildReader(ctx context.Context) (r io.Reader, err error) {
func packets(dmx *astits.Demuxer) (err error) {
// Loop through packets
var p *astits.Packet
astilog.Debug("Fetching packets...")
log.Println("Fetching packets...")
for {
// Get next packet
if p, err = dmx.NextPacket(); err != nil {
if err == astits.ErrNoMorePackets {
break
}
err = errors.Wrap(err, "astits: getting next packet failed")
err = fmt.Errorf("astits: getting next packet failed: %w", err)
return
}

// Log packet
astilog.Infof("PKT: %d", p.Header.PID)
astilog.Infof(" Continuity Counter: %v", p.Header.ContinuityCounter)
astilog.Infof(" Payload Unit Start Indicator: %v", p.Header.PayloadUnitStartIndicator)
astilog.Infof(" Has Payload: %v", p.Header.HasPayload)
astilog.Infof(" Has Adaptation Field: %v", p.Header.HasAdaptationField)
astilog.Infof(" Transport Error Indicator: %v", p.Header.TransportErrorIndicator)
astilog.Infof(" Transport Priority: %v", p.Header.TransportPriority)
astilog.Infof(" Transport Scrambling Control: %v", p.Header.TransportScramblingControl)
log.Printf("PKT: %d\n", p.Header.PID)
log.Printf(" Continuity Counter: %v\n", p.Header.ContinuityCounter)
log.Printf(" Payload Unit Start Indicator: %v\n", p.Header.PayloadUnitStartIndicator)
log.Printf(" Has Payload: %v\n", p.Header.HasPayload)
log.Printf(" Has Adaptation Field: %v\n", p.Header.HasAdaptationField)
log.Printf(" Transport Error Indicator: %v\n", p.Header.TransportErrorIndicator)
log.Printf(" Transport Priority: %v\n", p.Header.TransportPriority)
log.Printf(" Transport Scrambling Control: %v\n", p.Header.TransportScramblingControl)
if p.Header.HasAdaptationField {
astilog.Infof(" Adaptation Field: %+v", p.AdaptationField)
log.Printf(" Adaptation Field: %+v\n", p.AdaptationField)
}
}
return nil
Expand Down Expand Up @@ -227,51 +220,51 @@ func data(dmx *astits.Demuxer) (err error) {

// Loop through data
var d *astits.Data
astilog.Debug("Fetching data...")
log.Println("Fetching data...")
for {
// Get next data
if d, err = dmx.NextData(); err != nil {
if err == astits.ErrNoMorePackets {
break
}
err = errors.Wrap(err, "astits: getting next data failed")
err = fmt.Errorf("astits: getting next data failed: %w", err)
return
}

// Log data
if d.EIT != nil && (logAll || logEIT) {
astilog.Infof("EIT: %d", d.PID)
astilog.Info(eventsToString(d.EIT.Events))
log.Printf("EIT: %d\n", d.PID)
log.Println(eventsToString(d.EIT.Events))
} else if d.NIT != nil && (logAll || logNIT) {
astilog.Infof("NIT: %d", d.PID)
log.Printf("NIT: %d\n", d.PID)
} else if d.PAT != nil && (logAll || logPAT) {
astilog.Infof("PAT: %d", d.PID)
astilog.Infof(" Transport Stream ID: %v", d.PAT.TransportStreamID)
astilog.Infof(" Programs:")
log.Printf("PAT: %d\n", d.PID)
log.Printf(" Transport Stream ID: %v\n", d.PAT.TransportStreamID)
log.Println(" Programs:")
for _, p := range d.PAT.Programs {
astilog.Infof(" %+v", p)
log.Printf(" %+v\n", p)
}
} else if d.PES != nil && (logAll || logPES) {
astilog.Infof("PES: %d", d.PID)
astilog.Infof(" Stream ID: %v", d.PES.Header.StreamID)
astilog.Infof(" Packet Length: %v", d.PES.Header.PacketLength)
astilog.Infof(" Optional Header: %+v", d.PES.Header.OptionalHeader)
log.Printf("PES: %d\n", d.PID)
log.Printf(" Stream ID: %v\n", d.PES.Header.StreamID)
log.Printf(" Packet Length: %v\n", d.PES.Header.PacketLength)
log.Printf(" Optional Header: %+v\n", d.PES.Header.OptionalHeader)
} else if d.PMT != nil && (logAll || logPMT) {
astilog.Infof("PMT: %d", d.PID)
astilog.Infof(" ProgramNumber: %v", d.PMT.ProgramNumber)
astilog.Infof(" PCR PID: %v", d.PMT.PCRPID)
astilog.Infof(" Elementary Streams:")
log.Printf("PMT: %d\n", d.PID)
log.Printf(" ProgramNumber: %v\n", d.PMT.ProgramNumber)
log.Printf(" PCR PID: %v\n", d.PMT.PCRPID)
log.Println(" Elementary Streams:")
for _, s := range d.PMT.ElementaryStreams {
astilog.Infof(" %+v", s)
log.Printf(" %+v\n", s)
}
astilog.Infof(" Program Descriptors:")
log.Println(" Program Descriptors:")
for _, d := range d.PMT.ProgramDescriptors {
astilog.Infof(" %+v", d)
log.Printf(" %+v\n", d)
}
} else if d.SDT != nil && (logAll || logSDT) {
astilog.Infof("SDT: %d", d.PID)
log.Printf("SDT: %d\n", d.PID)
} else if d.TOT != nil && (logAll || logTOT) {
astilog.Infof("TOT: %d", d.PID)
log.Printf("TOT: %d\n", d.PID)
}
}
return
Expand All @@ -282,15 +275,15 @@ func programs(dmx *astits.Demuxer) (o []*Program, err error) {
var d *astits.Data
var pgmsToProcess = make(map[uint16]bool)
var pgms = make(map[uint16]*Program)
astilog.Debug("Fetching data...")
log.Println("Fetching data...")
for {
// Get next data
if d, err = dmx.NextData(); err != nil {
if err == astits.ErrNoMorePackets {
err = nil
break
}
err = errors.Wrap(err, "astits: getting next data failed")
err = fmt.Errorf("astits: getting next data failed: %w", err)
return
}

Expand Down
9 changes: 5 additions & 4 deletions data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package astits

import (
"fmt"

"github.com/asticode/go-astikit"
"github.com/pkg/errors"
)

// PIDs
Expand Down Expand Up @@ -32,7 +33,7 @@ func parseData(ps []*Packet, prs PacketsParser, pm programMap) (ds []*Data, err
if prs != nil {
var skip bool
if ds, skip, err = prs(ps); err != nil {
err = errors.Wrap(err, "astits: custom packets parsing failed")
err = fmt.Errorf("astits: custom packets parsing failed: %w", err)
return
} else if skip {
return
Expand Down Expand Up @@ -66,7 +67,7 @@ func parseData(ps []*Packet, prs PacketsParser, pm programMap) (ds []*Data, err
// Parse PSI data
var psiData *PSIData
if psiData, err = parsePSIData(i); err != nil {
err = errors.Wrap(err, "astits: parsing PSI data failed")
err = fmt.Errorf("astits: parsing PSI data failed: %w", err)
return
}

Expand All @@ -76,7 +77,7 @@ func parseData(ps []*Packet, prs PacketsParser, pm programMap) (ds []*Data, err
// Parse PES data
var pesData *PESData
if pesData, err = parsePESData(i); err != nil {
err = errors.Wrap(err, "astits: parsing PES data failed")
err = fmt.Errorf("astits: parsing PES data failed: %w", err)
return
}

Expand Down
20 changes: 10 additions & 10 deletions data_eit.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package astits

import (
"fmt"
"time"

"github.com/asticode/go-astikit"
"github.com/pkg/errors"
)

// EITData represents an EIT data
Expand Down Expand Up @@ -36,7 +36,7 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt
// Get next 2 bytes
var bs []byte
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}

Expand All @@ -45,7 +45,7 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt

// Get next 2 bytes
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}

Expand All @@ -55,7 +55,7 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt
// Get next byte
var b byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
err = fmt.Errorf("astits: fetching next byte failed: %w", err)
return
}

Expand All @@ -64,7 +64,7 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt

// Get next byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
err = fmt.Errorf("astits: fetching next byte failed: %w", err)
return
}

Expand All @@ -75,7 +75,7 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt
for i.Offset() < offsetSectionsEnd {
// Get next 2 bytes
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}

Expand All @@ -85,19 +85,19 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt

// Start time
if e.StartTime, err = parseDVBTime(i); err != nil {
err = errors.Wrap(err, "astits: parsing DVB time")
err = fmt.Errorf("astits: parsing DVB time")
return
}

// Duration
if e.Duration, err = parseDVBDurationSeconds(i); err != nil {
err = errors.Wrap(err, "astits: parsing DVB duration seconds failed")
err = fmt.Errorf("astits: parsing DVB duration seconds failed: %w", err)
return
}

// Get next byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
err = fmt.Errorf("astits: fetching next byte failed: %w", err)
return
}

Expand All @@ -112,7 +112,7 @@ func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExt

// Descriptors
if e.Descriptors, err = parseDescriptors(i); err != nil {
err = errors.Wrap(err, "astits: parsing descriptors failed")
err = fmt.Errorf("astits: parsing descriptors failed: %w", err)
return
}

Expand Down
Loading

0 comments on commit c99b9e1

Please sign in to comment.