Skip to content

Commit

Permalink
chore: replace fmt.Errorf with errors.New where possible
Browse files Browse the repository at this point in the history
This time use `eg` from `x/tools` repo tool to do this.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Feb 14, 2024
1 parent d4521ee commit fa3b933
Show file tree
Hide file tree
Showing 188 changed files with 508 additions and 437 deletions.
3 changes: 2 additions & 1 deletion cmd/installer/pkg/install/install.go
Expand Up @@ -6,6 +6,7 @@ package install

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -321,7 +322,7 @@ func (i *Installer) Install(ctx context.Context, mode Mode) (err error) {
}

if metaPartitionName == "" {
return fmt.Errorf("failed to detect META partition")
return errors.New("failed to detect META partition")
}

if metaState, err = meta.New(context.Background(), nil, meta.WithPrinter(i.options.Printf), meta.WithFixedPath(metaPartitionName)); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/installer/pkg/install/manifest.go
Expand Up @@ -60,7 +60,7 @@ func NewManifest(mode Mode, uefiOnlyBoot bool, bootLoaderPresent bool, opts *Opt

if opts.Board != constants.BoardNone {
if uefiOnlyBoot {
return nil, fmt.Errorf("board option can't be used with uefi-only-boot")
return nil, errors.New("board option can't be used with uefi-only-boot")
}

var b runtime.Board
Expand All @@ -79,11 +79,11 @@ func NewManifest(mode Mode, uefiOnlyBoot bool, bootLoaderPresent bool, opts *Opt
}

if !opts.Force && opts.Zero {
return nil, fmt.Errorf("zero option can't be used without force")
return nil, errors.New("zero option can't be used without force")
}

if !opts.Force && !bootLoaderPresent {
return nil, fmt.Errorf("install with preserve is not supported if existing boot partition was not found")
return nil, errors.New("install with preserve is not supported if existing boot partition was not found")
}

// Verify that the target device(s) can satisfy the requested options.
Expand Down
18 changes: 9 additions & 9 deletions cmd/talosctl/cmd/mgmt/cluster/create.go
Expand Up @@ -262,7 +262,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
}

if controlplanes < 1 {
return fmt.Errorf("number of controlplanes can't be less than 1")
return errors.New("number of controlplanes can't be less than 1")
}

controlPlaneNanoCPUs, err := parseCPUShare(controlPlaneCpus)
Expand All @@ -287,7 +287,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
}

if !cidr4.Addr().Is4() {
return fmt.Errorf("--cidr is expected to be IPV4 CIDR")
return errors.New("--cidr is expected to be IPV4 CIDR")
}

// use ULA IPv6 network fd00::/8, add 'TAL' in hex to build /32 network, add IPv4 CIDR to build /64 unique network
Expand All @@ -312,7 +312,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
}

if len(cidrs) == 0 {
return fmt.Errorf("neither IPv4 nor IPv6 network was enabled")
return errors.New("neither IPv4 nor IPv6 network was enabled")
}

// Gateway addr at 1st IP in range, ex. 192.168.0.1
Expand Down Expand Up @@ -362,7 +362,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
// Validate network chaos flags
if !networkChaos {
if jitter != 0 || latency != 0 || packetLoss != 0 || packetReorder != 0 || packetCorrupt != 0 || bandwidth != 0 {
return fmt.Errorf("network chaos flags can only be used with --with-network-chaos")
return errors.New("network chaos flags can only be used with --with-network-chaos")
}
}

Expand Down Expand Up @@ -426,7 +426,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {

if ports != "" {
if provisionerName != "docker" {
return fmt.Errorf("exposed-ports flag only supported with docker provisioner")
return errors.New("exposed-ports flag only supported with docker provisioner")
}

portList := strings.Split(ports, ",")
Expand Down Expand Up @@ -553,7 +553,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
}

if len(keys) == 0 {
return fmt.Errorf("no disk encryption key types enabled")
return errors.New("no disk encryption key types enabled")
}

if encryptStatePartition {
Expand Down Expand Up @@ -993,7 +993,7 @@ func getDisks() ([]*provision.Disk, error) {
)

if len(partitions)%2 != 0 {
return nil, fmt.Errorf("failed to parse malformed partition definitions")
return nil, errors.New("failed to parse malformed partition definitions")
}

partitionIndex := 0
Expand All @@ -1002,7 +1002,7 @@ func getDisks() ([]*provision.Disk, error) {
partitionPath := partitions[j]

if !strings.HasPrefix(partitionPath, "/var") {
return nil, fmt.Errorf("user disk partitions can only be mounted into /var folder")
return nil, errors.New("user disk partitions can only be mounted into /var folder")
}

value, e := strconv.ParseInt(partitions[j+1], 10, 0)
Expand All @@ -1012,7 +1012,7 @@ func getDisks() ([]*provision.Disk, error) {
partitionSize, e = humanize.ParseBytes(partitions[j+1])

if e != nil {
return nil, fmt.Errorf("failed to parse partition size")
return nil, errors.New("failed to parse partition size")
}
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/talosctl/cmd/mgmt/gen/config.go
Expand Up @@ -5,6 +5,7 @@
package gen

import (
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -262,15 +263,15 @@ func writeConfig(args []string) error {

func validateFlags() error {
if len(genConfigCmdFlags.outputTypes) == 0 {
return fmt.Errorf("at least one output type must be specified")
return errors.New("at least one output type must be specified")
}

if len(genConfigCmdFlags.outputTypes) > 1 && genConfigCmdFlags.output == stdoutOutput {
return fmt.Errorf("can't use multiple output types with stdout")
return errors.New("can't use multiple output types with stdout")
}

if genConfigCmdFlags.outputDir != "" && genConfigCmdFlags.output != "" {
return fmt.Errorf("can't use both output-dir and output")
return errors.New("can't use both output-dir and output")
}

if genConfigCmdFlags.outputDir != "" {
Expand Down
7 changes: 4 additions & 3 deletions cmd/talosctl/cmd/mgmt/gen/crt.go
Expand Up @@ -7,6 +7,7 @@ package gen
import (
stdlibx509 "crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -38,7 +39,7 @@ var genCrtCmd = &cobra.Command{

caPemBlock, _ := pem.Decode(caBytes)
if caPemBlock == nil {
return fmt.Errorf("error decoding cert PEM")
return errors.New("error decoding cert PEM")
}

caCrt, err := stdlibx509.ParseCertificate(caPemBlock.Bytes)
Expand All @@ -53,7 +54,7 @@ var genCrtCmd = &cobra.Command{

keyPemBlock, _ := pem.Decode(keyBytes)
if keyPemBlock == nil {
return fmt.Errorf("error decoding key PEM")
return errors.New("error decoding key PEM")
}

caKey, err := stdlibx509.ParsePKCS8PrivateKey(keyPemBlock.Bytes)
Expand All @@ -68,7 +69,7 @@ var genCrtCmd = &cobra.Command{

csrPemBlock, _ := pem.Decode(csrBytes)
if csrPemBlock == nil {
return fmt.Errorf("error parsing CSR PEM")
return errors.New("error parsing CSR PEM")
}

ccsr, err := stdlibx509.ParseCertificateRequest(csrPemBlock.Bytes)
Expand Down
3 changes: 2 additions & 1 deletion cmd/talosctl/cmd/mgmt/gen/csr.go
Expand Up @@ -7,6 +7,7 @@ package gen
import (
stdlibx509 "crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -40,7 +41,7 @@ var genCSRCmd = &cobra.Command{

pemBlock, _ := pem.Decode(keyBytes)
if pemBlock == nil {
return fmt.Errorf("error decoding PEM")
return errors.New("error decoding PEM")
}

keyEC, err := stdlibx509.ParsePKCS8PrivateKey(pemBlock.Bytes)
Expand Down
5 changes: 3 additions & 2 deletions cmd/talosctl/cmd/mgmt/gen/secureboot.go
Expand Up @@ -7,6 +7,7 @@ package gen
import (
"context"
"encoding/pem"
"errors"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -191,11 +192,11 @@ func init() {
func convertPEMToDER(data []byte) ([]byte, error) {
block, rest := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM data")
return nil, errors.New("failed to decode PEM data")
}

if len(rest) > 0 {
return nil, fmt.Errorf("more than one PEM block found in PEM data")
return nil, errors.New("more than one PEM block found in PEM data")
}

return block.Bytes, nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/talosctl/cmd/mgmt/kms_launch.go
Expand Up @@ -7,7 +7,6 @@ package mgmt
import (
"context"
"errors"
"fmt"
"log"
"net"

Expand All @@ -34,7 +33,7 @@ var kmsLaunchCmd = &cobra.Command{
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
if kmsLaunchCmdFlags.key == nil {
return fmt.Errorf("no key provided to the KMS server")
return errors.New("no key provided to the KMS server")
}

srv := server.NewServer(func(_ context.Context, nodeUUID string) ([]byte, error) {
Expand Down
7 changes: 4 additions & 3 deletions cmd/talosctl/cmd/talos/apply-config.go
Expand Up @@ -6,6 +6,7 @@ package talos

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -53,7 +54,7 @@ var applyConfigCmd = &cobra.Command{
} else if cmd.CalledAs() == "apply-config" {
cmd.Help() //nolint:errcheck

return fmt.Errorf("expected no positional arguments")
return errors.New("expected no positional arguments")
}
}

Expand All @@ -64,7 +65,7 @@ var applyConfigCmd = &cobra.Command{
}

if len(cfgBytes) < 1 {
return fmt.Errorf("no configuration data read")
return errors.New("no configuration data read")
}

if len(applyConfigCmdFlags.patches) != 0 {
Expand All @@ -89,7 +90,7 @@ var applyConfigCmd = &cobra.Command{
}
}
} else if applyConfigCmdFlags.Mode.Mode != helpers.InteractiveMode {
return fmt.Errorf("no filename supplied for configuration")
return errors.New("no filename supplied for configuration")
}

withClient := func(f func(context.Context, *client.Client) error) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/talosctl/cmd/talos/bootstrap.go
Expand Up @@ -6,6 +6,7 @@ package talos

import (
"context"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -38,7 +39,7 @@ Talos etcd cluster can be recovered from a known snapshot with '--recover-from='
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
if len(GlobalArgs.Nodes) > 1 {
return fmt.Errorf("command \"bootstrap\" is not supported with multiple nodes")
return errors.New("command \"bootstrap\" is not supported with multiple nodes")
}

if bootstrapCmdFlags.recoverFrom != "" {
Expand Down
8 changes: 4 additions & 4 deletions cmd/talosctl/cmd/talos/config.go
Expand Up @@ -53,7 +53,7 @@ func openConfigAndContext(context string) (*clientconfig.Config, error) {
}

if context == "" {
return nil, fmt.Errorf("no context is set")
return nil, errors.New("no context is set")
}

if _, ok := c.Contexts[context]; !ok {
Expand Down Expand Up @@ -215,7 +215,7 @@ var configRemoveCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
pattern := args[0]
if pattern == "" {
return fmt.Errorf("no context specified")
return errors.New("no context specified")
}

c, err := clientconfig.Open(GlobalArgs.Talosconfig)
Expand Down Expand Up @@ -293,7 +293,7 @@ func checkAndSetCrtAndKey(configContext *clientconfig.Context) error {
}

if crt == "" || key == "" {
return fmt.Errorf("if either the 'crt' or 'key' flag is specified, both are required")
return errors.New("if either the 'crt' or 'key' flag is specified, both are required")
}

crtBytes, err := os.ReadFile(crt)
Expand Down Expand Up @@ -491,7 +491,7 @@ func configInfo(config *clientconfig.Config, now time.Time) (talosconfigInfo, er

block, _ := pem.Decode(b)
if block == nil {
return talosconfigInfo{}, fmt.Errorf("error decoding PEM")
return talosconfigInfo{}, errors.New("error decoding PEM")
}

var crt *x509.Certificate
Expand Down
4 changes: 2 additions & 2 deletions cmd/talosctl/cmd/talos/crashdump.go
Expand Up @@ -6,7 +6,7 @@ package talos

import (
"context"
"fmt"
"errors"

"github.com/spf13/cobra"

Expand All @@ -26,7 +26,7 @@ var crashdumpCmd = &cobra.Command{
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
return fmt.Errorf("`talosctl crashdump` is deprecated, please use `talosctl support` instead")
return errors.New("`talosctl crashdump` is deprecated, please use `talosctl support` instead")
})
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/talosctl/cmd/talos/diskusage.go
Expand Up @@ -6,6 +6,7 @@ package talos

import (
"context"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -81,7 +82,7 @@ var duCmd = &cobra.Command{

return helpers.ReadGRPCStream(stream, func(info *machineapi.DiskUsageInfo, node string, multipleNodes bool) error {
if info.Error != "" {
return helpers.NonFatalError(fmt.Errorf(info.Error))
return helpers.NonFatalError(errors.New(info.Error))
}

pattern := "%s\t%s\n"
Expand Down
2 changes: 1 addition & 1 deletion cmd/talosctl/cmd/talos/edit.go
Expand Up @@ -55,7 +55,7 @@ func editFn(c *client.Client) func(context.Context, string, resource.Resource, e
}

if mc.Metadata().Type() != config.MachineConfigType {
return fmt.Errorf("only the machineconfig resource can be edited")
return errors.New("only the machineconfig resource can be edited")
}

metadata := mc.Metadata()
Expand Down
4 changes: 2 additions & 2 deletions cmd/talosctl/cmd/talos/interfaces.go
Expand Up @@ -6,7 +6,7 @@ package talos

import (
"context"
"fmt"
"errors"

"github.com/spf13/cobra"

Expand All @@ -22,7 +22,7 @@ var interfacesCmd = &cobra.Command{
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
return fmt.Errorf("`talosctl interfaces` is deprecated, please use `talosctl get addresses` and `talosctl get links` instead")
return errors.New("`talosctl interfaces` is deprecated, please use `talosctl get addresses` and `talosctl get links` instead")
})
},
}
Expand Down

0 comments on commit fa3b933

Please sign in to comment.