Fix code formatting according to go-staticcheck

This commit is contained in:
kebyn 2022-03-12 13:16:45 +00:00
parent c07aaffe65
commit d8b2675fda
15 changed files with 309 additions and 308 deletions

View File

@ -9,10 +9,10 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/pkg/profile"
"gopkg.in/op/go-logging.v1"
"github.com/urfave/cli"
"github.com/moby/moby/pkg/reexec"
"github.com/pkg/profile"
"github.com/urfave/cli"
"gopkg.in/op/go-logging.v1"
tunasync "github.com/tuna/tunasync/internal"
"github.com/tuna/tunasync/manager"

View File

@ -12,9 +12,9 @@ import (
"golang.org/x/sys/unix"
"github.com/moby/moby/pkg/reexec"
cgv1 "github.com/containerd/cgroups"
cgv2 "github.com/containerd/cgroups/v2"
"github.com/moby/moby/pkg/reexec"
contspecs "github.com/opencontainers/runtime-spec/specs-go"
)
@ -51,14 +51,15 @@ func waitExec () {
panic(err)
}
if err := pipe.Close(); err != nil {
panic(err)
}
cmd := execCmd(string(cmdBytes))
switch cmd {
case cmdAbrt:
fallthrough
case cmdCont:
default:
panic("Exited on request")
case cmdCont:
}
}
}
@ -71,7 +72,7 @@ func waitExec () {
panic("Exec failed.")
}
func initCgroup(cfg *cgroupConfig) (error) {
func initCgroup(cfg *cgroupConfig) error {
logger.Debugf("Initializing cgroup")
baseGroup := cfg.Group
@ -103,7 +104,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
}
if baseGroup == "" {
logger.Debugf("Creating a sub group and move all processes into it")
wkrMgr, err := cfg.cgMgrV2.NewChild("__worker", nil);
wkrMgr, err := cfg.cgMgrV2.NewChild("__worker", nil)
if err != nil {
return err
}
@ -117,7 +118,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
if len(procs) == 0 {
break
}
for _, p := range(procs) {
for _, p := range procs {
if err := wkrMgr.AddProc(p); err != nil {
if errors.Is(err, syscall.ESRCH) {
logger.Debugf("Write pid %d to sub group failed: process vanished, ignoring")
@ -129,7 +130,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
}
} else {
logger.Debugf("Trying to create a sub group in that group")
testMgr, err := cfg.cgMgrV2.NewChild("__test", nil);
testMgr, err := cfg.cgMgrV2.NewChild("__test", nil)
if err != nil {
logger.Errorf("Cannot create a sub group in the cgroup")
return err
@ -143,7 +144,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
return err
}
if len(procs) != 0 {
return fmt.Errorf("There are remaining processes in cgroup %s", baseGroup)
return fmt.Errorf("there are remaining processes in cgroup %s", baseGroup)
}
}
} else {
@ -152,9 +153,9 @@ func initCgroup(cfg *cgroupConfig) (error) {
if baseGroup != "" {
pather = cgv1.StaticPath(baseGroup)
} else {
pather = (func(p cgv1.Path) (cgv1.Path){
pather = (func(p cgv1.Path) cgv1.Path {
return func(subsys cgv1.Name) (string, error) {
path, err := p(subsys);
path, err := p(subsys)
if err != nil {
return "", err
}
@ -174,7 +175,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
return err
}
logger.Debugf("Available subsystems:")
for _, subsys := range(cfg.cgMgrV1.Subsystems()) {
for _, subsys := range cfg.cgMgrV1.Subsystems() {
p, err := pather(subsys.Name())
if err != nil {
return err
@ -183,11 +184,11 @@ func initCgroup(cfg *cgroupConfig) (error) {
}
if baseGroup == "" {
logger.Debugf("Creating a sub group and move all processes into it")
wkrMgr, err := cfg.cgMgrV1.New("__worker", &contspecs.LinuxResources{});
wkrMgr, err := cfg.cgMgrV1.New("__worker", &contspecs.LinuxResources{})
if err != nil {
return err
}
for _, subsys := range(cfg.cgMgrV1.Subsystems()) {
for _, subsys := range cfg.cgMgrV1.Subsystems() {
logger.Debugf("Reading pids for subsystem %s", subsys.Name())
for {
procs, err := cfg.cgMgrV1.Processes(subsys.Name(), false)
@ -202,7 +203,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
if len(procs) == 0 {
break
}
for _, proc := range(procs) {
for _, proc := range procs {
if err := wkrMgr.Add(proc); err != nil {
if errors.Is(err, syscall.ESRCH) {
logger.Debugf("Write pid %d to sub group failed: process vanished, ignoring")
@ -215,7 +216,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
}
} else {
logger.Debugf("Trying to create a sub group in that group")
testMgr, err := cfg.cgMgrV1.New("__test", &contspecs.LinuxResources{});
testMgr, err := cfg.cgMgrV1.New("__test", &contspecs.LinuxResources{})
if err != nil {
logger.Errorf("Cannot create a sub group in the cgroup")
return err
@ -223,7 +224,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
if err := testMgr.Delete(); err != nil {
return err
}
for _, subsys := range(cfg.cgMgrV1.Subsystems()) {
for _, subsys := range cfg.cgMgrV1.Subsystems() {
logger.Debugf("Reading pids for subsystem %s", subsys.Name())
procs, err := cfg.cgMgrV1.Processes(subsys.Name(), false)
if err != nil {
@ -239,7 +240,7 @@ func initCgroup(cfg *cgroupConfig) (error) {
if err != nil {
return err
}
return fmt.Errorf("There are remaining processes in cgroup %s of subsystem %s", p, subsys.Name())
return fmt.Errorf("there are remaining processes in cgroup %s of subsystem %s", p, subsys.Name())
}
}
}
@ -334,7 +335,7 @@ func (c *cgroupHook) killAll() error {
taskList := []int{}
if c.cgCfg.isUnified {
procs, err := c.cgMgrV2.Procs(false)
if (err != nil) {
if err != nil {
return []int{}, err
}
for _, proc := range procs {
@ -342,16 +343,16 @@ func (c *cgroupHook) killAll() error {
}
} else {
taskSet := make(map[int]struct{})
for _, subsys := range(c.cgMgrV1.Subsystems()) {
for _, subsys := range c.cgMgrV1.Subsystems() {
procs, err := c.cgMgrV1.Processes(subsys.Name(), false)
if err != nil {
return []int{}, err
}
for _, proc := range(procs) {
for _, proc := range procs {
taskSet[proc.Pid] = struct{}{}
}
}
for proc := range(taskSet) {
for proc := range taskSet {
taskList = append(taskList, proc)
}
}
@ -360,7 +361,7 @@ func (c *cgroupHook) killAll() error {
for i := 0; i < 4; i++ {
if i == 3 {
return errors.New("Unable to kill all child tasks")
return errors.New("unable to kill all child tasks")
}
taskList, err := readTaskList()
if err != nil {

View File

@ -1,16 +1,17 @@
package worker
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
"errors"
"syscall"
cgv1 "github.com/containerd/cgroups"
cgv2 "github.com/containerd/cgroups/v2"
units "github.com/docker/go-units"
@ -228,7 +229,7 @@ sleep 30
So(err, ShouldBeNil)
So(strings.Trim(string(memoLimit), "\n"), ShouldEqual, strconv.Itoa(512*1024*1024))
} else {
for _, subsys := range(cg.cgMgrV1.Subsystems()) {
for _, subsys := range cg.cgMgrV1.Subsystems() {
if subsys.Name() == cgv1.Memory {
cgpath := filepath.Join(cgcf.Group, provider.Name())
if useCurrentCgroup {
@ -248,9 +249,9 @@ sleep 30
Reset(func() {
if cgcf.isUnified {
if cgcf.Group == "" {
wkrg, err := cgv2.NestedGroupPath("");
wkrg, err := cgv2.NestedGroupPath("")
So(err, ShouldBeNil)
wkrMgr, err := cgv2.LoadManager("/sys/fs/cgroup", wkrg);
wkrMgr, _ := cgv2.LoadManager("/sys/fs/cgroup", wkrg)
allCtrls, err := wkrMgr.Controllers()
So(err, ShouldBeNil)
err = wkrMgr.ToggleControllers(allCtrls, cgv2.Disable)
@ -263,7 +264,7 @@ sleep 30
if len(procs) == 0 {
break
}
for _, p := range(procs) {
for _, p := range procs {
if err := origMgr.AddProc(p); err != nil {
if errors.Is(err, syscall.ESRCH) {
logger.Debugf("Write pid %d to sub group failed: process vanished, ignoring")
@ -278,9 +279,9 @@ sleep 30
}
} else {
if cgcf.Group == "" {
pather := (func(p cgv1.Path) (cgv1.Path){
pather := (func(p cgv1.Path) cgv1.Path {
return func(subsys cgv1.Name) (string, error) {
path, err := p(subsys);
path, err := p(subsys)
if err != nil {
return "", err
}
@ -296,14 +297,14 @@ sleep 30
})
So(err, ShouldBeNil)
origMgr := cgcf.cgMgrV1
for _, subsys := range(wkrMgr.Subsystems()){
for _, subsys := range wkrMgr.Subsystems() {
for {
procs, err := wkrMgr.Processes(subsys.Name(), false)
So(err, ShouldBeNil)
if len(procs) == 0 {
break
}
for _, proc := range(procs) {
for _, proc := range procs {
if err := origMgr.Add(proc); err != nil {
if errors.Is(err, syscall.ESRCH) {
logger.Debugf("Write pid %d to sub group failed: process vanished, ignoring")

View File

@ -106,7 +106,7 @@ func (p *cmdProvider) Run(started chan empty) error {
}
if len(matches) != 0 {
logger.Debug("Fail-on-match: %r", matches)
return fmt.Errorf("Fail-on-match regexp found %d matches", len(matches))
return fmt.Errorf("fail-on-match regexp found %d matches", len(matches))
}
}
if p.sizePattern != nil {

View File

@ -6,10 +6,10 @@ import (
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/imdario/mergo"
units "github.com/docker/go-units"
cgv1 "github.com/containerd/cgroups"
cgv2 "github.com/containerd/cgroups/v2"
units "github.com/docker/go-units"
"github.com/imdario/mergo"
)
type providerEnum uint8
@ -30,7 +30,7 @@ func (p *providerEnum) UnmarshalText(text []byte) error {
case `two-stage-rsync`:
*p = provTwoStageRsync
default:
return errors.New("Invalid value to provierEnum")
return errors.New("invalid value to provierEnum")
}
return nil
}

View File

@ -10,12 +10,12 @@ import (
func TestConfigDiff(t *testing.T) {
Convey("When old and new configs are equal", t, func() {
oldList := []mirrorConfig{
mirrorConfig{Name: "debian"},
mirrorConfig{Name: "debian-security"},
mirrorConfig{Name: "fedora"},
mirrorConfig{Name: "archlinux"},
mirrorConfig{Name: "AOSP"},
mirrorConfig{Name: "ubuntu"},
{Name: "debian"},
{Name: "debian-security"},
{Name: "fedora"},
{Name: "archlinux"},
{Name: "AOSP"},
{Name: "ubuntu"},
}
newList := make([]mirrorConfig, len(oldList))
copy(newList, oldList)
@ -25,19 +25,19 @@ func TestConfigDiff(t *testing.T) {
})
Convey("When giving two config lists with different names", t, func() {
oldList := []mirrorConfig{
mirrorConfig{Name: "debian"},
mirrorConfig{Name: "debian-security"},
mirrorConfig{Name: "fedora"},
mirrorConfig{Name: "archlinux"},
mirrorConfig{Name: "AOSP", Env: map[string]string{"REPO": "/usr/bin/repo"}},
mirrorConfig{Name: "ubuntu"},
{Name: "debian"},
{Name: "debian-security"},
{Name: "fedora"},
{Name: "archlinux"},
{Name: "AOSP", Env: map[string]string{"REPO": "/usr/bin/repo"}},
{Name: "ubuntu"},
}
newList := []mirrorConfig{
mirrorConfig{Name: "debian"},
mirrorConfig{Name: "debian-cd"},
mirrorConfig{Name: "archlinuxcn"},
mirrorConfig{Name: "AOSP", Env: map[string]string{"REPO": "/usr/local/bin/aosp-repo"}},
mirrorConfig{Name: "ubuntu-ports"},
{Name: "debian"},
{Name: "debian-cd"},
{Name: "archlinuxcn"},
{Name: "AOSP", Env: map[string]string{"REPO": "/usr/local/bin/aosp-repo"}},
{Name: "ubuntu-ports"},
}
difference := diffMirrorConfig(oldList, newList)

View File

@ -34,7 +34,7 @@ func (ctx *Context) Enter() *Context {
// Exit return the upper layer of context
func (ctx *Context) Exit() (*Context, error) {
if ctx.parent == nil {
return nil, errors.New("Cannot exit the bottom layer context")
return nil, errors.New("cannot exit the bottom layer context")
}
return ctx.parent, nil
}

View File

@ -49,7 +49,7 @@ func (d *dockerHook) preExec() error {
if _, err := os.Stat(workingDir); os.IsNotExist(err) {
logger.Debugf("Making dir %s", workingDir)
if err = os.MkdirAll(workingDir, 0755); err != nil {
return fmt.Errorf("Error making dir %s: %s", workingDir, err.Error())
return fmt.Errorf("error making dir %s: %s", workingDir, err.Error())
}
}

View File

@ -32,7 +32,7 @@ func newExecPostHook(provider mirrorProvider, execOn uint8, command string) (*ex
return nil, err
}
if execOn != execOnSuccess && execOn != execOnFailure {
return nil, fmt.Errorf("Invalid option for exec-on: %d", execOn)
return nil, fmt.Errorf("invalid option for exec-on: %d", execOn)
}
return &execPostHook{
@ -92,7 +92,7 @@ func (h *execPostHook) Do() error {
args = append(args, arg)
}
} else {
return errors.New("Invalid Command")
return errors.New("invalid command")
}
return session.Command(cmd, args...).Run()
}

View File

@ -79,7 +79,7 @@ func (m *mirrorJob) SetState(state uint32) {
func (m *mirrorJob) SetProvider(provider mirrorProvider) error {
s := m.State()
if (s != stateNone) && (s != stateDisabled) {
return fmt.Errorf("Provider cannot be switched when job state is %d", s)
return fmt.Errorf("provider cannot be switched when job state is %d", s)
}
m.provider = provider
return nil

View File

@ -188,7 +188,7 @@ func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider {
p.isMaster = isMaster
provider = p
default:
panic(errors.New("Invalid mirror provider"))
panic(errors.New("invalid mirror provider"))
}
// Add Logging Hook

View File

@ -11,22 +11,21 @@ import (
"time"
"github.com/codeskyblue/go-sh"
"golang.org/x/sys/unix"
"github.com/moby/moby/pkg/reexec"
cgv1 "github.com/containerd/cgroups"
"github.com/moby/moby/pkg/reexec"
"golang.org/x/sys/unix"
)
// runner is to run os commands giving command line, env and log file
// it's an alternative to python-sh or go-sh
var errProcessNotStarted = errors.New("Process Not Started")
var errProcessNotStarted = errors.New("process not started")
type cmdJob struct {
sync.Mutex
cmd *exec.Cmd
workingDir string
env map[string]string
logFile *os.File
finished chan empty
provider mirrorProvider
retErr error
@ -115,7 +114,7 @@ func (c *cmdJob) Start() error {
if cg != nil {
logger.Debugf("Preparing cgroup sync pipes for job %s", c.provider.Name())
var err error
pipeR, pipeW, err = os.Pipe();
pipeR, pipeW, err = os.Pipe()
if err != nil {
return err
}

View File

@ -112,7 +112,7 @@ func (p *twoStageRsyncProvider) Options(stage int) ([]string, error) {
options = append(options, p.stage1Options...)
stage1Profile, ok := rsyncStage1Profiles[p.stage1Profile]
if !ok {
return nil, errors.New("Invalid Stage 1 Profile")
return nil, errors.New("invalid stage 1 profile")
}
for _, exc := range stage1Profile {
options = append(options, exc)
@ -124,7 +124,7 @@ func (p *twoStageRsyncProvider) Options(stage int) ([]string, error) {
options = append(options, p.extraOptions...)
}
} else {
return []string{}, fmt.Errorf("Invalid stage: %d", stage)
return []string{}, fmt.Errorf("invalid stage: %d", stage)
}
if !p.rsyncNeverTimeout {

View File

@ -147,7 +147,7 @@ func TestWorker(t *testing.T) {
})
Convey("with one job", func(ctx C) {
workerCfg.Mirrors = []mirrorConfig{
mirrorConfig{
{
Name: "job-ls",
Provider: provCommand,
Command: "ls",
@ -194,17 +194,17 @@ func TestWorker(t *testing.T) {
})
Convey("with several jobs", func(ctx C) {
workerCfg.Mirrors = []mirrorConfig{
mirrorConfig{
{
Name: "job-ls-1",
Provider: provCommand,
Command: "ls",
},
mirrorConfig{
{
Name: "job-fail",
Provider: provCommand,
Command: "non-existent-command-xxxx",
},
mirrorConfig{
{
Name: "job-ls-2",
Provider: provCommand,
Command: "ls",

View File

@ -13,7 +13,7 @@ import (
func TestZFSHook(t *testing.T) {
Convey("ZFS Hook should work", t, func(ctx C) {
tmpDir, err := ioutil.TempDir("", "tunasync")
tmpDir, _ := ioutil.TempDir("", "tunasync")
tmpFile := filepath.Join(tmpDir, "log_file")
c := cmdConfig{