Skip to content

Commit

Permalink
Merge pull request #59 from luthermonson/goimports
Browse files Browse the repository at this point in the history
goimport fixups and removing superfluous wrapf calls
  • Loading branch information
luthermonson authored Oct 20, 2021
2 parents 92ed3ec + 4869627 commit d448a71
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 90 deletions.
10 changes: 5 additions & 5 deletions cmd/client/process/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func _runRequestParser(cliCtx *cli.Context) (err error) {
// validate
path := cliCtx.String("path")
if path == "" {
return errors.Errorf("--path is required")
return errors.New("--path is required")
}
path, err = paths.GetBinaryPath(path)
if err != nil {
return errors.Wrapf(err, "--path is invalid")
return errors.Wrap(err, "--path is invalid")
}
checksum, err := paths.GetFileSHA1Hash(path)
if err != nil {
Expand All @@ -69,7 +69,7 @@ func _runRequestParser(cliCtx *cli.Context) (err error) {
if exposesList := flags.GetListValue(cliCtx, "exposes"); !exposesList.IsEmpty() {
exposesListValue, err := exposesList.Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --exposes")
return errors.Wrap(err, "failed to parse --exposes")
}
exposes, err = parseExposes(exposesListValue)
if err != nil {
Expand All @@ -78,11 +78,11 @@ func _runRequestParser(cliCtx *cli.Context) (err error) {
}
args, err := flags.GetListValue(cliCtx, "args").Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --args")
return errors.Wrap(err, "failed to parse --args")
}
envs, err := flags.GetListValue(cliCtx, "envs").Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --envs")
return errors.Wrap(err, "failed to parse --envs")
}
dir := cliCtx.String("dir")
if dir == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/proxy/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func _proxyAction(cliCtx *cli.Context) (err error) {
// Get hostname to identify backend connection
hostname, err := os.Hostname()
if err != nil {
return errors.Wrapf(err, "unable to get hostname")
return errors.Wrap(err, "unable to get hostname")
}
proxyHeaders := http.Header{}
proxyHeaders.Set(proxy.ClientIDHeader, hostname)
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/route/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func _addRequestParser(cliCtx *cli.Context) error {
_addRequest = &types.RouteAddRequest{}
_addRequest.Addresses, err = addressList.Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --addresses")
return errors.Wrap(err, "failed to parse --addresses")
}
for idx, address := range _addRequest.Addresses {
if !strings.Contains(address, "/") {
Expand Down
2 changes: 1 addition & 1 deletion cmd/outputs/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func JSON(w io.Writer, obj interface{}) error {
func fprint(w io.Writer, obj interface{}) (err error) {
_, err = fmt.Fprint(w, obj)
if err != nil {
err = errors.Wrapf(err, "failed to output result")
err = errors.Wrap(err, "failed to output result")
}
return
}
8 changes: 4 additions & 4 deletions cmd/server/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func _runAction(cliCtx *cli.Context) error {

err = setupUpgrading(ctx, cfg)
if err != nil {
return errors.Wrapf(err, "failed to setup upgrading")
return errors.Wrap(err, "failed to setup upgrading")
}

serverOptions := []grpc.ServerOption{
Expand All @@ -97,19 +97,19 @@ func _runAction(cliCtx *cli.Context) error {

serverOptions, err = setupGRPCServerOptions(serverOptions, cfg)
if err != nil {
return errors.Wrapf(err, "failed to setup grpc middlewares")
return errors.Wrap(err, "failed to setup grpc middlewares")
}

logrus.Debugf("Proxy port whitelist: %v", cfg.WhiteList.ProxyPorts)
server, err := apis.NewServer(cfg.Listen, serverOptions, cfg.Proxy, cfg.WhiteList.ProxyPorts)
if err != nil {
return errors.Wrapf(err, "failed to create server")
return errors.Wrap(err, "failed to create server")
}
agent := systemagent.New(cfg.SystemAgent)

err = runService(ctx, server, agent)
if err != nil {
return errors.Wrapf(err, "failed to run server")
return errors.Wrap(err, "failed to run server")
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/app/run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func registerService() error {
// confirm wins binary path
binaryPath, err := paths.GetBinaryPath(os.Args[0])
if err != nil {
return errors.Wrapf(err, "could not get binary")
return errors.Wrap(err, "could not get binary")
}

// open SCM
Expand Down Expand Up @@ -160,7 +160,7 @@ func unregisterService() error {

err = w.Delete()
if err != nil {
return errors.Wrapf(err, "could not delete")
return errors.Wrap(err, "could not delete")
}

return nil
Expand All @@ -186,13 +186,13 @@ func runService(ctx context.Context, server *apis.Server, agent *systemagent.Age
// ETW tracing
etw, err := logs.NewEtwProviderHook(defaults.WindowsServiceName)
if err != nil {
return errors.Wrapf(err, "could not create ETW provider logrus hook")
return errors.Wrap(err, "could not create ETW provider logrus hook")
}
logrus.AddHook(etw)

el, err := logs.NewEventLogHook(defaults.WindowsServiceName)
if err != nil {
return errors.Wrapf(err, "could not create eventlog logrus hook")
return errors.Wrap(err, "could not create eventlog logrus hook")
}
logrus.AddHook(el)

Expand Down
16 changes: 8 additions & 8 deletions pkg/apis/process_service_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *process) kill(ctx context.Context) error {
// remove firewall route
_, err := powershell.RunCommandf(`Get-NetFirewallRule -PolicyStore ActiveStore -Name %s-* | ForEach-Object {Remove-NetFirewallRule -Name $_.Name -PolicyStore ActiveStore -ErrorAction Ignore | Out-Null}`, p.name)
if err != nil {
return errors.Wrapf(err, "could not remove firewall rules")
return errors.Wrap(err, "could not remove firewall rules")
}

// kill task
Expand Down Expand Up @@ -85,15 +85,15 @@ func (s *processService) getFromHost(pname string) (*process, error) {
// take windows processes snapshot
snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, errors.Wrapf(err, "could not take a snapshot for Windows processes")
return nil, errors.Wrap(err, "could not take a snapshot for Windows processes")
}
defer syscall.CloseHandle(snapshot)

// start the iterator
var procEntry syscall.ProcessEntry32
procEntry.Size = uint32(unsafe.Sizeof(procEntry))
if err := syscall.Process32First(snapshot, &procEntry); err != nil {
return nil, errors.Wrapf(err, "could not start the iterator for Windows processes")
return nil, errors.Wrap(err, "could not start the iterator for Windows processes")
}

// process iterating
Expand Down Expand Up @@ -134,21 +134,21 @@ func (s *processService) create(ctx context.Context, path string, dir string, ar
if pInHost != nil {
// detect the process is running via wins
if pInPool, _ := s.getFromPool(pname); pInPool != nil {
return nil, errors.Wrapf(err, "could not run duplicate process")
return nil, errors.Wrap(err, "could not run duplicate process")
}

// recreate the process to gain the std handler
logrus.Warnf("[Process] Found stale process %s, try to recreate a new process", pInHost)
if err := pInHost.kill(ctx); err != nil {
return nil, errors.Wrapf(err, "could not kill stale process")
return nil, errors.Wrap(err, "could not kill stale process")
}
}

// create firewall rules if needed
if fwrules != "" {
_, err = powershell.RunCommandf(`"%s" -split ' ' | ForEach-Object {$ruleMd = $_ -split '-'; $ruleName = "%s-$_"; New-NetFirewallRule -Name $ruleName -DisplayName $ruleName -Action Allow -Protocol $ruleMd[0] -LocalPort $ruleMd[1] -Enabled True -PolicyStore ActiveStore -ErrorAction Ignore | Out-Null}`, fwrules, pname)
if err != nil {
return nil, errors.Wrapf(err, "could not create process firewall rules")
return nil, errors.Wrap(err, "could not create process firewall rules")
}
}

Expand All @@ -162,11 +162,11 @@ func (s *processService) create(ctx context.Context, path string, dir string, ar
}
stdout, err := c.StdoutPipe()
if err != nil {
return nil, errors.Wrapf(err, "could not create process stdout stream")
return nil, errors.Wrap(err, "could not create process stdout stream")
}
stderr, err := c.StderrPipe()
if err != nil {
return nil, errors.Wrapf(err, "could not create process stderr stream")
return nil, errors.Wrap(err, "could not create process stderr stream")
}

if err := c.Start(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/route_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *routeService) Add(ctx context.Context, req *types.RouteAddRequest) (res
return &types.Void{}, nil
}

return nil, status.Errorf(codes.Internal, "there isn't a default gateway with a destination of 0.0.0.0")
return nil, status.Error(codes.Internal, "there isn't a default gateway with a destination of 0.0.0.0")
}

func ipnetToString(ipNet *net.IPNet) (addr string, mask string) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func GenerateSignedCert(commonName string, extKeyUsages []x509.ExtKeyUsage, key
func GenerateSelfSignedCACertAndKey(commonName string) (*x509.Certificate, *rsa.PrivateKey, error) {
key, err := GeneratePrivateKey()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to generate private key")
return nil, nil, errors.Wrap(err, "failed to generate private key")
}

cert, err := GenerateSelfSignedCACert(commonName, key)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to generate cert")
return nil, nil, errors.Wrap(err, "failed to generate cert")
}

return cert, key, nil
Expand All @@ -85,12 +85,12 @@ func GenerateSelfSignedCACertAndKey(commonName string) (*x509.Certificate, *rsa.
func GenerateCertAndKey(commonName string, extKeyUsages []x509.ExtKeyUsage, caCert *x509.Certificate, caKey *rsa.PrivateKey) (*x509.Certificate, *rsa.PrivateKey, error) {
key, err := GeneratePrivateKey()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to generate private key")
return nil, nil, errors.Wrap(err, "failed to generate private key")
}

cert, err := GenerateSignedCert(commonName, extKeyUsages, key, caCert, caKey)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to generate cert")
return nil, nil, errors.Wrap(err, "failed to generate cert")
}

return cert, key, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/converters/error_ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strconv"

"github.com/buger/jsonparser"

"golang.org/x/sys/windows/registry"
)

Expand Down
17 changes: 4 additions & 13 deletions pkg/defaults/constant.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package defaults

// permission
const (
PermissionBuiltinAdministratorsAndLocalSystem = "D:P(A;;GA;;;BA)(A;;GA;;;SY)"
)

// windows service
const (
WindowsServiceName = "rancher-wins"
WindowsServiceDisplayName = "Rancher Wins"
)

// windows npipe name
const (
NamedPipeName = "rancher_wins"
ProxyPipeName = "rancher_wins_proxy"
WindowsServiceName = "rancher-wins"
WindowsServiceDisplayName = "Rancher Wins"
NamedPipeName = "rancher_wins"
ProxyPipeName = "rancher_wins_proxy"
)
15 changes: 3 additions & 12 deletions pkg/defaults/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ import (
"path/filepath"
)

// application
var (
AppVersion = "dev"
AppCommit = "0000000"
)

// configuration
var (
ConfigPath = filepath.Join("c:/", "etc", "rancher", "wins", "config")
)

// upgrading
var (
AppVersion = "dev"
AppCommit = "0000000"
ConfigPath = filepath.Join("c:/", "etc", "rancher", "wins", "config")
UpgradeWatchingPath = filepath.Join("c:/", "etc", "rancher", "wins", "wins.exe")
)
2 changes: 1 addition & 1 deletion pkg/logs/etw.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func NewEventLogHook(serviceName string) (logrus.Hook, error) {
return &eventLogHook{log: elog}, nil
}

func etwCallback(sourceID guid.GUID, state etw.ProviderState, level etw.Level, matchAnyKeyword uint64, matchAllKeyword uint64, filterData uintptr) {
func etwCallback(_ guid.GUID, state etw.ProviderState, _ etw.Level, _ uint64, _ uint64, _ uintptr) {
if state == etw.ProviderStateCaptureState {
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", profilings.DumpStacks())
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/paths/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func MoveFile(srcPath, targetPath string) error {
d, err := os.Stat(dir)
if err != nil {
if !os.IsNotExist(err) {
return errors.Wrapf(err, "could not detect the directory of target file")
return errors.Wrap(err, "could not detect the directory of target file")
}

err = os.Mkdir(dir, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "could not create the directory of target file")
return errors.Wrap(err, "could not create the directory of target file")
}
} else if !d.IsDir() {
return errors.Errorf("%s is not a directory", dir)
Expand All @@ -59,13 +59,13 @@ func MoveFile(srcPath, targetPath string) error {
tempTargetPath := filepath.Join(os.TempDir(), filepath.Base(targetPath))
err = os.Rename(targetPath, tempTargetPath)
if err != nil {
return errors.Wrapf(err, "could not backup the existing target file")
return errors.Wrap(err, "could not backup the existing target file")
}
}

err = os.Rename(srcPath, targetPath)
if err != nil {
return errors.Wrapf(err, "could not move the source file to the target")
return errors.Wrap(err, "could not move the source file to the target")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/paths/file_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetBinarySHA1Hash(binaryName string) (string, error) {
func EnsureBinary(binaryName string, expectedChecksum string) error {
actualChecksum, err := GetBinarySHA1Hash(binaryName)
if err != nil {
return errors.Wrapf(err, "could not get checksum")
return errors.Wrap(err, "could not get checksum")
}
if expectedChecksum != actualChecksum {
return errors.Errorf("could not match (expect checksum %q, but get %q)", expectedChecksum, actualChecksum)
Expand Down
28 changes: 0 additions & 28 deletions pkg/syscalls/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,11 @@ var (
procCreateIPForwardEntry = modiphlpapi.NewProc("CreateIPForwardEntry")
)

// https://docs.microsoft.com/en-us/windows/win32/api/ipmib/ns-ipmib-mib_ipforwardtable
// typedef struct _MIB_IPFORWARDTABLE {
// DWORD dwNumEntries;
// MIB_IPFORWARDROW table[ANY_SIZE];
// }
type IPForwardTable struct {
NumEntries uint32
Table [1]IPForwardRow
}

// https://docs.microsoft.com/en-us/windows/win32/api/ipmib/ns-ipmib-mib_ipforwardrow
// typedef struct _MIB_IPFORWARDROW {
// DWORD dwForwardDest;
// DWORD dwForwardMask;
// DWORD dwForwardPolicy;
// DWORD dwForwardNextHop;
// IF_INDEX dwForwardIfIndex;
// union {
// DWORD dwForwardType;
// MIB_IPFORWARD_TYPE ForwardType;
// };
// union {
// DWORD dwForwardProto;
// MIB_IPFORWARD_PROTO ForwardProto;
// };
// DWORD dwForwardAge;
// DWORD dwForwardNextHopAS;
// DWORD dwForwardMetric1;
// DWORD dwForwardMetric2;
// DWORD dwForwardMetric3;
// DWORD dwForwardMetric4;
// DWORD dwForwardMetric5;
// }
type IPForwardRow struct {
ForwardDest uint32
ForwardMask uint32
Expand Down
1 change: 0 additions & 1 deletion pkg/systemagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/pkg/errors"

"github.com/rancher/system-agent/pkg/applyinator"
"github.com/rancher/system-agent/pkg/config"
"github.com/rancher/system-agent/pkg/image"
Expand Down

0 comments on commit d448a71

Please sign in to comment.