diff --git a/.golangci.yml b/.golangci.yml index 464cf5e1..9ab04e06 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,6 +6,7 @@ linters: - gochecknoinits - gocritic - gofmt + - gofumpt - golint - misspell - prealloc diff --git a/Makefile b/Makefile index ec0ebe75..6d9c3663 100644 --- a/Makefile +++ b/Makefile @@ -26,12 +26,20 @@ $(blank) endef mod-tidy: - docker run --rm -it -v $(PWD):/s amd64/$(BASE_IMAGE) \ - sh -c "apk add git && cd /s && GOPROXY=direct go get && go mod tidy" + docker run --rm -it -v $(PWD):/s -w /s amd64/$(BASE_IMAGE) \ + sh -c "apk add git && GOPROXY=direct go get && go mod tidy" + +define DOCKERFILE_FORMAT +FROM $(BASE_IMAGE) +RUN apk add --no-cache git +RUN GO111MODULE=on go get mvdan.cc/gofumpt +endef +export DOCKERFILE_FORMAT format: - docker run --rm -it -v $(PWD):/s amd64/$(BASE_IMAGE) \ - sh -c "cd /s && find . -type f -name '*.go' | xargs gofmt -l -w -s" + echo "$$DOCKERFILE_FORMAT" | docker build -q . -f - -t temp + docker run --rm -it -v $(PWD):/s -w /s temp \ + sh -c "find . -type f -name '*.go' | xargs gofumpt -l -w" define DOCKERFILE_TEST FROM amd64/$(BASE_IMAGE) diff --git a/internal/hlsconverter/converter.go b/internal/hlsconverter/converter.go index e3f96345..c4ddc2cd 100644 --- a/internal/hlsconverter/converter.go +++ b/internal/hlsconverter/converter.go @@ -162,7 +162,6 @@ func New( pathName string, pathMan PathMan, parent Parent) *Converter { - ctx, ctxCancel := context.WithCancel(ctxParent) c := &Converter{ @@ -425,7 +424,6 @@ func (c *Converter) runInner(innerCtx context.Context) error { } c.tsMutex.Unlock() } - } else { if !curTSFile.firstPacketWritten { continue diff --git a/internal/hlsserver/server.go b/internal/hlsserver/server.go index 8543254d..e603feba 100644 --- a/internal/hlsserver/server.go +++ b/internal/hlsserver/server.go @@ -51,7 +51,6 @@ func New( pathMan *pathman.PathManager, parent Parent, ) (*Server, error) { - ln, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 5a94b904..b5a8b2dc 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -57,7 +57,7 @@ func New(level Level, destinations map[Destination]struct{}, filePath string) (* if _, ok := destinations[DestinationFile]; ok { var err error - lh.file, err = os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + lh.file, err = os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) if err != nil { lh.Close() return nil, err diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index a238abed..0140b18f 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -38,7 +38,6 @@ func New( stats *stats.Stats, parent Parent, ) (*Metrics, error) { - listener, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/path/path.go b/internal/path/path.go index d8aaedeb..ce4125d8 100644 --- a/internal/path/path.go +++ b/internal/path/path.go @@ -117,7 +117,6 @@ func New( wg *sync.WaitGroup, stats *stats.Stats, parent Parent) *Path { - ctx, ctxCancel := context.WithCancel(ctxParent) pa := &Path{ @@ -171,7 +170,6 @@ func (pa *Path) run() { if pa.conf.Source == "redirect" { pa.source = &sourceRedirect{} - } else if pa.hasExternalSource() && !pa.conf.SourceOnDemand { pa.startExternalSource() } @@ -339,7 +337,6 @@ func (pa *Path) startExternalSource() { &pa.sourceWg, pa.stats, pa) - } else if strings.HasPrefix(pa.conf.Source, "rtmp://") { pa.source = rtmpsource.New( pa.ctx, diff --git a/internal/pathman/pathman.go b/internal/pathman/pathman.go index f223dadb..f320d6d1 100644 --- a/internal/pathman/pathman.go +++ b/internal/pathman/pathman.go @@ -76,7 +76,6 @@ func New( pathConfs map[string]*conf.PathConf, stats *stats.Stats, parent Parent) *PathManager { - ctx, ctxCancel := context.WithCancel(ctxParent) pm := &PathManager{ @@ -361,7 +360,6 @@ func (pm *PathManager) authenticate( pathUser string, pathPass string, ) error { - // validate ip if pathIPs != nil && ip != nil { if !ipEqualOrInRange(ip, pathIPs) { diff --git a/internal/pprof/pprof.go b/internal/pprof/pprof.go index 14089166..16252bac 100644 --- a/internal/pprof/pprof.go +++ b/internal/pprof/pprof.go @@ -27,7 +27,6 @@ func New( address string, parent Parent, ) (*PPROF, error) { - listener, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/rtcpsenderset/rtcpsenderset.go b/internal/rtcpsenderset/rtcpsenderset.go index d89e6b53..58980b16 100644 --- a/internal/rtcpsenderset/rtcpsenderset.go +++ b/internal/rtcpsenderset/rtcpsenderset.go @@ -24,7 +24,6 @@ func New( tracks gortsplib.Tracks, onFrame func(int, gortsplib.StreamType, []byte), ) *RTCPSenderSet { - s := &RTCPSenderSet{ onFrame: onFrame, terminate: make(chan struct{}), diff --git a/internal/rtmpconn/conn.go b/internal/rtmpconn/conn.go index 839b9a2b..17b4759b 100644 --- a/internal/rtmpconn/conn.go +++ b/internal/rtmpconn/conn.go @@ -95,7 +95,6 @@ func New( nconn net.Conn, pathMan PathMan, parent Parent) *Conn { - ctx, ctxCancel := context.WithCancel(ctxParent) c := &Conn{ @@ -218,7 +217,8 @@ func (c *Conn) runRead(ctx context.Context) error { ValidateCredentials: func(authMethods []headers.AuthMethod, pathUser string, pathPass string) error { return c.validateCredentials(pathUser, pathPass, query) }, - Res: sres}) + Res: sres, + }) res := <-sres if res.Err != nil { @@ -527,7 +527,6 @@ func (c *Conn) validateCredentials( pathPass string, query url.Values, ) error { - if query.Get("user") != pathUser || query.Get("pass") != pathPass { return readpublisher.ErrAuthCritical{ diff --git a/internal/rtmpserver/server.go b/internal/rtmpserver/server.go index be6b451f..7decbfb5 100644 --- a/internal/rtmpserver/server.go +++ b/internal/rtmpserver/server.go @@ -52,7 +52,6 @@ func New( stats *stats.Stats, pathMan *pathman.PathManager, parent Parent) (*Server, error) { - l, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/rtmpsource/source.go b/internal/rtmpsource/source.go index 09aeb612..84d388c5 100644 --- a/internal/rtmpsource/source.go +++ b/internal/rtmpsource/source.go @@ -53,7 +53,6 @@ func New( wg *sync.WaitGroup, stats *stats.Stats, parent Parent) *Source { - ctx, ctxCancel := context.WithCancel(ctxParent) s := &Source{ @@ -72,6 +71,7 @@ func New( s.wg.Add(1) go s.run() + return s } diff --git a/internal/rtspconn/conn.go b/internal/rtspconn/conn.go index fb911073..f9aa77d3 100644 --- a/internal/rtspconn/conn.go +++ b/internal/rtspconn/conn.go @@ -70,7 +70,6 @@ func New( stats *stats.Stats, conn *gortsplib.ServerConn, parent Parent) *Conn { - c := &Conn{ rtspAddress: rtspAddress, readTimeout: readTimeout, @@ -190,7 +189,6 @@ func (c *Conn) ValidateCredentials( pathName string, req *base.Request, ) error { - // reset authValidator every time the credentials change if c.authValidator == nil || c.authUser != pathUser || c.authPass != pathPass { c.authUser = pathUser diff --git a/internal/rtspserver/server.go b/internal/rtspserver/server.go index 1b022a5e..fefd2cf1 100644 --- a/internal/rtspserver/server.go +++ b/internal/rtspserver/server.go @@ -90,7 +90,6 @@ func New( stats *stats.Stats, pathMan *pathman.PathManager, parent Parent) (*Server, error) { - ctx, ctxCancel := context.WithCancel(ctxParent) s := &Server{ diff --git a/internal/rtspsession/session.go b/internal/rtspsession/session.go index cc281b22..0e479a4e 100644 --- a/internal/rtspsession/session.go +++ b/internal/rtspsession/session.go @@ -58,7 +58,6 @@ func New( sc *gortsplib.ServerConn, pathMan PathMan, parent Parent) *Session { - s := &Session{ rtspAddress: rtspAddress, protocols: protocols, diff --git a/internal/rtspsource/source.go b/internal/rtspsource/source.go index c3e1b478..8484e6e0 100644 --- a/internal/rtspsource/source.go +++ b/internal/rtspsource/source.go @@ -60,7 +60,6 @@ func New( wg *sync.WaitGroup, stats *stats.Stats, parent Parent) *Source { - ctx, ctxCancel := context.WithCancel(ctxParent) s := &Source{ @@ -83,6 +82,7 @@ func New( s.wg.Add(1) go s.run() + return s } diff --git a/main.go b/main.go index 0a816b2a..6b3c3847 100644 --- a/main.go +++ b/main.go @@ -112,7 +112,8 @@ func (p *program) Log(level logger.Level, format string, args ...interface{}) { countReaders := atomic.LoadInt64(p.stats.CountReaders) p.logger.Log(level, "[%d/%d] "+format, append([]interface{}{ - countPublishers, countReaders}, args...)...) + countPublishers, countReaders, + }, args...)...) } func (p *program) run() { diff --git a/main_rtspreadpub_test.go b/main_rtspreadpub_test.go index dbf29186..56cf5758 100644 --- a/main_rtspreadpub_test.go +++ b/main_rtspreadpub_test.go @@ -863,7 +863,7 @@ wait require.NoError(t, err) defer os.Remove(onDemandFile) - err = os.Chmod(onDemandFile, 0755) + err = os.Chmod(onDemandFile, 0o755) require.NoError(t, err) t.Run("describe", func(t *testing.T) { diff --git a/main_test.go b/main_test.go index 16009634..eeda88c3 100644 --- a/main_test.go +++ b/main_test.go @@ -60,9 +60,11 @@ func newContainer(image string, name string, args []string) (*container, error) exec.Command("docker", "kill", "rtsp-simple-server-test-"+name).Run() exec.Command("docker", "wait", "rtsp-simple-server-test-"+name).Run() - cmd := []string{"docker", "run", + cmd := []string{ + "docker", "run", "--name=rtsp-simple-server-test-" + name, - "rtsp-simple-server-test-" + image} + "rtsp-simple-server-test-" + image, + } cmd = append(cmd, args...) ecmd := exec.Command(cmd[0], cmd[1:]...) ecmd.Stdout = nil @@ -189,7 +191,7 @@ func TestHotReloading(t *testing.T) { " runOnInit: echo aaa\n"+ " test4:\n"+ " runOnInit: echo bbb\n"), - 0644) + 0o644) require.NoError(t, err) defer os.Remove(confPath) @@ -217,7 +219,7 @@ func TestHotReloading(t *testing.T) { " test3:\n"+ " test4:\n"+ " runOnInit: echo bbb\n"), - 0644) + 0o644) require.NoError(t, err) time.Sleep(1 * time.Second)