Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang 1.17 compat #337

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/nvidia-ctk/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package config

import (
"errors"
"fmt"
"reflect"
"strconv"
Expand All @@ -28,6 +27,7 @@ import (
createdefault "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/create-default"
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/flags"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/errors"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
)

Expand Down Expand Up @@ -141,7 +141,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) {

field, err := getField(key)
if err != nil {
return key, nil, fmt.Errorf("%w: %w", errInvalidConfigOption, err)
return key, nil, errors.Join(err, errInvalidConfigOption)
}

kind := field.Kind()
Expand All @@ -157,7 +157,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) {
case reflect.Bool:
b, err := strconv.ParseBool(value)
if err != nil {
return key, value, fmt.Errorf("%w: %w", errInvalidFormat, err)
return key, value, errors.Join(err, errInvalidFormat)
}
return key, b, err
case reflect.String:
Expand All @@ -172,7 +172,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) {
for _, v := range valueParts {
vi, err := strconv.ParseInt(v, 10, 0)
if err != nil {
return key, nil, fmt.Errorf("%w: %w", errInvalidFormat, err)
return key, nil, errors.Join(err, errInvalidFormat)
}
output = append(output, vi)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
# Copyright 2023 NVIDIA CORPORATION
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

24

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package errors

import (
"errors"
)

// New provides a wrapper for errors.New.
// This allows this internal package to be used as a drop-in replacement for the stdlib package.
func New(text string) error {
return errors.New(text)
}
36 changes: 36 additions & 0 deletions internal/errors/errors_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build !go1.20

/**
# Copyright 2023 NVIDIA CORPORATION
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

24

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package errors

import "fmt"

// Join provides a legacy implementation for errors.Join.
func Join(errs ...error) error {
var all error
for _, err := range errs {
if err == nil {
continue
}
if all == nil {
all = err
}
all = fmt.Errorf("%v %w", all, err)
}
return all
}
28 changes: 28 additions & 0 deletions internal/errors/errors_modern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build go1.20

/**
# Copyright 2023 NVIDIA CORPORATION
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

24

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package errors

import (
"errors"
)

// Join wraps errors.Join for newer golang versions.
func Join(errs ...error) error {
return errors.Join(errs...)
}
8 changes: 4 additions & 4 deletions internal/lookup/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package lookup

import "errors"
import "github.com/NVIDIA/nvidia-container-toolkit/internal/errors"

type first []Locator

Expand All @@ -34,20 +34,20 @@ func First(locators ...Locator) Locator {

// Locate returns the results for the first locator that returns a non-empty non-error result.
func (f first) Locate(pattern string) ([]string, error) {
var allErrors []error
var allErrors error
for _, l := range f {
if l == nil {
continue
}
candidates, err := l.Locate(pattern)
if err != nil {
allErrors = append(allErrors, err)
allErrors = errors.Join(allErrors, err)
continue
}
if len(candidates) > 0 {
return candidates, nil
}
}

return nil, errors.Join(allErrors...)
return nil, allErrors
}
2 changes: 1 addition & 1 deletion internal/modifier/cdi/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package cdi

import (
"errors"
"fmt"

"github.com/opencontainers/runtime-spec/specs-go"
"tags.cncf.io/container-device-interface/pkg/cdi"

"github.com/NVIDIA/nvidia-container-toolkit/internal/errors"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package runtime

import (
"errors"
"fmt"
"io"
"os"
Expand All @@ -28,6 +27,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/NVIDIA/nvidia-container-toolkit/internal/errors"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package runtime

import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/opencontainers/runtime-spec/specs-go"

"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/errors"
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
)

Expand Down
Loading